22#include " Device.h"
33#include " timers.h"
44
5- #include " ulp_riscv.h"
6- #include " ulp_keypad.h"
7-
8- #include " esp_private/esp_sleep_internal.h"
9- #include " esp_private/adc_share_hw_ctrl.h"
10-
11- #define VELOCITY_SENSITIVE_KEYPAD_ADC_ATTEN ADC_ATTEN_DB_0
12- #define VELOCITY_SENSITIVE_KEYPAD_ADC_WIDTH ADC_BITWIDTH_12
13-
14- extern const uint8_t ulp_keypad_bin_start[] asm (" _binary_ulp_keypad_bin_start" );
15- extern const uint8_t ulp_keypad_bin_end[] asm (" _binary_ulp_keypad_bin_end" );
16-
175namespace Device ::KeyPad
186{
19- adc_oneshot_unit_handle_t adc_handle;
207 StaticTimer_t keypad_timer_def;
218 TimerHandle_t keypad_timer;
229
2310 void Init () {
2411 LoadCustomSettings ();
12+ InitFN ();
2513 InitKeyPad ();
2614 InitTouchBar ();
2715 }
2816
29- void Scan () {
30- if (ScanFN ())
31- return ;
32- if (ScanKeyPad ())
33- return ;
34- }
35-
36- void InitKeyPad () {
17+ // TODO Change to use interrupt
18+ void InitFN () {
3719 gpio_config_t io_conf;
3820
3921 // Config FN
@@ -51,66 +33,30 @@ namespace Device::KeyPad
5133#endif
5234 gpio_config (&io_conf);
5335
54- // Config Input Pins
55- if (!keypad_config.velocity_sensitive ) // Non velocity sensitive keypad
56- {
57- io_conf.intr_type = GPIO_INTR_DISABLE;
58- io_conf.mode = GPIO_MODE_INPUT;
59- io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
60- io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
61- io_conf.pin_bit_mask = 0 ;
62- for (uint8_t y = 0 ; y < y_size; y++)
63- { io_conf.pin_bit_mask |= (1ULL << keypad_read_pins[y]); }
64- gpio_config (&io_conf);
65- }
66- else // Velocity sensitive keypad
67- {
68- adc_oneshot_unit_init_cfg_t init_config = {
69- .unit_id = ADC_UNIT_1,
70- .ulp_mode = ADC_ULP_MODE_RISCV,
71- };
72- adc_oneshot_new_unit (&init_config, &adc_handle);
73-
74- adc_oneshot_chan_cfg_t adc_config = {
75- .atten = VELOCITY_SENSITIVE_KEYPAD_ADC_ATTEN,
76- .bitwidth = VELOCITY_SENSITIVE_KEYPAD_ADC_WIDTH,
77- };
78-
79- for (uint8_t y = 0 ; y < y_size; y++)
80- { adc_oneshot_config_channel (adc_handle, keypad_read_adc_channel[y], &adc_config); }
81- }
82-
83- // Config Output Pins
84- io_conf.intr_type = GPIO_INTR_DISABLE;
85- io_conf.mode = GPIO_MODE_OUTPUT;
86- io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
87- io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
88- io_conf.pin_bit_mask = 0 ;
89- for (uint8_t x = 0 ; x < x_size; x++)
90- { io_conf.pin_bit_mask |= (1ULL << keypad_write_pins[x]); }
91- gpio_config (&io_conf);
92-
93- // Calibrate the ADC
94- adc_set_hw_calibration_code (ADC_UNIT_1, VELOCITY_SENSITIVE_KEYPAD_ADC_ATTEN);
95-
96- // Enables the use of ADC and temperature sensor in monitor (ULP) mode
97- esp_sleep_enable_adc_tsens_monitor (true );
98-
9936 // Set up Matrix OS key config
10037 fnState.setConfig (&fn_config);
38+ }
10139
102- for ( uint8_t x = 0 ; x < x_size; x++)
103- {
104- for ( uint8_t y = 0 ; y < y_size; y++)
105- { keypadState[x][y]. setConfig (&keypad_config); }
106- }
40+ void InitKeyPad () {
41+ if (!velocity_sensitivity)
42+ { Binary::Init (); }
43+ else
44+ { FSR::Init (); }
10745 }
10846
10947 void StartKeyPad () {
110- ulp_riscv_load_binary (ulp_keypad_bin_start, (ulp_keypad_bin_end - ulp_keypad_bin_start));
111- ulp_riscv_run ();
48+ if (!velocity_sensitivity)
49+ {
50+ Binary::Start ();
51+ }
52+ else
53+ {
54+ FSR::Start ();
55+ }
11256
11357 keypad_timer = xTimerCreateStatic (NULL , configTICK_RATE_HZ / Device::keypad_scanrate, true , NULL , reinterpret_cast <TimerCallbackFunction_t>(Scan), &keypad_timer_def);
58+
59+
11460 xTimerStart (keypad_timer, 0 );
11561 }
11662
@@ -119,6 +65,31 @@ namespace Device::KeyPad
11965 StartTouchBar ();
12066 }
12167
68+ void Scan () {
69+ ScanFN ();
70+ ScanKeyPad ();
71+ }
72+
73+ bool ScanKeyPad () {
74+ if (!velocity_sensitivity)
75+ { return Binary::Scan (); }
76+ else
77+ { return FSR::Scan (); }
78+ }
79+
80+ bool ScanFN () {
81+ Fract16 read = gpio_get_level (fn_pin) * UINT16_MAX;
82+ // ESP_LOGI("FN", "%d", gpio_get_level(fn_pin));
83+ if (fn_active_low)
84+ { read = UINT16_MAX - (uint16_t )read; }
85+ if (fnState.update (read, false ))
86+ {
87+ if (NotifyOS (0 , &fnState))
88+ { return true ; }
89+ }
90+ return false ;
91+ }
92+
12293 void Clear () {
12394 fnState.Clear ();
12495
@@ -165,86 +136,6 @@ namespace Device::KeyPad
165136 }
166137 return nullptr ; // Return an empty KeyInfo
167138 }
168-
169- bool ScanFN () {
170- Fract16 read = gpio_get_level (fn_pin) * UINT16_MAX;
171- // ESP_LOGI("FN", "%d", gpio_get_level(fn_pin));
172- if (fn_active_low)
173- { read = UINT16_MAX - (uint16_t )read; }
174- if (fnState.update (read, false ))
175- {
176- if (NotifyOS (0 , &fnState))
177- { return true ; }
178- }
179- return false ;
180- }
181-
182- uint16_t middleOfThree (uint16_t a, uint16_t b, uint16_t c)
183- {
184- // Checking for a
185- if ((b <= a && a <= c) || (c <= a && a <= b))
186- return a;
187-
188- // Checking for b
189- if ((a <= b && b <= c) || (c <= b && b <= a))
190- return b;
191-
192- return c;
193- }
194-
195- uint16_t minOfThree (uint16_t a, uint16_t b, uint16_t c)
196- {
197- // Checking for a
198- if (a < b && a < c)
199- return a;
200-
201- // Checking for b
202- if (b < a && b < c)
203- return b;
204-
205- return c;
206- }
207-
208- bool ScanKeyPad ()
209- {
210- // ESP_LOGI("Keypad ULP", "Scaned: %lu", ulp_count);
211- uint16_t (*result)[8 ][3 ] = (uint16_t (*)[8 ][3 ])&ulp_result;
212- for (uint8_t y = 0 ; y < Device::y_size; y ++)
213- {
214- for (uint8_t x = 0 ; x < Device::x_size; x++)
215- {
216- // printf("%d,", result[x][y][0]);
217- uint16_t reading = 0 ;
218- if (result[x][y][0 ] != 0 && result[x][y][1 ] != 0 && result[x][y][2 ] != 0 )
219- {
220- reading = middleOfThree (result[x][y][0 ], result[x][y][1 ], result[x][y][2 ]);
221- }
222- // uint16_t reading = resxult[x][y][0] + result[x][y][1] + result[x][y][2] + result[x][y][ulp_latest];
223- Fract16 read = (reading << 4 ) + (reading >> 8 ); // Raw Voltage mapped. Will add calibration curve later.
224- bool updated = keypadState[x][y].update (read, true );
225- // if(keypadState[x][y].state == DEBUNCING)
226- // {
227- // printf("Key %d %d Debuncing - %d - [%d, %d, %d]\n", x, y, (uint16_t)read, result[x][y][ulp_latest], result[x][y][(ulp_latest + 1) % 3], result[x][y][(ulp_latest + 2) % 3]);
228- // }
229- // else if(keypadState[x][y].state == PRESSED)
230- // {
231- // printf("Key %d %d Pressed - %d - [%d, %d, %d]\n", x, y, (uint16_t)read, result[x][y][ulp_latest], result[x][y][(ulp_latest + 1) % 3], result[x][y][(ulp_latest + 2) % 3]);
232- // }
233- if (updated)
234- {
235- uint16_t keyID = (1 << 12 ) + (x << 6 ) + y;
236- if (NotifyOS (keyID, &keypadState[x][y]))
237- {
238- // printf("\n");
239- return true ;
240- }
241- }
242- }
243- }
244- // printf("\n");
245- return false ;
246- }
247-
248139 bool NotifyOS (uint16_t keyID, KeyInfo* keyInfo) {
249140 KeyEvent keyEvent;
250141 keyEvent.id = keyID;
0 commit comments