forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.c
More file actions
167 lines (142 loc) · 5.79 KB
/
__init__.c
File metadata and controls
167 lines (142 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <string.h>
#include "shared-bindings/keypad/__init__.h"
#include "shared-bindings/keypad/EventQueue.h"
#include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/supervisor/__init__.h"
#include "supervisor/port.h"
#include "supervisor/shared/lock.h"
#include "supervisor/shared/tick.h"
supervisor_lock_t keypad_scanners_linked_list_lock;
static void keypad_scan_now(keypad_scanner_obj_t *self, uint64_t now);
static void keypad_scan_maybe(keypad_scanner_obj_t *self, uint64_t now);
void keypad_tick(void) {
// Fast path. Return immediately there are no scanners.
if (!MP_STATE_VM(keypad_scanners_linked_list)) {
return;
}
// Skip scanning if someone else has the lock. Don't wait for the lock.
if (supervisor_try_lock(&keypad_scanners_linked_list_lock)) {
uint64_t now = port_get_raw_ticks(NULL);
mp_obj_t scanner = MP_STATE_VM(keypad_scanners_linked_list);
while (scanner) {
keypad_scan_maybe(scanner, now);
scanner = ((keypad_scanner_obj_t *)scanner)->next;
}
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
}
void keypad_reset(void) {
keypad_scanner_obj_t *scanner = MP_STATE_VM(keypad_scanners_linked_list);
keypad_scanner_obj_t *next = MP_STATE_VM(keypad_scanners_linked_list);
while (scanner) {
next = scanner->next;
if (!scanner->never_reset) {
keypad_deregister_scanner(scanner);
}
scanner = next;
}
}
// Register a Keys, KeyMatrix, etc. that will be scanned in the background
void keypad_register_scanner(keypad_scanner_obj_t *scanner) {
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
scanner->next = MP_STATE_VM(keypad_scanners_linked_list);
MP_STATE_VM(keypad_scanners_linked_list) = scanner;
supervisor_release_lock(&keypad_scanners_linked_list_lock);
// One more request for ticks.
supervisor_enable_tick();
}
// Remove scanner from the list of active scanners.
void keypad_deregister_scanner(keypad_scanner_obj_t *scanner) {
// One less request for ticks.
supervisor_disable_tick();
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
if (MP_STATE_VM(keypad_scanners_linked_list) == scanner) {
// Scanner is at the front; splice it out.
MP_STATE_VM(keypad_scanners_linked_list) = scanner->next;
scanner->next = NULL;
} else {
keypad_scanner_obj_t *current = MP_STATE_VM(keypad_scanners_linked_list);
while (current) {
if (current->next == scanner) {
// Splice myself out.
current->next = scanner->next;
scanner->next = NULL;
break;
}
current = current->next;
}
}
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
void keypad_construct_common(keypad_scanner_obj_t *self, mp_float_t interval, size_t max_events, uint8_t debounce_threshold) {
size_t key_count = common_hal_keypad_generic_get_key_count(self);
self->debounce_counter = (int8_t *)m_malloc(sizeof(int8_t) * key_count);
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
keypad_eventqueue_obj_t *events = mp_obj_malloc(keypad_eventqueue_obj_t, &keypad_eventqueue_type);
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
self->debounce_threshold = debounce_threshold;
self->never_reset = false;
// Add self to the list of active keypad scanners.
keypad_register_scanner(self);
keypad_scan_now(self, port_get_raw_ticks(NULL));
}
static void keypad_scan_now(keypad_scanner_obj_t *self, uint64_t now) {
self->next_scan_ticks = now + self->interval_ticks;
self->funcs->scan_now(self, supervisor_ticks_ms());
}
static void keypad_scan_maybe(keypad_scanner_obj_t *self, uint64_t now) {
if (now < self->next_scan_ticks) {
return;
}
keypad_scan_now(self, now);
}
bool keypad_debounce(keypad_scanner_obj_t *self, mp_uint_t key_number, bool current) {
if (current) {
if ((self->debounce_counter[key_number] < self->debounce_threshold) &&
(++self->debounce_counter[key_number] == 0)) {
self->debounce_counter[key_number] = self->debounce_threshold;
return true;
}
} else {
if ((self->debounce_counter[key_number] > -self->debounce_threshold) &&
(--self->debounce_counter[key_number] == 0)) {
self->debounce_counter[key_number] = -self->debounce_threshold;
return true;
}
}
return false;
}
void keypad_never_reset(keypad_scanner_obj_t *self) {
self->never_reset = true;
}
void common_hal_keypad_generic_reset(void *self_in) {
keypad_scanner_obj_t *self = self_in;
size_t key_count = common_hal_keypad_generic_get_key_count(self);
memset(self->debounce_counter, -self->debounce_threshold, key_count);
keypad_scan_now(self, port_get_raw_ticks(NULL));
}
void common_hal_keypad_deinit_core(void *self_in) {
keypad_scanner_obj_t *self = self_in;
self->events = NULL;
}
bool common_hal_keypad_deinited(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return !self->events;
}
size_t common_hal_keypad_generic_get_key_count(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return self->funcs->get_key_count(self);
}
mp_obj_t common_hal_keypad_generic_get_events(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return self->events;
}
MP_REGISTER_ROOT_POINTER(mp_obj_t keypad_scanners_linked_list);