def __init__(self): self.w = 128 self.h = 64 self.oled = SH1106_I2C(self.w, self.h, self.i2c, Pin(16), 0x3c) self.oled.init_display() self.fps = 0 self.fps_sec_prev = 0 self.fps_c = 0 self.fps_new_sec = False
def __init__(self, id, i2c): super().__init__() self.id = 0 self.w = 128 self.h = 64 self.max_lines = 7 print(i2c) self.i2c = i2c self.style = "" self.SH1106 = SH1106_I2C(self.w, self.h, self.i2c) #, addr=0x70) self.image = Image.new('1', (self.w, self.h)) self.draw = ImageDraw.Draw(self.image) self.draw.rectangle((0, 0, self.w, self.h), outline=0, fill=0) self.font = ImageFont.load_default() self.SH1106.fill(0) self.SH1106.show() return
def __init__(self, id, i2c): super().__init__() self.num = id self.w = 128 self.h = 64 self.max_lines = 7 print(i2c) self.i2c = i2c self.menu_mode = False self.menu_window_start = 0 self.menu_window_height = self.max_lines - 1 self.highlight_line = 0 self.assignment = f"_encoder_{self.num}_" self.SH1106 = SH1106_I2C(self.w, self.h, self.i2c) #, addr=0x70) self.image = Image.new('1', (self.w, self.h)) self.draw = ImageDraw.Draw(self.image) self.draw.rectangle((0, 0, self.w, self.h), outline=0, fill=0) self.font = ImageFont.load_default() self.SH1106.fill(0) self.SH1106.show() return
from machine import Pin, SoftI2C from sh1106 import SH1106, SH1106_I2C from menu import MainMenu from time import sleep from time import time import config # # Create Display # i2c: SoftI2C = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) config.DISPLAY = SH1106_I2C(128, 64, i2c, addr=0x3c) config.DISPLAY.sleep(False) # # Create Menu # config.MAIN_MENU = MainMenu(config.DISPLAY) # Updates values on main menu config.delay(config.delay()) config.blob(config.blob()) config.active(config.active()) # Impoprt pin listeners import pinListener
import socket import json import struct from time import sleep from framebuf import FrameBuffer, MONO_VLSB from machine import I2C, Pin from sh1106 import SH1106_I2C i2c = I2C(scl=Pin(4), sda=Pin(5)) oled = SH1106_I2C(128, 64, i2c, None, 0x3c) HEADER = 64 PORT = 50500 FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" SERVER = "0.0.0.0" with open("params.json", "r") as r: params = json.load(r) HEADER = params["config"]["headerSize"] if "ipAddress" in params["config"]: SERVER = params["config"]["ipAddress"] elif "address": # getaddrinfo returns: # [(x, y, z, '', ('<IPADDRESS>', PORT))] # we can get the ip address with this index: [0][-1][0] SERVER = socket.getaddrinfo(params["config"]["address"], PORT)[0][-1][0]
# Display Image & text on I2C driven ssd1306 OLED display from machine import Pin, I2C from sh1106 import SH1106_I2C import framebuf WIDTH = 128 # oled display width HEIGHT = 32 # oled display height i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000) # Init I2C using pins GP8 & GP9 (default I2C0 pins) print("I2C Address : " + hex(i2c.scan()[0]).upper()) # Display device address print("I2C Configuration: " + str(i2c)) # Display I2C config oled = SH1106_I2C(WIDTH, HEIGHT, i2c) # Init oled display # Raspberry Pi logo as 32x32 bytearray 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" ) # Load the raspberry pi logo into the framebuffer (the image is 32x32) fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) # Clear the oled display in case it has junk on it. oled.fill(0) # Blit the image from the framebuffer to the oled display oled.blit(fb, 96, 0) # Add some text
import ubinascii import vl53l0x from sh1106 import SH1106_SPI, SH1106_I2C from writer import Writer import myfont12 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=40000) #configure display WIDTH = const(128) Lastline = 0 NewScreen = False HEIGHT = 64 shdisp = SH1106_I2C(WIDTH, HEIGHT, i2c, None) DispTopic = ["", "", "", "", ""] DispMsg = ["", "", "", "", ""] wri2 = Writer(shdisp, myfont12, verbose=False) # freesans20 Writer.set_clip(True, True) wri2.set_textpos(0, 0) wri2.printstring("Distance") shdisp.show() #configue VL53L0X TOF/Distance Sensor sensor = vl53l0x.VL53L0X(i2c) #configure MQTT channel = [b'Aqua/Tank']
from machine import Pin, I2C from sh1106 import SH1106_I2C import time _i2c = I2C(scl=Pin(5), sda=Pin(4), freq=1000000) _addr = _i2c.scan() _display = SH1106_I2C(128, 64, i2c=_i2c, addr=0x3c, res=None) _display.reset() _display.poweron() _display.fill(0) _display.text('Hello', 5, 5, 1) _display.show() time.sleep(5) _display.poweroff()
from machine import I2C, Pin from time import sleep OLED = 0 # 1 : 1.3 pouces, 0 : 0.96 pouces # choix de la bibliotheque en fonction du type d'ecran OLED if OLED == 1: from ssd1306 import SSD1306_I2C # module pour commander le OLED i2c = I2C(-1, Pin(22), Pin(21)) # pin SCK et SDA du OLED display = SSD1306_I2C(128, 64, i2c) # declaration taille ecran, pins else: from sh1106 import SH1106_I2C i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) display = SH1106_I2C(128, 64, i2c, Pin(16), 0x3c) display.sleep(False) bp = Pin(25, Pin.IN) # poussoir sur pin 25 pour passer # fonction pour attendre l'appui+relach. pour passer au graphique suivant def attend_appui(): while bp.value() == False: pass sleep(.02) # attend appui poussoir while bp.value() == True: pass sleep(.02) # attend le relachement display.fill(0) # Remplit l'afficheur avec 0 -> OFF display.show() # Mise a jour de l'affichage (envoie)
def run(so): pin_in = [] oled_width = 1 oled_heigth = -1 oled = 0 adc = 0 i2c_msg = 0 send_type = 0 # 0: data, 1: nfc, 2: adc #so.settimeout(0.05) disp = False data = b'' rdr = 0 nfc = '0' while (1): #so.setsockopt(socket.TCP_NODELAY,1) s = b'' '''try: s=so.recv(1024) except: pass''' s = bytes(recv_msg(so)) d = s print(len(s)) if (len(s) == 1): # send NFC nfc = '0' send_type = 1 if (rdr): (stat, _) = rdr.request(rdr.REQIDL) if stat == rdr.OK: (stat, raw_uid) = rdr.anticoll() if stat == rdr.OK: nfc = ":%02x%02x%02x%02x:" % (raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3]) elif (len(s) == 2): # set Input Pin if (not (int(s) in pin_in)): pin_in.append(int(s)) elif (len(s) == 3): # set Output Pin pin = int(int(s) / 10) Pin(pin, Pin.OUT).value(int(s) % 10) if pin in pin_in: pin_in.remove(pin) elif (len(s) == 4): # read ADC pin = int(s) adc = ADC(pin) send_type = 2 if pin in pin_in: pin_in.remove(pin) elif (len(s) == 8): # read I2C #print("i2c") s = int(s) nbytes = s % 100 s = int(s / 100) addr = s % 100 s = int(s / 100) scl = s % 100 sda = int(s / 100) #print(sda,scl,addr,nbytes) i2c = I2C(-1, scl=Pin(scl), sda=Pin(sda)) i2c_msg = i2c.readfrom(addr, nbytes) send_type = 3 #print(i2c_msg) elif (len(s) == 9): # set PWM s = int(s) duty = s % 10000 s = int(s / 10000) freq = s % 1000 + 1 pin = int(s / 1000) PWM(Pin(pin), freq=freq, duty=duty) if pin in pin_in: pin_in.remove(pin) elif (len(s) == 10): # set NFC s = s.decode() sda = int(s[-2:]) s = int(s[:-2]) rst = s % 100 s = int(s / 100) miso = s % 100 s = int(s / 100) mosi = s % 100 s = int(s / 100) sclk = s % 100 from mfrc522 import MFRC522 rdr = MFRC522(sclk, mosi, miso, rst, sda) elif (len(s) == 11): # set Neopixel s = int(s) val = [0, 0, 0] val[2] = (s % 1000) s = int(s / 1000) val[1] = (s % 1000) s = int(s / 1000) val[0] = (s % 1000) pin = int(s / 1000) np = neopixel.NeoPixel(Pin(pin), 1) np[0] = val np.write() if pin in pin_in: pin_in.remove(pin) elif (len(s) == 12): #set Display s = s.decode() disp = int(s[-2:]) s = s[:-2] s = int(s) oled_heigth = s % 1000 s = int(s / 1000) oled_width = s % 1000 s = int(s / 1000) scl = s % 100 sda = int(s / 100) i2c = I2C(-1, scl=Pin(scl), sda=Pin(sda)) print(disp, oled_heigth, oled_width, sda, scl) if (disp == 0): from sh1106 import SH1106_I2C oled = SH1106_I2C(oled_width, oled_heigth, i2c) elif (disp == 1): from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(oled_width, oled_heigth, i2c) elif (len(s) != 0): p = bytearray(s) fbuf = framebuf.FrameBuffer(p, oled_width, oled_heigth, framebuf.MONO_HLSB) oled.blit(fbuf, 0, 0) oled.show() #****** send ******# msg = 0 if (send_type == 0): for p in pin_in: msg += ((not Pin(p, Pin.IN, Pin.PULL_UP).value()) << p) try: if (send_type == 0): so.send(str(msg).encode()) elif (send_type == 1): so.send(str(nfc).encode()) elif (send_type == 2): so.send(str(adc).encode()) elif (send_type == 3): so.send(i2c_msg) send_type = 0 except: import machine machine.reset()
async def recv(reader): args = [] while True: s = bytes(await recv_msg(reader)) #s=await reader.readline() print(chr(s[0])) msg_type = chr(s[0]) msg = None if msg_type != 'S': msg = s.decode()[1:] args = list(map(int, msg.split("-"))) print(args) else: msg = s[1:] if (msg_type == 'f'): # read NFC flag = args[0] if flag: send_type[0] = 1 else: send_type[0] = 0 elif (msg_type == 'I'): # set Input Pin pin = args[0] if (not (pin in pin_in)): pin_in.append(pin) elif (msg_type == 'O'): # set Output Pin pin = args[0] value = args[1] Pin(pin, Pin.OUT).value(value) if pin in pin_in: pin_in.remove(pin) elif (msg_type == 'A'): # read ADC flag = args[0] if flag: pin = args[1] adc_pin[0] = pin if pin in pin_in: pin_in.remove(pin) send_type[0] = 2 else: adc_pin[0] = -1 send_type[0] = 0 elif (msg_type == 'i'): # set I2C sda_pin = args[0] scl_pin = args[1] #i2c = [0, 0, 0] isqc[0] = I2C(-1, scl=Pin(scl_pin), sda=Pin(sda_pin)) elif (msg_type == 'c'): # read I2C flag = args[0] if flag: addr = args[1] nbytes = args[2] isqc[1] = addr isqc[2] = nbytes send_type[0] = 3 print(send_type, isqc) else: send_type[0] = 0 elif (msg_type == 'P'): # set PWM pin = args[0] freq = args[1] duty = args[2] PWM(Pin(pin), freq=freq, duty=duty) if pin in pin_in: pin_in.remove(pin) elif (msg_type == 'n'): # set NFC sclk = args[0] mosi = args[1] miso = args[2] rst = args[3] sda = args[4] from mfrc522 import MFRC522 nfc_rdr[0] = MFRC522(sclk, mosi, miso, rst, sda) elif (msg_type == 'N'): # set Neopixel pin = args[0] red = args[1] green = args[2] blue = args[3] np = neopixel.NeoPixel(Pin(pin), 1) np[0] = [red, green, blue] np.write() if pin in pin_in: pin_in.remove(pin) elif (msg_type == 'D'): #set Display sda = args[0] scl = args[1] oled_height = args[3] oled_width = args[2] oled[0] = oled_width oled[1] = oled_height disp = args[4] i2c = I2C(-1, scl=Pin(scl), sda=Pin(sda)) print(disp, oled_height, oled_width, sda, scl) if (disp == 0): from sh1106 import SH1106_I2C oled[2] = SH1106_I2C(oled_width, oled_height, i2c) elif (disp == 1): from ssd1306 import SSD1306_I2C oled[2] = SSD1306_I2C(oled_width, oled_height, i2c) elif (msg_type == "S"): p = bytearray(msg) if oled[0] != '' and oled[1] != '': fbuf = framebuf.FrameBuffer(p, oled[0], oled[1], framebuf.MONO_HLSB) if oled[2] != '': oled[2].blit(fbuf, 0, 0) oled[2].show() yield
def write_to(self, driver: SH1106_I2C): from model import OP_MODE_OFF, STATE_HEAT is_heating = (self.data.state == STATE_HEAT) wri_t = Writer(driver, freesans40, verbose=False) wri_t.set_clip(False, False, False) # Char wrap Writer.set_textpos(driver, 16 + ROW_OFFSET, 26) if is_heating: driver.fill_rect(0, 14 + ROW_OFFSET, driver.width - 10, wri_t.height(), 1) wri_t.printstring(str(int(self.data.current_temperature)) + ".", invert=is_heating) wri_t_s = Writer(driver, freesans23, verbose=False) wri_t_s.set_clip(False, False, False) # Char wrap Writer.set_textpos(driver, 29 + ROW_OFFSET, 85) wri_t_s.printstring(str(self.data.current_temperature)[-1:], invert=is_heating) if is_heating: driver.fill_rect(0, 52 + ROW_OFFSET, driver.width, 4, 0) driver.text("H", driver.width - 8, 16 + ROW_OFFSET) driver.text("E", driver.width - 8, 16 + 9 + ROW_OFFSET) driver.text("A", driver.width - 8, 16 + 9 * 2 + ROW_OFFSET) driver.text("T", driver.width - 8, 16 + 9 * 3 + ROW_OFFSET) driver.text("{0:.1f}%RH".format(self.data.sensor_sample.h), 0, ROW_OFFSET) pressure_str = "{0:.1f}kPa".format(self.data.sensor_sample.p / 10) driver.text(pressure_str, driver.width - len(pressure_str) * 8, 0 + ROW_OFFSET) driver.text("room", driver.height - 16, 56 + ROW_OFFSET) if self.data.operation_mode == OP_MODE_OFF: driver.text("OFF", driver.width - 24, 20 + ROW_OFFSET)
def write_to(self, driver: SH1106_I2C): from sys_status import instance as sys_status if sys_status.boot: driver.text('Starting...', 0, ROW_OFFSET) if sys_status.sensor: driver.text('Sensor OK', 0, 10 + ROW_OFFSET) else: driver.text('Sensor checking', 0, 10 + ROW_OFFSET) if sys_status.hass_api: driver.text('HASS OK', 0, 20) else: driver.text('HASS connecting', 0, 20 + ROW_OFFSET) if sys_status.mqtt: driver.text('MQTT OK', 0, 30) else: driver.text('MQTT connecting', 0, 30 + ROW_OFFSET) else: driver.text('Running', 0, ROW_OFFSET) driver.text('Sensor {}'.format('OK' if sys_status.sensor else 'fail'), 0, 10 + ROW_OFFSET) driver.text('HASS {}'.format('OK' if sys_status.hass_api else 'conn lost'), 0, 20 + ROW_OFFSET) driver.text('MQTT {}'.format('OK' if sys_status.mqtt else 'conn lost'), 0, 30 + ROW_OFFSET)
def __init__(self, i2c: I2C): self.driver = SH1106_I2C(i2c=i2c, addr=DISP_I2C_ADDR, width=128, height=64) if ROTATE: self.driver.rotate(True, update=True)
def write_to(self, driver: SH1106_I2C): from model import ATTR_OP_MODE driver.text("set", int(driver.width / 2) - 12, 56 + ROW_OFFSET) driver.fill_rect(0, ROW_OFFSET, driver.width, 2, 1) # top driver.fill_rect(0, ROW_OFFSET, 2, driver.height, 1) # left driver.fill_rect(driver.width - 2, ROW_OFFSET, 2, driver.height, 1) # right driver.fill_rect(0, driver.height - 2 + ROW_OFFSET, int(driver.width / 2) - 12 - 4, 2, 1) # bottom-left driver.fill_rect(int(driver.width / 2) + 12 + 4, driver.height - 2 + ROW_OFFSET , int(driver.width / 2) - 12 - 4, 2, 1) # bottom-right wri = Writer(driver, freesans40, verbose=False) if self.lc.last_item == ATTR_OP_MODE: text = self.lc.operation_mode.upper() text_w = wri.stringlen(text) Writer.set_textpos(driver, 16 + ROW_OFFSET, int((driver.width - text_w) / 2) + 2) wri.printstring(text) else: Writer.set_textpos(driver, 16 + ROW_OFFSET, 26) wri.printstring("{0:.1f}".format(self.lc.temperature))