from machine import Pin, I2C
from i2c_lcd import I2cLcd
from rotary import Rotary
from utime import ticks_ms, sleep_ms
import gc
from screens import Lcd_Screens
from network import WLAN, STA_IF
#############################################################################
# setups e funções
selection = Rotary(5, 17, 16)
cursor = 0
cx = 0
cy = 0
bufferDict = {"ganhos": [[0,0,0,0], [0,0,0,0], [0,0,0,0]],
"setpoint": [0,0,0]} # ganho, sp
values = {"ganhos": {"kp": 0.0, 'ki': 0.0, 'kd': 0.0},
"setpoint": 0, "modo": "offline"}
state = "none"
lcd = I2cLcd(I2C(0), 0x27, 4, 20)
def clamp(value: int, maxValue: int) -> int:
value %= maxValue+1
if value < 0:
velue = maxValue
return value
def rotary_changed(change): # máquina de estados
global tela, state, cursor, block, lcd, values, cx, cy
if tela == "ganho" or tela == "setpoint":
cursor_tuples = {"ganho":(7, 8, 10, 11), "setpoint":(10, 11, 12)}
buff = bufferDict[tela]
if change == Rotary.ROT_CW:
buff[cursor] = (buff[cursor] + 1) % 10
elif change == Rotary.ROT_CCW:
buff[cursor] -= 1
elif change == Rotary.SW_PRESS:
state = "confirm_init"
elif change == Rotary.SW_RELEASE:
state = "none"
if block: return
cursor += 1
cursor = (cursor +1) % len(cursor_tuples[tela])
lcd.move_to(cursor_tuples[tela][cursor], 0)
return
if tela != "setpoint":
buff[cursor] = clamp(buff[cursor], 9)
else: # 0 - 270 máximo
buff = bufferDict[tela]
if cursor == 0:
buff[cursor] = clamp(buff[cursor], 2)
if buff[0] == 2 and buff[1] > 7:
lcd.hide_cursor()
lcd.move_to(11,0)
lcd.overwrite("7")
if buff[2] > 0:
lcd.move_to(12, 0)
lcd.overwrite("0")
buff[1] = 7
buff[2] = 0
lcd.move_to(10,0)
lcd.show_cursor()
elif cursor == 1:
if buff[0] == 2:
buff[cursor] = clamp(buff[cursor], 7)
if buff[cursor] == 7 and buff[2] > 0:
lcd.hide_cursor()
lcd.move_to(12,0)
lcd.overwrite("0")
lcd.move_to(11,0)
lcd.show_cursor()
buff[2] = 0
else:
buff[cursor] = clamp(buff[cursor], 9)
elif cursor == 2:
if buff[0] == 2 and buff[1] == 7:
buff[2] = 0
else:
buff[cursor] = clamp(buff[cursor], 9)
if block == False:
lcd.overwrite(str(buff[cursor]))
elif tela.name == "modo_on_offline":
cursor_tuple = (1, 11)
cursorHist = cursor
if change == Rotary.ROT_CW:
cursor += 1
if cursor > 1: cursor = 0
elif change == Rotary.ROT_CCW:
cursor -= 1
if cursor < 0: cursor = 1
elif change == Rotary.SW_PRESS:
state = "confirm_init"
elif change == Rotary.SW_RELEASE and tela.block == False:
state = "none"
if cursor: values["modo"] = "online"
else: values["modo"] = "offline"
print(values["modo"])
if tela.block == False:
lcd.hide_cursor()
if cursorHist != cursor: lcd.overwrite(' ')
lcd.move_to(cursor_tuple[cursor], 2)
lcd.overwrite("~")
elif tela.name == "online":
if change == Rotary.SW_PRESS:
state = "confirm_init"
elif change == Rotary.SW_RELEASE and tela.block == False:
state = "none"
elif tela.name == "offline":
# 9, 10, 12, 13
cursor_tuple = (9,10,12,13)
buff = bufferDict['ganhos']
cursorHist = cursor
if change == Rotary.ROT_CW:
buff[cy][cx] = clamp(buff[cy][cx] + 1, 9)
print("oi")
elif change == Rotary.ROT_CCW:
buff[cy][cx] = clamp(buff[cy][cx] - 1, 9)
print("oi")
elif change == Rotary.SW_PRESS:
state = "confirm_init"
elif change == Rotary.SW_RELEASE and tela.block == False:
state = "none"
cx += 1
if cx > 3: cy +=1
cx %= 4
cy %= 3
print(cx)
if tela.block == False:
lcd.hide_cursor()
lcd.move_to(cursor_tuple[cx], cy+1)
lcd.overwrite(str(buff[cy][cx]))
lcd.show_cursor()
print("oi")
gc.collect()
selection.add_handler(rotary_changed) # passa o callback para o objeto do encoder
tela = Lcd_Screens(lcd)
tela.inicializacao()
wifi = WLAN(STA_IF)
print(tela.name)
#############################################################################
# loop principal e máquina de estados
print("________________________________________________")
# delay = 0
while True:
if state == "confirm_init":
delay = ticks_ms() + 3000
state = "countdown"
if state != "countdown" or ticks_ms() < delay:
continue
state = "none"
if tela.name == "modo_on_offline":
if values["modo"] == "online":
tela.selecao_wifi()
else:
tela.offline()
elif tela.name == "online":
values["modo"] = "offline"
tela.modo_on_offline()
elif tela.name == "offline":
valores = values['ganhos']
valores['kp'] = float(''.join(str(i) for i in bufferDict['ganhos'][0])) / 100
valores['ki'] = float(''.join(str(i) for i in bufferDict['ganhos'][1])) / 100
valores['kd'] = float(''.join(str(i) for i in bufferDict['ganhos'][2])) / 100
print(valores['kp'], valores['ki'], valores['kd'])
elif tela.name == "ganho":
values[tela.name] = ''.join((str(i) for i in bufferDict[tela]))
print(values[tela])
tela = lcd_screens("setpoint")
elif tela.name == "setpoint":
values[tela.name] = ''.join((str(i) for i in bufferDict[tela]))
print(values[tela])
gc.collect()