Release 1.01
Diff: ButtonController.cpp
- Revision:
- 1:86f6ebbe4fd1
- Parent:
- 0:b6d729ae4f27
- Child:
- 2:1d5204d29bc5
diff -r b6d729ae4f27 -r 86f6ebbe4fd1 ButtonController.cpp --- a/ButtonController.cpp Thu Jul 25 00:43:08 2019 +0000 +++ b/ButtonController.cpp Thu Sep 12 11:27:59 2019 +0000 @@ -1,11 +1,12 @@ #include "ButtonController.h" -ButtonController::ButtonController(Navigator * navigator) +ButtonController::ButtonController() { spi_io_exp = new mcp23s08 (p5, p6, p7, p26, 0x20); + // --------------------------------------- // set pin modes // --------------------------------------- spi_io_exp->gpioPinMode(0,0); @@ -17,6 +18,7 @@ spi_io_exp->gpioPinMode(6,1); spi_io_exp->gpioPinMode(7,1); + // --------------------------------------- // enable the audio amplifier // --------------------------------------- spi_io_exp->gpioPinMode(5,1); @@ -24,7 +26,6 @@ spi_io_exp->gpioDigitalWrite(6,0); spi_io_exp->gpioDigitalWrite(7,0); - this->navigator = navigator; } ButtonController::~ButtonController() @@ -37,40 +38,63 @@ } -void ButtonController::update(int currentValue, Menu * menu) +void ButtonController::processCmdQueue(Menu * activeMenu) { - switch(currentValue) - { - case 1: - menu->pressUp(); - break; - case 2: - menu->pressDown(); - break; - case 3: - menu->pressMode(); - break; - case 4: - menu->pressSet(); - break; - case 5: - break; - default: - /* no action */ - printf("!Button Pressed:%d\n\r",currentValue); - break; - }; + while (!cmd_queue.empty()) { + + uint8_t e; + + cmd_queue.pop(e); + + printf("\n<%02x>", e); + + switch(e) { + case 0x01://up + activeMenu->pressUp(); + break; + + case 0x02://down + activeMenu->pressDown(); + break; + + case 0x04://clear + activeMenu->pressClear(); + break; + + case 0x08://mode + activeMenu->pressMode(); + break; + + case 0x10://set + activeMenu->pressSet(); + break; + default: + break; + } + } } -void ButtonController::update(Menu * menu) + +uint8_t ButtonController::getCurrentState() { + currentValue = spi_io_exp->readGpioPort(); + + return currentValue; +} + + +void ButtonController::update(void) +{ + int trigger_action = 0; - currentValue = spi_io_exp->readGpioPort(); + + currentValue = spi_io_exp->readGpioPort() ; + currentValue = (~currentValue & 0x1F); // clear bit is stuck // if (raising edge | falling edge ) if (currentValue != prevValue ) { prevValue = currentValue; - countsSinceChange = 0; + countsSinceChange = 0; trigger_action = 1; } @@ -82,6 +106,7 @@ if (isHeld != 2) printf("Button Long Held:%d\n\r",currentValue); isHeld = 2; trigger_action = 1; + countsSinceChange = 0; } else if (currentValue != NO_BUTTON && countsSinceChange>=5000) { @@ -96,116 +121,13 @@ } } - if (trigger_action || isHeld){ - switch(currentValue) - { - case BUTTON_UP: - menu->pressUp(); - break; - case BUTTON_DOWN: - menu->pressDown(); - break; - case BUTTON_MODE: - menu->pressMode(); - break; - case BUTTON_SET: - menu->pressSet(); - break; - case BUTTON_CLEAR: - break; - case NO_BUTTON: - break; - default: - break; - } - } -} - -uint8_t ButtonController::getCurrentState() -{ - currentValue = spi_io_exp->readGpioPort(); - return currentValue; -} - -void ButtonController::update(Navigator * navigator) -{ - currentValue = spi_io_exp->readGpioPort(); - - // if (raising edge | falling edge ) - if (currentValue != prevValue ) { - prevValue = currentValue; - countsSinceChange = 0; - switch(currentValue) - { - case BUTTON_UP: - if(!cmd_queue.full()) cmd_queue.push(1); - pressButtonUp(); - break; - case BUTTON_DOWN: - if(!cmd_queue.full()) cmd_queue.push(2); - pressButtonDown(); - break; - case BUTTON_MODE: - if(!cmd_queue.full()) cmd_queue.push(3); - pressButtonMode(); - break; - case BUTTON_SET: - if(!cmd_queue.full()) cmd_queue.push(4); - pressButtonSet(); - break; - case BUTTON_CLEAR: - if(!cmd_queue.full()) cmd_queue.push(5); - pressButtonClear(); - break; - case NO_BUTTON: - if(!cmd_queue.full()) cmd_queue.push(0); - releaseButton(); - break; - default: - /* no action */ - printf("!!!Button Pressed:%d\n\r",currentValue); - break; - } - } - else - { //TODO: play with time for what is short or long hold - countsSinceChange++; - if ( currentValue != NO_BUTTON && countsSinceChange>=100000) - { - if (isHeld != 2) printf("Button Long Held:%d\n\r",currentValue); - isHeld = 2; - } - else if (currentValue != NO_BUTTON && countsSinceChange>=50000) - { - if (isHeld != 1) printf("Button Short Held:%d\n\r",currentValue); - isHeld = 1; - } - else - { - isHeld =0; - } - } - -} - - - - -void ButtonController::update(void) -{ - currentValue = spi_io_exp->readGpioPort() ; - currentValue = (~currentValue & 0x1F); // clear bit is stuck - - // if (raising edge | falling edge ) - if (currentValue != prevValue ) { - prevValue = currentValue; - countsSinceChange = 0; - + if (trigger_action) + { if (currentValue && !cmd_queue.full()) { cmd_queue.push(currentValue); - //printf("!!Button Pressed:%d\n\r",currentValue); } + trigger_action = 0; } } @@ -214,22 +136,16 @@ void ButtonController::pressButtonUp(void) { printf("Up Button Pressed:%d\n\r",currentValue); - if (navigator) - navigator->moveUp(); } void ButtonController::pressButtonDown(void) { printf("Down Button Pressed:%d\n\r",currentValue); - if (navigator) - navigator->moveDown(); } void ButtonController::pressButtonSet(void) { printf("Set Button Pressed:%d\n\r",currentValue); - if (navigator) - navigator->selectMenu(); } void ButtonController::pressButtonMode(void)