2023-10-16 14:29:23 -07:00
|
|
|
/*
|
2023-10-19 16:20:41 -07:00
|
|
|
* userio.c: reacting when you fiddle with buttons, switches, and knobs
|
2023-10-16 14:29:23 -07:00
|
|
|
*
|
|
|
|
* mode switch: modes are changed only when the button is not being
|
|
|
|
* pushed, as button being pushed negates the mode switch response.
|
|
|
|
* for the mode to change, the mode switch must be in the new position
|
|
|
|
* without wavering for at least 3 rounds. this will prevent the
|
|
|
|
* mode from changing by someone fucking around with the button or
|
|
|
|
* when the button is used normally.
|
|
|
|
*
|
|
|
|
* button: is connected to mode switch ADC. if below threshold, then
|
|
|
|
* we can be certain button is pushed. we still wait a debounce
|
|
|
|
* round to ensure button is pushed. our code can fire events on
|
|
|
|
* either button being held or button being released. the actual
|
|
|
|
* push event does not do anything with this code.
|
|
|
|
*
|
|
|
|
* settings knobs: these are used by the settings module which works
|
|
|
|
* with the other modules and the button for setting user settings.
|
|
|
|
* the values are scaled from the ADC value, with bounds being set
|
|
|
|
* on the low and high ends and further scaled for usability.
|
|
|
|
* please read the manual if you want to know what settings do.
|
|
|
|
*
|
|
|
|
* file creation: 20231015 0122
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2023-10-19 19:53:04 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "adc.h"
|
|
|
|
#include "testo.h"
|
2023-10-16 14:29:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MODE_CONT 0
|
2023-10-19 19:53:04 -07:00
|
|
|
#define MODE_CONT_TARGET 3072 // (4096 * (1 - (1 / 4)))
|
2023-10-16 14:29:23 -07:00
|
|
|
|
|
|
|
#define MODE_FUN 1
|
2023-10-19 19:53:04 -07:00
|
|
|
#define MODE_FUN_TARGET 2731 // (4096 * (1 - (1 / 3)))
|
2023-10-16 14:29:23 -07:00
|
|
|
|
|
|
|
#define MODE_DIODE 2
|
2023-10-19 19:53:04 -07:00
|
|
|
#define MODE_DIODE_TARGET 2048 // (4096 * 1 - ((1 / 2)))
|
2023-10-16 14:29:23 -07:00
|
|
|
|
2023-10-19 19:53:04 -07:00
|
|
|
#define MODE_SET_LIMIT 60 // 3x worst case expected (1% tol)
|
|
|
|
#define MODE_CHANGE_TICKS 4 // how many samples before switching modes
|
2023-10-16 14:29:23 -07:00
|
|
|
|
2023-10-19 16:20:41 -07:00
|
|
|
#define MODE_ANALOG_MIN 20
|
2023-10-16 14:29:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-19 16:20:41 -07:00
|
|
|
uint8_t mode;
|
|
|
|
uint8_t mode_next;
|
|
|
|
uint8_t mode_count;
|
2023-10-19 19:53:04 -07:00
|
|
|
static const int16_t mode_targets[] = {
|
2023-10-19 16:20:41 -07:00
|
|
|
MODE_CONT_TARGET, MODE_FUN_TARGET, MODE_DIODE_TARGET
|
|
|
|
};
|
2023-10-16 14:29:23 -07:00
|
|
|
|
|
|
|
uint8_t knob[2];
|
|
|
|
|
|
|
|
uint8_t btn = 0;
|
|
|
|
uint16_t btn_held = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-19 16:20:41 -07:00
|
|
|
void userio_update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-16 14:29:23 -07:00
|
|
|
void userio_parse()
|
|
|
|
{
|
2023-10-19 19:53:04 -07:00
|
|
|
uint8_t i;
|
|
|
|
volatile int16_t m, w;
|
|
|
|
|
|
|
|
// button
|
|
|
|
// debounce is handled by the fact that we've had to have averaged
|
|
|
|
// about 16ms of measured hold time below the zero threshold.
|
|
|
|
m = adc_avg[ADC_SET_MODE];
|
|
|
|
if (m < MODE_ANALOG_MIN) {
|
|
|
|
// button is pushed
|
|
|
|
btn = 1;
|
|
|
|
if (btn_held != 0xffff) btn_held++;
|
|
|
|
} else if (btn == 1) {
|
|
|
|
// button is released
|
|
|
|
btn = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mode
|
|
|
|
if (!btn) {
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
// clear mode_count if we aren't actively changing modes
|
|
|
|
if (i == 3) {
|
|
|
|
mode_count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize mode selection
|
|
|
|
w = abs(m - mode_targets[i]);
|
|
|
|
|
|
|
|
// determine if we mode switch
|
|
|
|
if (w < MODE_SET_LIMIT) {
|
|
|
|
// currently in this mode? if so, ignore it
|
|
|
|
if (mode == i) continue;
|
|
|
|
|
|
|
|
// is the next mode set to this item?
|
|
|
|
if (mode_next == i) {
|
|
|
|
// increment the verification count
|
|
|
|
mode_count++;
|
|
|
|
if (mode_count >= MODE_CHANGE_TICKS) {
|
|
|
|
// we've got enough data to do a change of mode
|
|
|
|
mode = i;
|
|
|
|
mode_count = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// set the next mode to the newly selected mode
|
|
|
|
mode_next = i;
|
|
|
|
mode_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-16 14:29:23 -07:00
|
|
|
}
|