Using interrupts for buttons
This commit is contained in:
parent
fa774e088f
commit
93c1ed4107
2 changed files with 73 additions and 52 deletions
4
TODO.md
4
TODO.md
|
@ -1,4 +0,0 @@
|
||||||
# TODOs
|
|
||||||
|
|
||||||
- Improve button press behavior / performance
|
|
||||||
- Add more config options (e.g. temp range)
|
|
119
main.py
119
main.py
|
@ -1,14 +1,16 @@
|
||||||
from machine import Pin, I2C
|
from machine import Pin, I2C
|
||||||
from onewire import OneWire
|
from onewire import OneWire
|
||||||
from ds18x20 import DS18X20
|
from ds18x20 import DS18X20
|
||||||
from time import sleep_ms
|
from time import sleep_ms, ticks_ms, ticks_diff
|
||||||
from lcd_api import LcdApi
|
from lcd_api import LcdApi
|
||||||
from pico_i2c_lcd import I2cLcd
|
from pico_i2c_lcd import I2cLcd
|
||||||
import _thread
|
|
||||||
|
# Version
|
||||||
|
VERSION = "1.0.0"
|
||||||
|
|
||||||
# Relais
|
# Relais
|
||||||
relais = Pin(18, machine.Pin.OUT, value = 1)
|
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
|
# Temperature
|
||||||
temp_sensor = DS18X20(OneWire(Pin(28)))
|
temp_sensor = DS18X20(OneWire(Pin(28)))
|
||||||
|
@ -32,80 +34,103 @@ degree = (
|
||||||
0b00000,
|
0b00000,
|
||||||
0b00000,
|
0b00000,
|
||||||
)
|
)
|
||||||
|
umlaut_a = (
|
||||||
# Buttons
|
0b01010,
|
||||||
button1 = Pin(17, Pin.IN, Pin.PULL_DOWN)
|
0b00000,
|
||||||
button2 = Pin(16, Pin.IN, Pin.PULL_DOWN)
|
0b01110,
|
||||||
button3 = Pin(15, Pin.IN, Pin.PULL_DOWN)
|
0b00001,
|
||||||
global button1_pressed
|
0b01111,
|
||||||
global button2_pressed
|
0b10001,
|
||||||
global button3_pressed
|
0b01111,
|
||||||
button1_pressed = False
|
0b00000,
|
||||||
button2_pressed = False
|
)
|
||||||
button3_pressed = False
|
|
||||||
|
|
||||||
def ctrl_relais(active = True):
|
def ctrl_relais(active = True):
|
||||||
if active:
|
if active:
|
||||||
relais.value(0)
|
relais.value(0)
|
||||||
led_onboard.on()
|
#led_onboard.on()
|
||||||
else:
|
else:
|
||||||
relais.value(1)
|
relais.value(1)
|
||||||
led_onboard.off()
|
#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)
|
|
||||||
|
|
||||||
lcd.backlight_on()
|
lcd.backlight_on()
|
||||||
lcd.clear()
|
lcd.clear()
|
||||||
lcd.custom_char(0, degree)
|
lcd.custom_char(0, degree)
|
||||||
|
lcd.custom_char(1, umlaut_a)
|
||||||
|
|
||||||
temp_tgt = 28.0
|
temp_tgt = 28.0
|
||||||
temp_gap = 0.5
|
temp_gap = 0.5
|
||||||
is_heating = False
|
is_heating = False
|
||||||
heat_string = ""
|
heat_string = ""
|
||||||
system_on = False
|
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:
|
while True:
|
||||||
temp_sensor.convert_temp()
|
temp_sensor.convert_temp()
|
||||||
sleep_ms(750)
|
sleep_ms(750)
|
||||||
temp_curr = temp_sensor.read_temp(sensor_id)
|
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:
|
if system_on == True and temp_curr < temp_tgt - temp_gap:
|
||||||
is_heating = True
|
is_heating = True
|
||||||
elif system_on == True and temp_curr > temp_tgt + temp_gap:
|
elif system_on == True and temp_curr > temp_tgt + temp_gap:
|
||||||
is_heating = False
|
is_heating = False
|
||||||
|
elif system_on == False:
|
||||||
|
is_heating = False
|
||||||
|
|
||||||
ctrl_relais(is_heating)
|
ctrl_relais(is_heating)
|
||||||
|
|
||||||
if system_on == True:
|
|
||||||
system_on_string = "ON "
|
|
||||||
else:
|
|
||||||
system_on_string = "OFF"
|
|
||||||
is_heating = False
|
|
||||||
|
|
||||||
if is_heating == True:
|
if is_heating == True:
|
||||||
heat_string = ">H<"
|
heat_string = ">H<"
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue