Skip to content

Commit cef8c06

Browse files
authored
Merge pull request #2 from HLammers/v0.2.0
V0.2.0
2 parents 31cc6c5 + e0de801 commit cef8c06

File tree

11 files changed

+336
-361
lines changed

11 files changed

+336
-361
lines changed

README.md

Lines changed: 28 additions & 20 deletions
Large diffs are not rendered by default.

src/button.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,58 @@
2626
_IRQ_RISING_FALLING = Pin.IRQ_RISING | Pin.IRQ_FALLING
2727

2828
_DEBOUNCE_DELAY = const(50) # ms
29-
_KEPT_PRESSED_DELAY = const(200) # ms
29+
_LONG_PRESS_DELAY = const(200) # ms
3030

3131
_IRQ_LAST_STATE = const(0)
3232
_IRQ_IDLE_STATE = const(1)
3333
_IRQ_LAST_TIME = const(2)
34-
_IRQ_AVAILABLE = const(3)
34+
_IRQ_ONE_BUT_LAST_TIME = const(3)
35+
_IRQ_AVAILABLE = const(4)
3536

36-
_BUTTON_EVENT_RELEASED = const(0)
37-
_BUTTON_EVENT_PRESSED = const(1)
38-
_BUTTON_EVENT_KEPT = const(2)
37+
_BUTTON_EVENT_PRESS = const(0)
38+
_BUTTON_EVENT_LONG_PRESS = const(1)
3939

4040
class Button():
4141
'''button handling class; initiated by ui.__init__'''
4242

43-
def __init__(self, pin_number: int, pull_up: bool = False, trigger_kept_pressed: bool = False) -> None:
43+
def __init__(self, pin_number: int, pull_up: bool = False, long_press: bool = False) -> None:
4444
self.pin_number = pin_number
45-
self.trigger_kept_pressed = trigger_kept_pressed
46-
self.irq_data = array('l', [_NONE, _NONE, 0, 0])
45+
self.long_press = long_press
46+
self.irq_data = array('l', [_NONE, _NONE, 0, _NONE, 0])
4747
self.irq_data[_IRQ_IDLE_STATE] = int(pull_up)
4848
_pin = Pin
4949
pull_direction = _pin.PULL_UP if pull_up else _pin.PULL_DOWN
5050
_pin = _pin(pin_number, _pin.IN, pull_direction)
5151
self.pin = _pin
52-
self.last_pressed_time = _NONE
5352
_pin.irq(self._callback, _IRQ_RISING_FALLING)
5453

5554
def close(self) -> None:
5655
self.pin.irq(handler=None)
5756

5857
@micropython.viper
5958
def value(self) -> int:
60-
'''return 1 if triggered and trigger_kept_pressed == False, 1 if short pressed and trigger_kept_pressed == True, 2 if
61-
kept pressed and trigger_kept_pressed == True, 0 if released and trigger_kept_ressed == True or _NONE if no new data is
62-
available (yet); called by ui.process_user_input'''
59+
'''if long_press == False: return _BUTTON_EVENT_PRESS if pressed; if long_press == True: return _BUTTON_EVENT_PRESS if pressed
60+
shorter then _LONG_PRESS_DELAY milliseconds or return _BUTTON_EVENT_LONG_PRESS if pressed longer (if kept pressed the trigger
61+
happens after _LONG_PRESS_DELAY milliseconds); called by ui.process_user_input'''
6362
irq_data = ptr32(self.irq_data) # type: ignore
6463
if not irq_data[_IRQ_AVAILABLE]:
6564
return _NONE
6665
now = int(time.ticks_ms())
6766
if int(time.ticks_diff(now, irq_data[_IRQ_LAST_TIME])) < _DEBOUNCE_DELAY:
6867
return _NONE
6968
pressed = irq_data[_IRQ_LAST_STATE] != irq_data[_IRQ_IDLE_STATE]
70-
if bool(self.trigger_kept_pressed):
69+
if bool(self.long_press):
7170
if pressed:
72-
if int(self.last_pressed_time) == _NONE:
73-
self.last_pressed_time = irq_data[_IRQ_LAST_TIME]
74-
difference = int(time.ticks_diff(now, int(self.last_pressed_time)))
75-
if difference < _KEPT_PRESSED_DELAY:
71+
difference = int(time.ticks_diff(now, irq_data[_IRQ_LAST_TIME]))
72+
if difference < _LONG_PRESS_DELAY:
7673
return _NONE
7774
irq_data[_IRQ_AVAILABLE] = 0
78-
return _BUTTON_EVENT_KEPT
79-
difference = int(time.ticks_diff(irq_data[_IRQ_LAST_TIME], int(self.last_pressed_time)))
80-
self.last_pressed_time = _NONE
75+
return _BUTTON_EVENT_LONG_PRESS
76+
difference = int(time.ticks_diff(irq_data[_IRQ_LAST_TIME], irq_data[_IRQ_ONE_BUT_LAST_TIME]))
8177
irq_data[_IRQ_AVAILABLE] = 0
82-
return _BUTTON_EVENT_PRESSED if difference < _KEPT_PRESSED_DELAY else _BUTTON_EVENT_RELEASED
78+
return _BUTTON_EVENT_PRESS if difference < _LONG_PRESS_DELAY else _NONE
8379
irq_data[_IRQ_AVAILABLE] = 0
84-
return _BUTTON_EVENT_PRESSED if pressed else _NONE
80+
return _BUTTON_EVENT_PRESS if pressed else _NONE
8581

8682
@micropython.viper
8783
def _callback(self, pin):
@@ -92,4 +88,5 @@ def _callback(self, pin):
9288
return
9389
irq_data[_IRQ_LAST_STATE] = state
9490
irq_data[_IRQ_AVAILABLE] = 1
91+
irq_data[_IRQ_ONE_BUT_LAST_TIME] = irq_data[_IRQ_LAST_TIME]
9592
irq_data[_IRQ_LAST_TIME] = int(time.ticks_ms())

src/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def load_program_json_file(self, id: int) -> dict|None:
7777

7878
def save_data_json_file(self, file: str = 'data.json') -> None:
7979
'''save data set (self.data) to json file; called by self.save_back_up, router._save, router._save_program, Page*._save_*_settings
80-
or Page*.process_user_input and Page*.midi_learn'''
80+
and Page*.process_user_input'''
8181
data_file = open(f'/data_files/{file}', 'w')
8282
json.dump(self.data, data_file)
8383
data_file.close()
@@ -177,7 +177,7 @@ def load(self) -> None:
177177
self.settings = self.data['settings']
178178
self.programs_tuple = tuple((program[0] for program in programs))
179179

180-
###### this might not be necessary if the set-up is changed to ID-based data mapping
180+
###### this might not be necessary if the set-up is changed to ID-based data mapping
181181
def change_in_programs(self, field: str, old_value: str, new_value: str, condition_field: str = '', condition_value: str = '') -> None:
182182
'''change an old value into a new value for a given field in all program data files; called by router.rename_device and
183183
router.rename_preset'''

src/main_loops.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ def main():
6767
monitor data'''
6868
_ticks_diff = time.ticks_diff
6969
_ticks_ms = time.ticks_ms
70-
######
71-
# _get_encoder_input = ui._get_encoder_input
7270
_process_encoder_input = ui.process_encoder_input
7371
_read_midi_learn_data = router.read_midi_learn_data
7472
_process_midi_learn_data = ui.process_midi_learn_data
7573
_process_user_input = ui.process_user_input
7674
_process_monitor = ui.process_monitor
7775
_process_program_change_break = router.process_program_change_break
78-
######
79-
# previous_encoder_input = (None, None)
8076
previous_midi_learn_data = None
8177
main_loop_time = time.ticks_ms()
8278
while True:
@@ -87,10 +83,6 @@ def main():
8783
# turn display off after prolonged inactivity
8884
ui.check_sleep_time_out()
8985
# process encoders
90-
######
91-
# encoder_input = _get_encoder_input()
92-
# if encoder_input != (None, None):
93-
# _process_encoder_input(encoder_input)
9486
_process_encoder_input()
9587
# process buttons and other input
9688
_process_user_input()

src/router.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def update(self, reload_input_devices: bool = True, reload_output_devices: bool
100100
reload_output_triggers: bool = True, reload_input_presets: bool = True, reload_output_presets: bool = True,
101101
program_number: int = _NONE, already_waiting: bool = False) -> None:
102102
'''reload data and call ui.program_change to triggers redraw; called by main_loops.py: init, self._save, self._save_program,
103-
Page*.process_user_input, Page*.midi_learn, Page*._save_*_settings, Page*._callback_menu, Page*._callback_select'''
103+
ui._callback_confirm, ui._callback_select, Page*.process_user_input, Page*._save_*_settings, Page*._callback_menu,
104+
Page*._callback_select'''
104105
if not already_waiting:
105106
# request second thread to wait (handshake)
106107
with self.thread_lock:
@@ -416,7 +417,8 @@ def trigger(self) -> None:
416417
self.ui_trigger = key_int
417418

418419
def save_active_program(self, replace: bool) -> None:
419-
'''save active program changes, either replacing or as a new program after the original; called by PageProgram._callback_confirm'''
420+
'''save active program changes, either replacing or as a new program after the original; called by ui._callback_confirm and
421+
PageProgram._callback_confirm'''
420422
# request second thread to wait (handshake)
421423
with self.thread_lock:
422424
self.request_wait = True

0 commit comments

Comments
 (0)