1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| from machine import Pin, I2C
import time
i2c = I2C(scl=Pin(2), sda=Pin(14), freq=20000) # création d'un objet i2c au mauvais endroit
# Comment éviter de coder en dur le brochage dans la bibliothèque ?
# Cette ligne devrait être dans le programme principal, passée en paramètre comme suit :
# afficheur = Grove_lcd(i2c)
class Grove_lcd():
# global i2c ne fonctionne pas, et ce n'est pas le meilleur moyen.
"""
Pour gerer l'afficheur 1602 Grove
"""
def __init__(self):
# initialisation
self.set_register(0x00, 0)
self.set_register(0x01, 0)
# toutes les Leds controllées par PWM
self.set_register(0x08, 0xAA)
# attente initialisation après mise sous tension
time.sleep_ms(50)
# envoi configuration afficheur 2 lignes
self.cmd(0x20 | 0x04 | 0x08)
time.sleep_us(4500)
self.cmd(0x20 | 0x04 | 0x08)
time.sleep_us(150)
self.cmd(0x20 | 0x04 | 0x08)
self.cmd(0x20 | 0x04 | 0x08)
# allumage afficheur
self.disp_ctrl = 0x04 | 0x00 | 0x00
self.display1(True)
self.clear()
self.disp_mode = 0x02 | 0x00
self.cmd(0x04 | self.disp_mode)
def set_register(self, reg, val):
val = bytes((reg, val))
i2c.writeto(0x62, val) # adresse i2c de l'afficheur pour le fond
def color(self, R, G, B):
self.set_register(0x04, R)
self.set_register(0x03, G)
self.set_register(0x02, B)
def cmd(self, command):
assert command >= 0 and command < 256
val = bytes((0x80, command))
i2c.writeto(0x3e, val) # adresse i2c de l'afficheur pour le texte
def write_char(self, c):
assert c >= 0 and c < 256
val = bytes((0x40, c))
i2c.writeto(0x3e, val)
def write(self, text):
text = str(text)
for char in text:
self.write_char(ord(char))
def setCursor(self, col, row):
col = (col | 0x80) if row == 0 else (col | 0xc0)
self.cmd(col)
def display1(self, state):
if state:
self.disp_ctrl |= 0x04
self.cmd(0x08 | self.disp_ctrl)
else:
self.disp_ctrl &= ~0x04
self.cmd(0x08 | self.disp_ctrl)
def clear(self):
self.cmd(0x01)
time.sleep_ms(2) |
Partager