Skip to content

Commit

Permalink
More refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
slabua committed Mar 12, 2024
1 parent 0c86800 commit 1b775aa
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 121 deletions.
132 changes: 73 additions & 59 deletions LCD/picomotodash_lcd_pd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@
import math

import ds18x20
import machine
import onewire
import picomotodash_env as pmdenv
import qrcode
import utime
from machine import (
ADC,
Pin,
PWM,
Timer,
# enable_irq,
# disable_irq,
)
from picographics import (
# DISPLAY_PICO_DISPLAY,
DISPLAY_PICO_DISPLAY_2,
PEN_P4,
PicoGraphics,
)
from utime import (
sleep,
sleep_ms,
sleep_us,
ticks_ms,
ticks_us,
time,
)
from pimoroni import RGBLED

gc.enable()
Expand Down Expand Up @@ -114,8 +128,8 @@
last_press_time = 0

# Timer
timer = machine.Timer()
start_time = utime.time()
timer = Timer()
start_time = time()


# Thread
Expand All @@ -127,16 +141,16 @@ def thread1(PWM2RPM_FACTOR):
n_repeats = 1

while True:
if pwm0.value() == 1:
cycle_start = utime.ticks_us()
if pwm22.value() == 1:
cycle_start = ticks_us()
for _ in range(n_repeats):
while pwm0.value() == 1:
while pwm22.value() == 1:
# pass
utime.sleep_us(100)
while pwm0.value() == 0:
sleep_us(100)
while pwm22.value() == 0:
# pass
utime.sleep_us(100)
cycle_stop = utime.ticks_us()
sleep_us(100)
cycle_stop = ticks_us()
cycle_duration = cycle_stop - cycle_start
cycle = 1000000 / (cycle_duration / n_repeats)
repeat_factor = ((50 - 20) / (1500 - 150)) * cycle + ( # y = mx + q
Expand Down Expand Up @@ -169,7 +183,7 @@ def in_use_led(in_use):

def blink_led(duration, r, g, b):
led.set_rgb(r, g, b)
utime.sleep(duration)
sleep(duration)
led.set_rgb(0, 0, 0)


Expand Down Expand Up @@ -240,22 +254,22 @@ def draw_qr_code(ox, oy, size, code):


# GPIO
temp_builtin = machine.ADC(4) # Built-in temperature sensor
temp_builtin = ADC(4) # Built-in temperature sensor

ds_pin = machine.Pin(11) # 1-Wire temperature sensors
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_scan_roms(ds_sensor, DS_RESOLUTION)

adc0 = machine.ADC(machine.Pin(26, machine.Pin.IN)) # Battery adc
adc1 = machine.ADC(machine.Pin(27, machine.Pin.IN)) # Fuel adc
adc2 = machine.ADC(machine.Pin(28, machine.Pin.IN)) # RPM adc
adc0 = ADC(Pin(26, Pin.IN)) # Battery adc
adc1 = ADC(Pin(27, Pin.IN)) # Fuel adc
adc2 = ADC(Pin(28, Pin.IN)) # RPM adc

# TEST
pwm1 = machine.PWM(machine.Pin(0))
pwm1.freq(275)
pwm1.duty_u16(32768)
pwm0 = PWM(Pin(0))
pwm0.freq(275)
pwm0.duty_u16(32768)
###
pwm0 = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN) # RPM pwm
pwm22 = Pin(22, Pin.IN, Pin.PULL_DOWN) # RPM pwm


# Pico Display boilerplate
Expand Down Expand Up @@ -288,7 +302,7 @@ def draw_qr_code(ox, oy, size, code):
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=False)

display.update()
utime.sleep(2)
sleep(2)
# TODO check
# j.open_file(BG_IMAGES[1])

Expand Down Expand Up @@ -342,18 +356,18 @@ def display_init(bv):


# Buttons
button_a = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
button_b = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
button_x = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
button_y = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
button_a = Pin(12, Pin.IN, Pin.PULL_UP)
button_b = Pin(13, Pin.IN, Pin.PULL_UP)
button_x = Pin(14, Pin.IN, Pin.PULL_UP)
button_y = Pin(15, Pin.IN, Pin.PULL_UP)


# def int_a(pin):
# global in_use
# global current_screen

# button_a.irq(handler=None)
# # state = machine.disable_irq()
# # state = disable_irq()

# print("Interrupted (A)")
# if not in_use and current_screen != 0:
Expand All @@ -364,16 +378,16 @@ def display_init(bv):
# in_use = True
# timer.init(
# freq=(1 / USE_TIMEOUT),
# mode=machine.Timer.PERIODIC,
# mode=Timer.PERIODIC,
# callback=set_in_use,
# )

# display.remove_clip()
# display_clear()

