lotus: Clean up ADC code a bit

Should now work on non-pi and prints:

```
...
Framework Computer Inc:Lotus Keyboard:1: scan
Framework Computer Inc:Lotus Keyboard:1: 5 ms (200 Hz)
Framework Computer Inc:Lotus Keyboard:1: Temperature: 18.71
Framework Computer Inc:Lotus Keyboard:1: ADC Voltage: 0.1
Framework Computer Inc:Lotus Keyboard:1: scan
Framework Computer Inc:Lotus Keyboard:1: 6 ms (166 Hz)
Framework Computer Inc:Lotus Keyboard:1: Temperature: 18.24
Framework Computer Inc:Lotus Keyboard:1: ADC Voltage: 0.1
Framework Computer Inc:Lotus Keyboard:1: scan
Framework Computer Inc:Lotus Keyboard:1: 6 ms (166 Hz)
...
```

Signed-off-by: Daniel Schaefer <git@danielschaefer.me>
This commit is contained in:
Daniel Schaefer
2023-01-07 20:54:30 +08:00
parent 37aae1748e
commit 72247be69f
3 changed files with 41 additions and 39 deletions

View File

@@ -6,6 +6,7 @@
#include <config_common.h> #include <config_common.h>
// Prototype for Lotus with Raspberry Pi Pico // Prototype for Lotus with Raspberry Pi Pico
// It'll disable column drive because we need to remap some pins to there
// SDB is mapped to GP22 // SDB is mapped to GP22
#define PICO_LOTUS #define PICO_LOTUS

View File

