|
|
|
@ -8,100 +8,283 @@ const int pushPin = 5; |
|
|
|
|
const int wheelPin1 = 18; |
|
|
|
|
const int wheelPin2 = 23; |
|
|
|
|
|
|
|
|
|
#define DEBOUNCE_WHEEL_MS 2 |
|
|
|
|
#define DEBOUNCE_BTN_MS 5 |
|
|
|
|
#define DEBOUNCE_WHEEL_MS 6 |
|
|
|
|
#define DEBOUNCE_BTN_MS 20 |
|
|
|
|
|
|
|
|
|
static void debounce_service(void *arg); |
|
|
|
|
static TaskHandle_t hDebouncer; |
|
|
|
|
static void handle_pushbtn(void *arg); |
|
|
|
|
static void handle_wheel(void *arg); |
|
|
|
|
|
|
|
|
|
void knob_init() { |
|
|
|
|
printf("Knob init\n"); |
|
|
|
|
|
|
|
|
|
// gpio_config_t cfgWheel = {
|
|
|
|
|
// .pin_bit_mask = (1 << wheelPin1) /*| (1 << wheelPin2)*/,
|
|
|
|
|
// .mode = GPIO_MODE_INPUT,
|
|
|
|
|
// .pull_up_en = 1,
|
|
|
|
|
// .intr_type = GPIO_INTR_NEGEDGE, // neg means active
|
|
|
|
|
// };
|
|
|
|
|
// gpio_config(&cfgWheel);
|
|
|
|
|
|
|
|
|
|
gpio_config_t cfgPush = { |
|
|
|
|
.pin_bit_mask = (1 << pushPin) | (1 << wheelPin1) | (1 << wheelPin2), |
|
|
|
|
.pin_bit_mask = (1 << pushPin) | (1 << wheelPin1), |
|
|
|
|
.mode = GPIO_MODE_INPUT, |
|
|
|
|
.pull_up_en = 1 |
|
|
|
|
.pull_up_en = 1, |
|
|
|
|
.intr_type = GPIO_INTR_ANYEDGE, // neg means active
|
|
|
|
|
}; |
|
|
|
|
gpio_config(&cfgPush); |
|
|
|
|
|
|
|
|
|
int rv = xTaskCreate(debounce_service, "debo", 4096, NULL, 6, &hDebouncer); |
|
|
|
|
assert (rv == pdPASS); |
|
|
|
|
cfgPush.intr_type = GPIO_INTR_DISABLE; |
|
|
|
|
cfgPush.pin_bit_mask = (1 << wheelPin2); |
|
|
|
|
gpio_config(&cfgPush); |
|
|
|
|
|
|
|
|
|
gpio_install_isr_service(0); |
|
|
|
|
gpio_intr_enable(pushPin); |
|
|
|
|
gpio_intr_enable(wheelPin1); |
|
|
|
|
// gpio_intr_enable(wheelPin2);
|
|
|
|
|
|
|
|
|
|
gpio_isr_handler_add(pushPin, handle_pushbtn, (void *)0); |
|
|
|
|
gpio_isr_handler_add(wheelPin1, handle_wheel, (void *)0); |
|
|
|
|
//gpio_isr_handler_add(wheelPin2, handle_wheel, (void *)1);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void __attribute__((noreturn)) debounce_service(void *arg) { |
|
|
|
|
struct debo { |
|
|
|
|
bool state; |
|
|
|
|
uint32_t start_time; |
|
|
|
|
uint32_t debo_time; |
|
|
|
|
#if 0 |
|
|
|
|
enum state { |
|
|
|
|
S_11 = 0, |
|
|
|
|
|
|
|
|
|
S_FWD_1_01 = 1, |
|
|
|
|
S_FWD_2_00 = 2, |
|
|
|
|
S_FWD_3_10 = 3, |
|
|
|
|
|
|
|
|
|
S_BCK_1_10 = 4, |
|
|
|
|
S_BCK_2_00 = 5, |
|
|
|
|
S_BCK_3_01 = 6, |
|
|
|
|
|
|
|
|
|
S_ILLEGAL = 7, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct debo state[3] = { |
|
|
|
|
{.debo_time = pdMS_TO_TICKS(DEBOUNCE_BTN_MS)}, // push
|
|
|
|
|
{.debo_time = pdMS_TO_TICKS(DEBOUNCE_WHEEL_MS), .state=1}, // wheel input 1
|
|
|
|
|
{.debo_time = pdMS_TO_TICKS(DEBOUNCE_WHEEL_MS), .state=1} // wheel input 2
|
|
|
|
|
#define SW_FWD 0b10000000 |
|
|
|
|
#define SW_BCK 0b01000000 |
|
|
|
|
|
|
|
|
|
static enum state wheelState = S_11; |
|
|
|
|
|
|
|
|
|
const uint8_t switch_table[28] = { |
|
|
|
|
/* -- S_11 -- */ |
|
|
|
|
S_ILLEGAL, /* 00 ILLEGAL */ |
|
|
|
|
S_FWD_1_01, /* 01 ADVANCE */ |
|
|
|
|
S_BCK_1_10, /* 10 BACK */ |
|
|
|
|
S_11, /* 11 */ |
|
|
|
|
|
|
|
|
|
/* -- S_FWD_1_01 -- */ |
|
|
|
|
S_FWD_2_00, /* 00 ADVANCE */ |
|
|
|
|
S_FWD_1_01, /* 01 */ |
|
|
|
|
S_FWD_3_10, /* 10 ILLEGAL */ |
|
|
|
|
S_11, /* 11 ABORT */ |
|
|
|
|
|
|
|
|
|
/* -- S_FWD_2_00 -- */ |
|
|
|
|
S_FWD_2_00, /* 00 */ |
|
|
|
|
S_FWD_1_01, /* 01 ABORT */ |
|
|
|
|
S_FWD_3_10, /* 10 ADVANCE */ |
|
|
|
|
S_ILLEGAL, /* 11 ILLEGAL */ |
|
|
|
|
|
|
|
|
|
/* -- S_FWD_3_10 -- */ |
|
|
|
|
S_FWD_2_00, /* 00 ABORT */ |
|
|
|
|
S_FWD_1_01, /* 01 ILLEGAL */ |
|
|
|
|
S_FWD_3_10, /* 10 */ |
|
|
|
|
SW_FWD | S_11, /* 11 ADVANCE */ |
|
|
|
|
|
|
|
|
|
/* -- S_BCK_1_10 -- */ |
|
|
|
|
S_BCK_2_00, /* 00 ADVANCE */ |
|
|
|
|
S_BCK_3_01, /* 01 ILLEGAL */ |
|
|
|
|
S_BCK_1_10, /* 10 */ |
|
|
|
|
S_11, /* 11 ABORT */ |
|
|
|
|
|
|
|
|
|
/* -- S_BCK_2_00 -- */ |
|
|
|
|
S_BCK_2_00, /* 00 */ |
|
|
|
|
S_BCK_3_01, /* 01 */ |
|
|
|
|
S_BCK_1_10, /* 10 ABORT */ |
|
|
|
|
S_ILLEGAL, /* 11 */ |
|
|
|
|
|
|
|
|
|
/* -- S_BCK_3_01 -- */ |
|
|
|
|
S_BCK_2_00, /* 00 */ |
|
|
|
|
S_BCK_3_01, /* 01 */ |
|
|
|
|
S_BCK_1_10, /* 10 */ |
|
|
|
|
SW_BCK | S_11, /* 11 */ |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1)); |
|
|
|
|
static uint8_t OldEnc = 0b00; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
static void handle_wheel(void *arg) { |
|
|
|
|
static uint32_t negedge_time = 0; |
|
|
|
|
int dir = 0; |
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
const bool a = 0 != (inputs & (1 << wheelPin1)); // neg = active
|
|
|
|
|
const bool b = 0 != (inputs & (1 << wheelPin2)); |
|
|
|
|
|
|
|
|
|
const uint32_t time = xTaskGetTickCount(); |
|
|
|
|
|
|
|
|
|
if (!a) { |
|
|
|
|
// negedge
|
|
|
|
|
negedge_time = time; |
|
|
|
|
} else { |
|
|
|
|
// posedge
|
|
|
|
|
if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_WHEEL_MS)) { |
|
|
|
|
if (b) { |
|
|
|
|
dir = -1; |
|
|
|
|
} else { |
|
|
|
|
dir = 1; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// reset the timer
|
|
|
|
|
negedge_time = time; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t now = xTaskGetTickCount(); |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
bool a = 0 == (inputs & (1 << wheelPin1)); // neg = active
|
|
|
|
|
bool b = 0 == (inputs & (1 << wheelPin2)); |
|
|
|
|
|
|
|
|
|
bool input[3] = { |
|
|
|
|
(inputs & (1 << pushPin)) == 0, |
|
|
|
|
(inputs & (1 << wheelPin1)) != 0, |
|
|
|
|
(inputs & (1 << wheelPin2)) != 0, |
|
|
|
|
}; |
|
|
|
|
#define ENCODER_POS1 1 |
|
|
|
|
#define ENCODER_POS2 2 |
|
|
|
|
#define ENCODER_POS3 3 |
|
|
|
|
#define ENCODER_POS0 0 |
|
|
|
|
|
|
|
|
|
enum change { |
|
|
|
|
CHG_NONE = 2, |
|
|
|
|
CHG_SET = 1, |
|
|
|
|
CHG_CLEAR = 0, |
|
|
|
|
uint8_t NewEnc = a | (b<<1); |
|
|
|
|
if (NewEnc ^ OldEnc) { // Encoder value changed???
|
|
|
|
|
switch(NewEnc) { |
|
|
|
|
case ENCODER_POS1 : // 01
|
|
|
|
|
if (OldEnc == ENCODER_POS0) // 00
|
|
|
|
|
dir--; |
|
|
|
|
else |
|
|
|
|
dir++; |
|
|
|
|
break; |
|
|
|
|
case ENCODER_POS3 : // 11
|
|
|
|
|
if (OldEnc == ENCODER_POS1) // 01
|
|
|
|
|
dir--; |
|
|
|
|
else |
|
|
|
|
dir++; |
|
|
|
|
break; |
|
|
|
|
case ENCODER_POS2 : // 10
|
|
|
|
|
if (OldEnc == ENCODER_POS3) // 11
|
|
|
|
|
dir--; |
|
|
|
|
else |
|
|
|
|
dir++; |
|
|
|
|
break; |
|
|
|
|
case ENCODER_POS0 : // 00
|
|
|
|
|
if (OldEnc == ENCODER_POS2) // 10
|
|
|
|
|
dir--; |
|
|
|
|
else |
|
|
|
|
dir++; |
|
|
|
|
break; |
|
|
|
|
}; |
|
|
|
|
OldEnc = NewEnc; |
|
|
|
|
}; // end if encoder value changed.
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
enum change changes[3] = {CHG_NONE, CHG_NONE, CHG_NONE}; |
|
|
|
|
#if 0 |
|
|
|
|
// Omron version
|
|
|
|
|
int which = (int)arg; |
|
|
|
|
|
|
|
|
|
for (int i=0; i<3;i++) { |
|
|
|
|
if (input[i] == !state[i].state) { |
|
|
|
|
// Change detected
|
|
|
|
|
if (state[i].start_time == 0) { |
|
|
|
|
// start counting
|
|
|
|
|
state[i].start_time = now; |
|
|
|
|
} |
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
|
|
|
|
|
bool a = 0 == (inputs & (1 << wheelPin1)); // neg = active
|
|
|
|
|
bool b = 0 == (inputs & (1 << wheelPin2)); |
|
|
|
|
|
|
|
|
|
if ((state[i].start_time != 0) && (now - state[i].start_time > state[i].debo_time)) { |
|
|
|
|
state[i].state = !state[i].state; |
|
|
|
|
changes[i] = state[i].state ? CHG_SET : CHG_CLEAR; |
|
|
|
|
if (which == 0) { // itr from A
|
|
|
|
|
if (a) { |
|
|
|
|
if (b) { |
|
|
|
|
dir = -1; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
dir = 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { // itr from B
|
|
|
|
|
if (b) { |
|
|
|
|
if (a) { |
|
|
|
|
dir = 1; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
// returned back to original, reset counter
|
|
|
|
|
state[i].start_time = 0; |
|
|
|
|
dir = -1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
// Naive version
|
|
|
|
|
int which = arg; |
|
|
|
|
|
|
|
|
|
uint32_t signal = 0; |
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
|
|
|
|
|
bool a = 0 == (inputs & (1 << wheelPin1)); // neg = active
|
|
|
|
|
bool b = 0 == (inputs & (1 << wheelPin2)); |
|
|
|
|
|
|
|
|
|
// Wheel logic
|
|
|
|
|
if (changes[1] == CHG_SET) { |
|
|
|
|
if (state[2].state) { |
|
|
|
|
signal |= 0b01; |
|
|
|
|
if (which == 0) { // itr from A
|
|
|
|
|
if (b) { |
|
|
|
|
dir = -1; |
|
|
|
|
} /*else {
|
|
|
|
|
dir = 1; |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
signal |= 0b10; |
|
|
|
|
else { // itr from B
|
|
|
|
|
if (a) { |
|
|
|
|
dir = 1; |
|
|
|
|
} /*else {
|
|
|
|
|
dir = -1; |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
// More complex version
|
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
const uint8_t ab = |
|
|
|
|
(((inputs >> wheelPin2) & 1) << 1) | |
|
|
|
|
((inputs >> wheelPin1) & 1); |
|
|
|
|
|
|
|
|
|
if (wheelState == S_ILLEGAL) { |
|
|
|
|
if (ab != 0b11) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// knob
|
|
|
|
|
if (changes[0] != CHG_NONE) { |
|
|
|
|
signal |= 0b100 << changes[0]; |
|
|
|
|
uint8_t next = switch_table[wheelState*4 + ab]; |
|
|
|
|
|
|
|
|
|
if (next & SW_FWD) dir = 1; |
|
|
|
|
if (next & SW_BCK) dir = -1; |
|
|
|
|
next &= ~(SW_FWD | SW_BCK); |
|
|
|
|
|
|
|
|
|
wheelState = next; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (dir != 0) { |
|
|
|
|
BaseType_t higherWoken = 0; |
|
|
|
|
xTaskNotifyFromISR(hGuiThread, dir == 1 ? 0b10 : 0b01, eSetBits, &higherWoken); |
|
|
|
|
if (higherWoken) portYIELD_FROM_ISR(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// send event to GUI
|
|
|
|
|
if (signal != 0) { |
|
|
|
|
static void handle_pushbtn(void *arg) { |
|
|
|
|
static uint32_t negedge_time = 0; |
|
|
|
|
|
|
|
|
|
const uint32_t inputs = gpio_input_get(); |
|
|
|
|
BaseType_t higherWoken = 0; |
|
|
|
|
xTaskNotifyFromISR(hGuiThread, signal, eSetBits, &higherWoken); |
|
|
|
|
bool pushed = (inputs & (1 << pushPin)) == 0; |
|
|
|
|
|
|
|
|
|
const uint32_t time = xTaskGetTickCount(); |
|
|
|
|
if (pushed) { |
|
|
|
|
negedge_time = time; |
|
|
|
|
} else { |
|
|
|
|
if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_BTN_MS)) { |
|
|
|
|
xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken); |
|
|
|
|
if (higherWoken) portYIELD_FROM_ISR(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|