# utime.sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_a)
# # machine.enable_irq(state)
# sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
# # enable_irq(state)


def int_a(pin):
Expand All @@ -382,9 +396,9 @@ def int_a(pin):
global last_press_time

# button_a.irq(handler=None)
# state = machine.disable_irq()
# state = disable_irq()

new_press_time = utime.ticks_ms()
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (A)")
if not in_use and current_screen != 0:
Expand All @@ -395,17 +409,17 @@ def int_a(pin):
in_use = True
timer.init(
freq=(1 / USE_TIMEOUT),
mode=machine.Timer.PERIODIC,
mode=Timer.PERIODIC,
callback=set_in_use,
)

display.remove_clip()
display_clear()

# utime.sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_a)
# machine.enable_irq(state)
last_press_time = utime.ticks_ms()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
# enable_irq(state)
last_press_time = ticks_ms()


def int_b(pin):
Expand All @@ -418,7 +432,7 @@ def int_b(pin):

# button_b.irq(handler=None)

new_press_time = utime.ticks_ms()
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (B)")
set_in_use(in_use)
Expand Down Expand Up @@ -451,7 +465,7 @@ def int_b(pin):

display.update()

utime.sleep(INFO_SCROLL_DELAY)
sleep(INFO_SCROLL_DELAY)

if not button_x.value():
if current_screen == 0:
Expand All @@ -474,9 +488,9 @@ def int_b(pin):
BV = (BV + 1) % len(BACKLIGHT_VALUES)
display.set_backlight(BACKLIGHT_VALUES[BV])

# utime.sleep(BUTTON_DEBOUNCE_TIME)
# button_b.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_b)
last_press_time = utime.ticks_ms()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_b.irq(trigger=Pin.IRQ_FALLING, handler=int_b)
last_press_time = ticks_ms()


def int_x(pin):
Expand All @@ -490,7 +504,7 @@ def int_x(pin):

# button_x.irq(handler=None)

new_press_time = utime.ticks_ms()
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (X)")
set_in_use(in_use)
Expand Down Expand Up @@ -523,9 +537,9 @@ def int_x(pin):
)
blink_led(0.2, 0, 0, 255)

# utime.sleep(BUTTON_DEBOUNCE_TIME)
# button_x.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_x)
last_press_time = utime.ticks_ms()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_x.irq(trigger=Pin.IRQ_FALLING, handler=int_x)
last_press_time = ticks_ms()


def int_y(pin):
Expand All @@ -539,7 +553,7 @@ def int_y(pin):

# button_y.irq(handler=None)

new_press_time = utime.ticks_ms()
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (Y)")
set_in_use(in_use)
Expand All @@ -557,17 +571,17 @@ def int_y(pin):
temperature_matrix[temp_id] = []

elif current_screen == 5:
start_time = utime.time()
start_time = time()

# utime.sleep(BUTTON_DEBOUNCE_TIME)
# button_y.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_y)
last_press_time = utime.ticks_ms()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_y.irq(trigger=Pin.IRQ_FALLING, handler=int_y)
last_press_time = ticks_ms()


button_a.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_a)
button_b.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_b)
button_x.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_x)
button_y.irq(trigger=machine.Pin.IRQ_FALLING, handler=int_y)
button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=int_b)
button_x.irq(trigger=Pin.IRQ_FALLING, handler=int_x)
button_y.irq(trigger=Pin.IRQ_FALLING, handler=int_y)


# Interface
Expand Down Expand Up @@ -919,7 +933,7 @@ def screen_temperature():
temperatures = []
if onewire_sensors:
ds_sensor.convert_temp()
utime.sleep_ms(round(750 / (2 ** (12 - DS_RESOLUTION))))
sleep_ms(round(750 / (2 ** (12 - DS_RESOLUTION))))
temperatures.append(acq_temp(temp_builtin))
for ows in range(onewire_sensors):
temperatures.append(ds_sensor.read_temp(roms[ows]))
Expand Down Expand Up @@ -1094,7 +1108,7 @@ def screen_rpm():
def screen_stats():
display_clear()

uptime = utime.time() - start_time
uptime = time() - start_time
print("Uptime: " + str(uptime))

display.set_pen(whitePen)
Expand Down Expand Up @@ -1125,7 +1139,7 @@ def screen_stats():


while True:
utime.sleep(UPDATE_INTERVAL)
sleep(UPDATE_INTERVAL)

in_use_led(in_use)

Expand All @@ -1147,4 +1161,4 @@ def screen_stats():

screen_functions[current_screen]()

utime.sleep(UPDATE_INTERVAL)
sleep(UPDATE_INTERVAL)
Loading

0 comments on commit 1b775aa

Please sign in to comment.