From: jimmy Date: Fri, 28 Jun 2024 01:26:51 +0000 (-0500) Subject: token board using LCD X-Git-Url: https://vault307.fbx.one/gitweb/lcdTracker.git/commitdiff_plain/365a9b12ce1459bd8db000edd40936deac029f7b token board using LCD --- 365a9b12ce1459bd8db000edd40936deac029f7b diff --git a/groveLCD.py b/groveLCD.py new file mode 100644 index 0000000..27cb53e --- /dev/null +++ b/groveLCD.py @@ -0,0 +1,79 @@ +import machine, time + +i2c=machine.I2C(0) + + +# this device has two I2C addresses +DISPLAY_RGB_ADDR = 0x62 +DISPLAY_TEXT_ADDR = 0x3e + +RGB_REG0=0x00 +RGB_REG1=0x01 +RGB_REG5=0x08 +RGB_REGR=0x04 +RGB_REGG=0x03 +RGB_REGB=0x02 + +i2c=machine.I2C(0) + +def reg_write(i2c,addr,reg,data): + msg=bytearray() + msg.append(data) + i2c.writeto_mem(addr,reg,msg) + +# set backlight to (R,G,B) (values from 0..255 for each) +def setRGB(r,g,b): + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REG0,0) + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REG1,0) + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REG5,0xaa) + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REGR,r) + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REGG,g) + reg_write(i2c,DISPLAY_RGB_ADDR,RGB_REGB,b) + +def textCommand(cmd): + reg_write(i2c,DISPLAY_TEXT_ADDR,0x80,cmd) + +def setText(text): + textCommand(0x01) + time.sleep(.05) + textCommand(0x08 | 0x04) + textCommand(0x28) + time.sleep(.05) + count=0 + row=0 + for c in text: + if c == '\n' or count == 16: + count=0 + row+=1 + if row ==2: + break + textCommand(0xc0) + if c == '\n': + continue + count +=1 + reg_write(i2c,DISPLAY_TEXT_ADDR,0x40,ord(c)) + +def setText_norefresh(text): + textCommand(0x02) # return home + time.sleep(.05) + textCommand(0x08 | 0x04) # display on, no cursor + textCommand(0x28) # 2 lines + time.sleep(.05) + count = 0 + row = 0 + while len(text) < 32: #clears the rest of the screen + text += ' ' + for c in text: + if c == '\n' or count == 16: + count = 0 + row += 1 + if row == 2: + break + textCommand(0xc0) + if c == '\n': + continue + count += 1 + reg_write(i2c,DISPLAY_TEXT_ADDR,0x40,ord(c)) + +def clearText(): + textCommand(0x01) \ No newline at end of file diff --git a/lcdToken.py b/lcdToken.py new file mode 100644 index 0000000..23bac18 --- /dev/null +++ b/lcdToken.py @@ -0,0 +1,78 @@ +import machine, time, random +import groveLCD as lcd + +global count +count=0 + +def updateDisplay(): + lcd.setText_norefresh("Number of stamps\n %d" %count) + +lcd.setRGB(50,0,50) +lcd.clearText() +lcd.setText_norefresh("Hello Marcus!") +time.sleep(2) +updateDisplay() + +btnP=machine.Pin(15,machine.Pin.IN,machine.Pin.PULL_UP) +btnD=machine.Pin(16,machine.Pin.IN,machine.Pin.PULL_UP) + +global btnP_pressed +btnP_pressed=False + +global btnD_pressed +btnD_pressed=False + +# button handlers (interrupts) +def btn_reader(pin): + btn=pin + while True: + if btnP.value()==0: + btnP_pressed=True + increment() + time.sleep(0.5) + btnP_pressed=False + elif btnD.value()==0: + btnD_pressed=True + decrement() + time.sleep(0.5) + btnD_pressed=False + +def increment(): + global count + count+=1 + global r + global g + global b + r=0 + g=0 + b=0 + def setR(): + global r + r=random.randrange(50) + return r + def setG(): + global g + g=random.randrange(50) + return g + def setB(): + b=random.randrange(50) + return b + if (count%10)==0: + setR();setG();setB() + lcd.setRGB(r,g,b) + lcd.setText("Good Job Marcus!\n Pick a prize") + time.sleep(3) + lcd.setRGB(50,0,50) + updateDisplay() + return count + +def decrement(): + global count + count-=1 + updateDisplay() + return count + +btnP.irq(trigger=machine.Pin.IRQ_FALLING,handler=btn_reader(btnP)) +btnD.irq(trigger=machine.Pin.IRQ_FALLING,handler=btn_reader(btnD)) + +# TODO put special surprise if reach "full" board <66> \ No newline at end of file