Skip to content

Commit

Permalink
Merge pull request #36 from gamaPhy/32-calibrate-min-value-of-sensors…
Browse files Browse the repository at this point in the history
…-on-power-up

32 calibrate min value of sensors on power up
  • Loading branch information
tlietz authored Dec 8, 2023
2 parents c17208e + 750b3af commit 4d04b03
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
1 change: 1 addition & 0 deletions firmware/freedom/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#define RGBLIGHT_EFFECT_RAINBOW_SWIRL
#define RGBLIGHT_DEFAULT_MODE (RGBLIGHT_MODE_RAINBOW_SWIRL + 5)
// #define RGBLIGHT_DEFAULT_VAL 0

#define WS2812_DI_PIN GP12
#define WS2812_PIO_USE_PIO0
Expand Down
65 changes: 49 additions & 16 deletions firmware/freedom/freedom.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
#include "helpers/sensor_read.h"
#include "helpers/lookup_table.h"

bool calibrating_sensors = false;

kb_config_t kb_config;
sensor_bounds_t running_sensor_bounds[SENSOR_COUNT];
uint8_t sensor_lookup_table[SENSOR_COUNT][MAX_ADC_READING];

const pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
const pin_scan_mode_t pin_scan_modes[MATRIX_ROWS][MATRIX_COLS] = PIN_SCAN_MODES;
const int sensor_nums[MATRIX_ROWS][MATRIX_COLS] = SENSOR_NUMS;

extern uint8_t sensor_lookup_table[SENSOR_COUNT][MAX_ADC_READING];
bool bootup_calibrated = false;
uint8_t startup_count = 0;

uint16_t min1, max1, min2, max2, min3, max3;
bool calibrating_sensors = false;

// Our bootmagic implementation allows optionally clearing EEPROM depending on
// whether the BOOTMAGIC_CLEAR button is held down along with the original BOOTMAGIC_LITE button.
Expand Down Expand Up @@ -84,11 +85,21 @@ bool calibration_successful(void) {
return true;
}

void keyboard_pre_init_user(void) {
setPinOutput(PICO_LED);
setPinOutput(WS2812_DI_PIN);
}

void keyboard_post_init_user(void) {
debug_enable = true;
setPinOutput(PICO_LED);
// have to turn on the rgb again after s min values have been calibrated
eeconfig_read_kb_datablock(&kb_config);
create_lookup_table(&kb_config, sensor_lookup_table);
for (int s = 0; s < SENSOR_COUNT; s++) {
running_sensor_bounds[s].min = -1;
running_sensor_bounds[s].max = 0;
}
rgblight_sethsv_noeeprom(HSV_BLACK);
}

bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
Expand Down Expand Up @@ -159,17 +170,39 @@ void matrix_scan_kb(void) {
static uint16_t key_timer;
if (timer_elapsed(key_timer) > 1000) {
key_timer = timer_read();
dprintf("(%i, %i) (%i, %i) (%i, %i)\n", min1, max1, min2, max2, min3, max3);
dprintf("(%i, %i) (%i, %i) (%i, %i)\n\n",
sensor_lookup_table[0][min1], sensor_lookup_table[0][max1],
sensor_lookup_table[1][min2], sensor_lookup_table[1][max2],
sensor_lookup_table[2][min3], sensor_lookup_table[2][max3]);
min1 = -1;
max1 = 0;
min2 = -1;
max2 = 0;
min3 = -1;
max3 = 0;

if (!bootup_calibrated) {
startup_count++;
// wait a bit for stable equilibrium, otherwise sensor min values will be off
if (startup_count == 2) {
for (int s = 0; s < SENSOR_COUNT; s++) {
kb_config.matrix_sensor_bounds[0][s].min = running_sensor_bounds[s].min;
}
bootup_calibrated = true;
create_lookup_table(&kb_config, sensor_lookup_table);
rgblight_reload_from_eeprom();
}
} else {
for (int s = 0; s < SENSOR_COUNT; s++) {
dprintf("(%i,%i) ", running_sensor_bounds[s].min, running_sensor_bounds[s].max);
}
dprintf("\n");

dprintf("(%i, %i) (%i, %i) (%i, %i)\n", kb_config.matrix_sensor_bounds[0][0].min, kb_config.matrix_sensor_bounds[0][0].max,
kb_config.matrix_sensor_bounds[0][1].min, kb_config.matrix_sensor_bounds[0][1].max,
kb_config.matrix_sensor_bounds[0][2].min, kb_config.matrix_sensor_bounds[0][2].max );

for (int s = 0; s < SENSOR_COUNT; s++) {
dprintf("(%i, %i) ",
sensor_lookup_table[s][running_sensor_bounds[s].min], sensor_lookup_table[s][running_sensor_bounds[s].max]);
}
dprintf("\n\n");

for (int s = 0; s < SENSOR_COUNT; s++) {
running_sensor_bounds[s].min = -1;
running_sensor_bounds[s].max = 0;
}
}
}

if (calibrating_sensors) {
Expand Down
3 changes: 3 additions & 0 deletions firmware/freedom/freedom.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ typedef struct {
} kb_config_t;


extern bool bootup_calibrated;
extern kb_config_t kb_config;
extern uint8_t sensor_lookup_table[SENSOR_COUNT][MAX_ADC_READING];
extern sensor_bounds_t running_sensor_bounds[SENSOR_COUNT];
extern bool calibrating_sensors;
extern const pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS];
extern const pin_scan_mode_t pin_scan_modes[MATRIX_ROWS][MATRIX_COLS];
Expand Down
36 changes: 9 additions & 27 deletions firmware/freedom/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
extern uint16_t min1, max1, min2, max2, min3, max3;

sensor_bounds_t running_sensor_bounds[SENSOR_COUNT];
uint8_t sensor_lookup_table[SENSOR_COUNT][MAX_ADC_READING];

void matrix_init_custom(void) {
Expand All @@ -33,29 +33,10 @@ bool scan_pin_analog(pin_t pin, uint8_t row, uint8_t col) {
uint16_t sensor_value = oversample(pin);
uint8_t key_x = sensor_lookup_table[sensor_nums[row][col]][sensor_value];

if (col == 0) {
if (sensor_value > max1) {
max1 = sensor_value;
}
if (sensor_value < min1) {
min1 = sensor_value;
}
}
if (col == 1) {
if (sensor_value > max2) {
max2 = sensor_value;
}
if (sensor_value < min2) {
min2 = sensor_value;
}
}
if (col == 2) {
if (sensor_value > max3) {
max3 = sensor_value;
}
if (sensor_value < min3) {
min3 = sensor_value;
}
if (sensor_value > running_sensor_bounds[sensor_nums[row][col]].max) {
running_sensor_bounds[sensor_nums[row][col]].max = sensor_value;
} else if (sensor_value < running_sensor_bounds[sensor_nums[row][col]].min) {
running_sensor_bounds[sensor_nums[row][col]].min = sensor_value;
}

uint16_t actuation_point;
Expand Down Expand Up @@ -143,9 +124,10 @@ void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
pin_scan_mode_t pin_mode = pin_scan_modes[current_row][col_index];
if (pin_mode == DIGITAL) {
current_row_value |= readPin(pin) ? 0 : row_shifter;
}
else if (!kb_config.calibrated) {
// analog keys don't operate if they aren't calibrated
}
else if (!kb_config.calibrated || !bootup_calibrated) {
// analog keys don't operate, but still scan matrix for data that other parts of code use
scan_pin_analog(pin, current_row, col_index);
current_row_value |= 0;
}
else if (pin_mode == ANALOG && !calibrating_sensors) {
Expand Down

0 comments on commit 4d04b03

Please sign in to comment.