Using interrupts for buttons

This commit is contained in:
Martin Brodbeck 2022-05-25 23:01:40 +02:00
parent fa774e088f
commit 93c1ed4107
2 changed files with 73 additions and 52 deletions

View File

@ -1,4 +0,0 @@
# TODOs
- Improve button press behavior / performance
- Add more config options (e.g. temp range)

121
main.py
View File

@ -1,14 +1,16 @@
from machine import Pin, I2C
from onewire import OneWire
from ds18x20 import DS18X20
from time import sleep_ms
from time import sleep_ms, ticks_ms, ticks_diff
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
import _thread
# Version
VERSION = "1.0.0"
# Relais
relais = Pin(18, machine.Pin.OUT, value = 1)
led_onboard = Pin(25, machine.Pin.OUT, value = 0)
#led_onboard = Pin(25, machine.Pin.OUT, value = 0)
# Temperature
temp_sensor = DS18X20(OneWire(Pin(28)))
@ -32,80 +34,103 @@ degree = (
0b00000,
0b00000,
)
# Buttons
button1 = Pin(17, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(16, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(15, Pin.IN, Pin.PULL_DOWN)
global button1_pressed
global button2_pressed
global button3_pressed
button1_pressed = False
button2_pressed = False
button3_pressed = False
umlaut_a = (
0b01010,
0b00000,
0b01110,
0b00001,
0b01111,
0b10001,
0b01111,
0b00000,
)
def ctrl_relais(active = True):
if active:
relais.value(0)
led_onboard.on()
#led_onboard.on()
else:
relais.value(1)
led_onboard.off()
def button_reader_thread():
global button1_pressed
global button2_pressed
global button3_pressed
while True:
if button1.value() == 1:
button1_pressed = True
if button2.value() == 1:
button2_pressed = True
if button3.value() == 1:
button3_pressed = True
sleep_ms(100)
#led_onboard.off()
lcd.backlight_on()
lcd.clear()
lcd.custom_char(0, degree)
lcd.custom_char(1, umlaut_a)
temp_tgt = 28.0
temp_gap = 0.5
is_heating = False
heat_string = ""
system_on = False
system_on_string = ""
system_on_string = "OFF"
temp_curr = 0
_thread.start_new_thread(button_reader_thread, ())
lcd.move_to(0, 0)
lcd.putstr(" G" + chr(1) + "rbox Manager\n (Ver. "+ VERSION + ")")
sleep_ms(3000)
# Buttons
PIN_BUTTON_1 = 17
PIN_BUTTON_2 = 16
PIN_BUTTON_3 = 15
button1 = Pin(PIN_BUTTON_1, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(PIN_BUTTON_2, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(PIN_BUTTON_3, Pin.IN, Pin.PULL_DOWN)
button1_last = ticks_ms()
button2_last = ticks_ms()
button3_last = ticks_ms()
def button_action(pin):
global temp_tgt
global button1_last
global button2_last
global button3_last
global system_on
global system_on_string
if pin is button1:
if ticks_diff(ticks_ms(), button1_last) > 750:
temp_tgt -= 0.5
#lcd.move_to(5, 1)
#lcd.putstr("{0:3.1f}".format(temp_tgt))
button1_last = ticks_ms()
elif pin is button2:
if ticks_diff(ticks_ms(), button2_last) > 750:
temp_tgt += 0.5
#lcd.move_to(5, 1)
#lcd.putstr("{0:3.1f}".format(temp_tgt))
button2_last = ticks_ms()
elif pin is button3:
if ticks_diff(ticks_ms(), button3_last) > 750:
system_on = not system_on
if system_on == True:
system_on_string = "ON "
else:
system_on_string = "OFF"
#lcd.move_to(13, 1)
#lcd.putstr(system_on_string)
button3_last = ticks_ms()
button1.irq(trigger = Pin.IRQ_RISING, handler = button_action)
button2.irq(trigger = Pin.IRQ_RISING, handler = button_action)
button3.irq(trigger = Pin.IRQ_RISING, handler = button_action)
while True:
temp_sensor.convert_temp()
sleep_ms(750)
temp_curr = temp_sensor.read_temp(sensor_id)
if button1_pressed == True:
temp_tgt -= 0.5
button1_pressed = False
if button2_pressed == True:
temp_tgt += 0.5
button2_pressed = False
if button3_pressed == True:
system_on = not system_on
button3_pressed = False
if system_on == True and temp_curr < temp_tgt - temp_gap:
is_heating = True
elif system_on == True and temp_curr > temp_tgt + temp_gap:
is_heating = False
elif system_on == False:
is_heating = False
ctrl_relais(is_heating)
if system_on == True:
system_on_string = "ON "
else:
system_on_string = "OFF"
is_heating = False
if is_heating == True:
heat_string = ">H<"
else:
@ -113,4 +138,4 @@ while True:
lcd.move_to(0, 0)
lcd.putstr("ACT: {0:3.1f}".format(temp_curr) + chr(0) + "C " + heat_string +
"\nTGT: {0:3.1f}".format(temp_tgt) + chr(0) + "C " + system_on_string)
"\nTGT: {0:3.1f}".format(temp_tgt) + chr(0) + "C " + system_on_string)