from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf, sys
#set res of ssd1306
pix_res_x = 128
pix_res_y = 64
def init_i2c(scl_pin, sda_pin):
# Initialize I2C device
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
#Display Text
def display_text(oled):
# Display text on the OLED
oled.text("Raspberry Pi", 5, 5)
oled.text("Pico", 5, 15)
oled.show()
# Test for sdcard block protocol
# Peter hinch 30th Jan 2016
import os, sdcard, machine
def sdtest():
spi = machine.SPI(1)
spi.init() # Ensure right baudrate
sd = sdcard.SDCard(spi, machine.Pin.board.X21) # Compatible with PCB
vfs = os.VfsFat(sd)
os.mount(vfs, "/fc")
print("Filesystem check")
print(os.listdir("/fc"))
line = "abcdefghijklmnopqrstuvwxyz\n"
lines = line * 200 # 5400 chars
short = "1234567890\n"
fn = "/fc/rats.txt"
print()
print("Multiple block read/write")
with open(fn, "w") as f:
n = f.write(lines)
print(n, "bytes written")
n = f.write(short)
print(n, "bytes written")
n = f.write(lines)
print(n, "bytes written")
with open(fn, "r") as f:
result1 = f.read()
print(len(result1), "bytes read")
fn = "/fc/rats1.txt"
print()
print("Single block read/write")
with open(fn, "w") as f:
n = f.write(short) # one block
print(n, "bytes written")
with open(fn, "r") as f:
result2 = f.read()
print(len(result2), "bytes read")
os.umount("/fc")
print()
print("Verifying data read back")
success = True
if result1 == "".join((lines, short, lines)):
print("Large file Pass")
else:
print("Large file Fail")
success = False
if result2 == short:
print("Small file Pass")
else:
print("Small file Fail")
success = False
print()
print("Tests", "passed" if success else "failed")
def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26) # setting pins that are used
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev) # setting res and display type (spi/i2c)
display_text(oled)
sdtest()
if __name__ == '__main__':
main()