#made by partly DIEGO but mostly by VIE ;-)
#The red button is an on/off switch
#The yellow button clears the screan and returns the curor
#The green button moves the cursor to the right and back to the left once the "X" is all the way right
#The blue button moves the cursor up and back to the bottom once the "X" is all the way up
#(avec l'utilisation des bibliothèques "lcd_api.py" et "pico_i2c_lcd.py")
#----Import de Bibliothèques----------------------------------------------
from machine import Pin, I2C, ADC
from time import sleep_ms
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
#----Entrées Boutons----------------------------------------------
button_rouge = Pin(13, Pin.IN, Pin.PULL_UP)
button_jaune = Pin(11, Pin.IN, Pin.PULL_UP)
#----Sorties Ecran LCD1----------------------------------------------
I2C_ADDR1 = 39 #adresse I2D
I2C_NUM_ROWS1 = 2 #nombre de lignes
I2C_NUM_COLS1 = 16 #nombre de colonnes
i2c1 = I2C(1, sda=Pin(2, Pin.OUT), scl=Pin(3, Pin.OUT), freq=400000)
lcd1 = I2cLcd(i2c1, I2C_ADDR1, I2C_NUM_ROWS1, I2C_NUM_COLS1)
#----Sorties Ecran LCD2----------------------------------------------
I2C_ADDR2 = 39 #adresse I2D
I2C_NUM_ROWS2 = 2 #nombre de lignes
I2C_NUM_COLS2 = 16 #nombre de colonnes
i2c2 = I2C(0, sda=Pin(0, Pin.OUT), scl=Pin(1, Pin.OUT), freq=400000)
lcd2 = I2cLcd(i2c2, I2C_ADDR2, I2C_NUM_ROWS2, I2C_NUM_COLS2)
#----Entrées Joystick 2 ----------------------------------------------
#xValue = xAxis.read_u16()
#Position gauche du joystick : 65535
#Position milieu (neutre) du joystick : 32759
#Position droite du joystick : 0
#yValue = yAxis.read_u16()
#Position haute du joystick : 65535
#Position milieu (neutre) du joystick : 32759
#Position basse du joystick : 0
joy_right = Pin(7, Pin.IN, Pin.PULL_UP)
joy_up = Pin(8, Pin.IN, Pin.PULL_UP)
joy_left = Pin(9, Pin.IN, Pin.PULL_UP)
joy_down = Pin(11, Pin.IN, Pin.PULL_UP)
#----Entrées Joystick 1 ----------------------------------------------
#xValue = xAxis.read_u16()
#Position gauche du joystick : 65535
#Position milieu (neutre) du joystick : 32759
#Position droite du joystick : 0
#yValue = yAxis.read_u16()
#Position haute du joystick : 65535
#Position milieu (neutre) du joystick : 32759
#Position basse du joystick : 0
xAxis = ADC(Pin(26))
yAxis = ADC(Pin(27))
button_push = Pin(28, Pin.IN, Pin.PULL_UP)
#----CREATION de fonctions----------------------------------------------
def eteindre_ecran_all():
lcd1.backlight_off()
lcd2.backlight_off()
print("éteindre_ecran()")
def allumer_ecran_all():
lcd1.backlight_on()
lcd2.backlight_on()
print("allumer_ecran()")
def clear_ecran_all():
lcd1.clear()
lcd2.clear()
print("clear_ecran()")
def X_wall_line():
if line == 0:
print("left wall")
return("left_wall")
elif line == 15:
print("right wall")
return("right_wall")
def X_wall_colomn():
if colomnNumber == 1:
print("bottom wall")
return("bottom_wall")
elif colomnNumber == 4:
print("top wall")
return("top_wall")
def put_X():
if screansOn == True:
if screanClear == False:
if onScreen1 == True:
lcd1.move_to(line,colomn)
lcd1.putstr("X")
else:
lcd2.move_to(line,colomn)
lcd2.putstr("X")
else:
lcd2.putstr("'X' not on screan")
lcd1.putstr("press yellow")
print("'X' not on screan, press yellow")
else:
clear_ecran_all()
lcd2.putstr("screans are off")
lcd1.putstr("press red")
print("screan is off, press red")
sleep_ms(sleepTime)
#-----Conditions initiales---------------------------------------------
allumer_ecran_all()
clear_ecran_all()
print("screan on")
#----Début du programme----------------------------------------------
print(i2c1.scan(),i2c2.scan(),) #scanner un bus I2C et renvoyer une liste d'adresses I2C valides qui sont connectées à ce bus
line = 0
colomn = 0
onScreen1 = True
screansOn = True
screanClear = False
colomnNumber = 2
sleepTime = 500
lcd1.move_to(line,colomn)
lcd1.putstr("X")
while True:
if button_rouge.value() == 0 :
print("button_rouge appuyé") #red button - on/off button
if screansOn == True:
eteindre_ecran_all()
sleep_ms(sleepTime)
screansOn = False
else:
clear_ecran_all() #clearing the error message if there is one and returning the "X"
allumer_ecran_all()
sleep_ms(sleepTime)
screansOn = True
if onScreen1 == True:
lcd1.move_to(line,colomn)
lcd1.putstr("X")
else:
lcd2.move_to(line,colomn)
lcd2.putstr("X")
screanClear = False
if button_jaune.value() == 0:
print("button_jaune appuyé") #yellow button - clear screan
if screanClear == False:
clear_ecran_all()
sleep_ms(sleepTime)
screanClear = True
else:
if onScreen1 == True: #yellow button - unclear screan
clear_ecran_all()
lcd1.move_to(line,colomn)
lcd1.putstr("X")
else:
lcd2.move_to(line,colomn)
lcd2.putstr("X")
sleep_ms(sleepTime)
print("unclear screan")
screanClear = False
xValue = xAxis.read_u16() #lecture de la valeur sur X du joystick
yValue = yAxis.read_u16() #lecture de la valeur sur Y du joystick
if xValue == 65535:
if X_wall_line() == "left_wall":
line = 15
else:
line -= 1
clear_ecran_all()
put_X()
print("go left")
if xValue == 0:
if X_wall_line() == "right_wall":
line = 0
else:
line += 1
clear_ecran_all()
put_X()
if yValue == 65535:
if X_wall_colomn() == "top_wall":
colomnNumber = 1
else:
colomnNumber += 1
if colomnNumber == 1 or colomnNumber == 3:
colomn = 1
else:
colomn = 0
if colomnNumber == 3 or colomnNumber == 4 :
onScreen1 = False
else:
onScreen1 = True
clear_ecran_all()
put_X()
print("go up")
print("colomn number", colomnNumber)
if yValue == 0:
if X_wall_colomn() == "bottom_wall":
print("bottom wall")
colomnNumber = 4
else:
colomnNumber -= 1
if colomnNumber == 1 or colomnNumber == 3:
colomn = 1
else:
colomn = 0
if colomnNumber == 3 or colomnNumber == 4 :
onScreen1 = False
else:
onScreen1 = True
clear_ecran_all()
put_X()
print("go down")
print("colomn number", colomnNumber)