import sys, time, framebuf, _thread, neopixel
from time import sleep
from ssd1306 import SSD1306_I2C
from machine import Pin, I2C, SoftI2C, ADC
import freesans20
from writer import Writer
import gc
time.sleep(0.1) # Wait for USB to become ready
OLED_WIDTH = const(128)
OLED_HEIGHT = const(64)
OLED_SDA = const(26)
OLED_SCL = const(27)
def init_i2c(scl_pin, sda_pin, id=0):
# Initialize I2C device
i2c_dev = I2C(id, 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
def display_text(text, x = 0, y = 0):
OLED.text('hello', 5, 5)
OLED.show()
# def display_image(buffer, width = 128, height = 32, x = 0, y = 0):
def display_image(buffer):
# fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MONO_HLSB)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
print(len(buffer))
OLED.fill(0)
# OLED.blit(fb, x, y)
OLED.blit(fb, 0, 0)
OLED.show()
return
from images import img_gadge_base, img_progress_one, img_progress_zero
RED = 200
GREEN = 64
BLUE = 3
def display_color_code():
OLED.fill(0)
wri = Writer(OLED, freesans20)
wri.set_textpos(OLED, 2, 6)
wri.printstring(f'R: {RED:03d}\n')
wri.printstring(f'G: {GREEN:03d}\n')
wri.printstring(f'B: {BLUE:03d}')
OLED.show()
def display_berry_logo():
img_buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
fb = framebuf.FrameBuffer(img_buffer, 32, 32, framebuf.MONO_HLSB)
OLED.blit(fb, 0, 0)
OLED.show()
def display_gadge():
full_width = 100
bar_height = 4
bar_width = 1
margin = 18
r_x = 1
r_y = 16
g_x = r_x
g_y = r_y + margin
b_x = r_x
b_y = g_y + margin
size = 2
ratio_r = int(RED / 255 * 100)
ratio_g = int(GREEN /255 * 100)
ratio_b = int(BLUE /255 * 100)
OLED.fill(0)
OLED.text(f'R:{RED:02X} G:{GREEN:02X} B:{BLUE:02X}', 0 , 0)
OLED.rect(r_x, r_y, full_width + 6, bar_height + 6, 1)
OLED.fill_rect(r_x + 3 , r_y + 3, ratio_r , bar_height, 1)
OLED.rect(g_x, g_y, full_width + 6, bar_height + 6, 1)
OLED.fill_rect(g_x + 3 , g_y + 3, ratio_g , bar_height, 1)
OLED.rect(b_x, b_y, full_width + 6, bar_height + 6, 1)
OLED.fill_rect(b_x + 3 , b_y + 3, ratio_b , bar_height, 1)
OLED.show()
return
def main():
display_gadge()
return
i2c_dev = init_i2c(scl_pin=OLED_SCL, sda_pin=OLED_SDA, id=1)
OLED = SSD1306_I2C(OLED_WIDTH, OLED_HEIGHT, i2c_dev)
button = Pin(28, Pin.IN, Pin.PULL_UP)
if __name__ == '__main__':
main()
while True:
print(str(button.value()))