@@ -36,10 +36,8 @@ void keyboard_pre_init_kb(void) {
writePinHigh(BOOT_DONE_GPIO); writePinHigh(BOOT_DONE_GPIO);
//// TODO: Do we ever need to disable it to save power? //// TODO: Do we ever need to disable it to save power?
#ifndef PICO_LOTUS
setPinOutput(MUX_ENABLE_GPIO); setPinOutput(MUX_ENABLE_GPIO);
writePinHigh(MUX_ENABLE_GPIO); writePinHigh(MUX_ENABLE_GPIO);
#endif
#ifdef RGB_MATRIX_ENABLE #ifdef RGB_MATRIX_ENABLE
//// TODO: Do we ever need to disable it to save power? //// TODO: Do we ever need to disable it to save power?

View File

@@ -13,10 +13,13 @@
#include "matrix.h" #include "matrix.h"
// Use raw ChibiOS ADC functions instead of those from QMK // Use raw ChibiOS ADC functions instead of those from QMK
// Using the QMK functions doesn't work yet
#define CHIBIOS_ADC TRUE #define CHIBIOS_ADC TRUE
bool letsgo = false; bool letsgo = false;
uint32_t prev_matrix_ts = 0; uint32_t prev_matrix_ts = 0;
float adc_voltage;
float temperature;
enum sample_state { enum sample_state {
s_never, // Never received a sample s_never, // Never received a sample
@@ -70,30 +73,28 @@ float average_samples(adcsample_t s[], int channel) {
void print_samples(adcsample_t s[]) { void print_samples(adcsample_t s[]) {
// Samples go from 0 to 4095 // Samples go from 0 to 4095
uprintf("Raw ADC samples: %d, %d, %d, %d\n", s[0], s[1], s[2], s[3]); uprintf("Raw ADC samples: %d, %d, %d, %d\n", samples[0], samples[1], samples[2], samples[3]);
float adv_avg = average_samples(s, 0); print("Temp: ");
float temp_avg = average_samples(s, 1); print_float(temperature);
uprintf("ADC Voltage: ");
print_float(adc_voltage);
}
void handle_sample(void) {
float adv_avg = average_samples(samples, 0);
float temp_avg = average_samples(samples, 1);
// Convert them to voltage (0 to 3.3V) // Convert them to voltage (0 to 3.3V)
float adc_v = adv_avg * CONV_FACTOR; float adc_v = adv_avg * CONV_FACTOR;
float temp_v = temp_avg * CONV_FACTOR; float temp_v = temp_avg * CONV_FACTOR;
// Convert to real temperature based on RP2040 datasheet // Convert to real temperature based on RP2040 datasheet
double temp = 27.0 - (temp_v - 0.706)/0.001721; temperature = 27.0 - (temp_v - 0.706)/0.001721;
adc_voltage = adc_v;
print("Temp: ");
print_float(temp);
uprintf("ADC Voltage: ");
print_float(adc_v);
}
void handle_sample(void) {
//print("Current:") //print("Current:")
print_samples(samples); //print_samples(samples);
//print("Previous:")
//print_samples(prev_samples);
print("\n");
} }
/* /*
@@ -102,7 +103,7 @@ void handle_sample(void) {
void adc_end_callback(ADCDriver *adcp) { void adc_end_callback(ADCDriver *adcp) {
(void)adcp; (void)adcp;
adc_state = s_ready; adc_state = s_ready;
print("adc_end_callback\n"); //print("adc_end_callback\n");
//handle_sample(); //handle_sample();
} }
@@ -137,10 +138,10 @@ void factory_trigger_adc(void) {
} }
adcConvert(&ADCD1, &adcConvGroup, adcConvert(&ADCD1, &adcConvGroup,
samples, ADC_GRP_BUF_DEPTH); samples, ADC_GRP_BUF_DEPTH);
print("After adcConvert\n"); //print("After adcConvert\n");
memcpy(prev_samples, samples, sizeof(samples)); memcpy(prev_samples, samples, sizeof(samples));
handle_sample(); handle_sample();
print("After handle_sample"); //print("After handle_sample");
} }
/** /**
@@ -151,7 +152,6 @@ void adc_gpio_init(int gpio) {
palSetLineMode(gpio, PAL_MODE_INPUT_ANALOG); palSetLineMode(gpio, PAL_MODE_INPUT_ANALOG);
} }
#ifndef PICO_LOTUS
/** /**
* Tell the mux to select a specific column * Tell the mux to select a specific column
* *
@@ -211,7 +211,13 @@ static bool interpret_adc_row(matrix_row_t cur_matrix[], uint16_t adc_value, int
} }
uprintf("Col %d - Row %d - ADC value:%04X, Voltage: %d\n", col, row, adc_value, voltage); uprintf("Col %d - Row %d - ADC value:%04X, Voltage: %d\n", col, row, adc_value, voltage);
// Don't update matrix on Pico to avoid messing with the debug system
// Can't attach the matrix anyways
#ifdef PICO_LOTUS
(void)key_state;
#else
cur_matrix[row] |= key_state ? 0 : (1 << col); cur_matrix[row] |= key_state ? 0 : (1 << col);
#endif
return changed; return changed;
} }
@@ -268,14 +274,18 @@ void drive_col(int col, bool high) {
return; return;
} }
// Don't drive columns on pico because we're using these GPIOs for other purposes
#ifdef PICO_LOTUS
(void)gpio;
#else
if (high) { if (high) {
// TODO: Could set up the pins with `setPinOutputOpenDrain` instead // TODO: Could set up the pins with `setPinOutputOpenDrain` instead
writePinHigh(gpio); writePinHigh(gpio);
} else { } else {
writePinLow(gpio); writePinLow(gpio);
} }
#endif
} }
#endif // PICO_LOTUS
/** /**
* Overriding behavior of matrix_scan from quantum/matrix.c * Overriding behavior of matrix_scan from quantum/matrix.c
@@ -283,31 +293,23 @@ void drive_col(int col, bool high) {
bool matrix_scan_custom(matrix_row_t current_matrix[]) { bool matrix_scan_custom(matrix_row_t current_matrix[]) {
bool changed = false; bool changed = false;
#ifdef PICO_LOTUS
print("scan\n"); print("scan\n");
uint32_t current_ts = timer_read32(); uint32_t current_ts = timer_read32();
if (prev_matrix_ts) { if (prev_matrix_ts) {
uprintf("prev: %lu\n", prev_matrix_ts); uint32_t delta = current_ts - prev_matrix_ts;
uprintf("curr: %lu\n", current_ts); uprintf("%lu ms (%ld Hz)\n", delta, 1000 / delta);
uprintf("%lu ms\n", current_ts - prev_matrix_ts);
uprintf("%ld Hz\n", 1000 / (current_ts - prev_matrix_ts));
} }
prev_matrix_ts = current_ts; prev_matrix_ts = current_ts;
chThdSleepMilliseconds(5); chThdSleepMilliseconds(5);
//chThdSleepMilliseconds(200);
#if !CHIBIOS_ADC #if !CHIBIOS_ADC
uint16_t val = analogReadPin(ADC_CH2_PIN); uint16_t val = analogReadPin(ADC_CH2_PIN);
uprintf("val: %u\n", val); adc_voltage = val * CONV_FACTOR;
#else #else
if (letsgo) { if (letsgo) {
print("letsgo\n");
factory_trigger_adc(); factory_trigger_adc();
} }
//adcConvert(&ADCD1, &adcConvGroup,
// samples, ADC_GRP_BUF_DEPTH);
//memcpy(prev_samples, samples, sizeof(samples));
//handle_sample();
// Interrupt-driven
//if (adc_state == s_ready) { //if (adc_state == s_ready) {
// print("new sample\n"); // print("new sample\n");
@@ -324,7 +326,11 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
// trigger_adc(); // trigger_adc();
//} //}
#endif // CHIBIOS_ADC #endif // CHIBIOS_ADC
#else // PICO_LOTUS uprintf("Temperature: ");
print_float(temperature);
uprintf("ADC Voltage: ");
print_float(adc_voltage);
for (int col = 0; col < MATRIX_COLS; col++) { for (int col = 0; col < MATRIX_COLS; col++) {
break; break;
// Drive column low so we can measure the resistors on each row in this column // Drive column low so we can measure the resistors on each row in this column
@@ -345,7 +351,6 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
// Drive column high again // Drive column high again
drive_col(col, true); drive_col(col, true);
} }
#endif // PICO_LOTUS
return changed; return changed;
} }
@@ -356,9 +361,7 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
* TODO: Do we need a de-init? Probably not. * TODO: Do we need a de-init? Probably not.
*/ */
static void adc_mux_init(void) { static void adc_mux_init(void) {
#ifndef PICO_LOTUS
writePinHigh(MUX_ENABLE); writePinHigh(MUX_ENABLE);
#endif
} }
/** /**