Release 1.01
ButtonController.cpp@1:86f6ebbe4fd1, 2019-09-12 (annotated)
- Committer:
- foxbrianr
- Date:
- Thu Sep 12 11:27:59 2019 +0000
- Revision:
- 1:86f6ebbe4fd1
- Parent:
- 0:b6d729ae4f27
- Child:
- 2:1d5204d29bc5
beta1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
foxbrianr | 0:b6d729ae4f27 | 1 | #include "ButtonController.h" |
foxbrianr | 0:b6d729ae4f27 | 2 | |
foxbrianr | 0:b6d729ae4f27 | 3 | |
foxbrianr | 1:86f6ebbe4fd1 | 4 | ButtonController::ButtonController() |
foxbrianr | 0:b6d729ae4f27 | 5 | { |
foxbrianr | 0:b6d729ae4f27 | 6 | |
foxbrianr | 0:b6d729ae4f27 | 7 | spi_io_exp = new mcp23s08 (p5, p6, p7, p26, 0x20); |
foxbrianr | 0:b6d729ae4f27 | 8 | |
foxbrianr | 1:86f6ebbe4fd1 | 9 | // --------------------------------------- |
foxbrianr | 0:b6d729ae4f27 | 10 | // set pin modes |
foxbrianr | 0:b6d729ae4f27 | 11 | // --------------------------------------- |
foxbrianr | 0:b6d729ae4f27 | 12 | spi_io_exp->gpioPinMode(0,0); |
foxbrianr | 0:b6d729ae4f27 | 13 | spi_io_exp->gpioPinMode(1,0); |
foxbrianr | 0:b6d729ae4f27 | 14 | spi_io_exp->gpioPinMode(2,0); |
foxbrianr | 0:b6d729ae4f27 | 15 | spi_io_exp->gpioPinMode(3,0); |
foxbrianr | 0:b6d729ae4f27 | 16 | spi_io_exp->gpioPinMode(4,0); |
foxbrianr | 0:b6d729ae4f27 | 17 | spi_io_exp->gpioPinMode(5,1); |
foxbrianr | 0:b6d729ae4f27 | 18 | spi_io_exp->gpioPinMode(6,1); |
foxbrianr | 0:b6d729ae4f27 | 19 | spi_io_exp->gpioPinMode(7,1); |
foxbrianr | 0:b6d729ae4f27 | 20 | |
foxbrianr | 1:86f6ebbe4fd1 | 21 | // --------------------------------------- |
foxbrianr | 0:b6d729ae4f27 | 22 | // enable the audio amplifier |
foxbrianr | 0:b6d729ae4f27 | 23 | // --------------------------------------- |
foxbrianr | 0:b6d729ae4f27 | 24 | spi_io_exp->gpioPinMode(5,1); |
foxbrianr | 0:b6d729ae4f27 | 25 | spi_io_exp->gpioDigitalWrite(5,1); |
foxbrianr | 0:b6d729ae4f27 | 26 | spi_io_exp->gpioDigitalWrite(6,0); |
foxbrianr | 0:b6d729ae4f27 | 27 | spi_io_exp->gpioDigitalWrite(7,0); |
foxbrianr | 0:b6d729ae4f27 | 28 | |
foxbrianr | 0:b6d729ae4f27 | 29 | } |
foxbrianr | 0:b6d729ae4f27 | 30 | |
foxbrianr | 0:b6d729ae4f27 | 31 | ButtonController::~ButtonController() |
foxbrianr | 0:b6d729ae4f27 | 32 | { |
foxbrianr | 0:b6d729ae4f27 | 33 | |
foxbrianr | 0:b6d729ae4f27 | 34 | } |
foxbrianr | 0:b6d729ae4f27 | 35 | |
foxbrianr | 0:b6d729ae4f27 | 36 | void ButtonController::init(void) |
foxbrianr | 0:b6d729ae4f27 | 37 | { |
foxbrianr | 0:b6d729ae4f27 | 38 | |
foxbrianr | 0:b6d729ae4f27 | 39 | } |
foxbrianr | 0:b6d729ae4f27 | 40 | |
foxbrianr | 1:86f6ebbe4fd1 | 41 | void ButtonController::processCmdQueue(Menu * activeMenu) |
foxbrianr | 0:b6d729ae4f27 | 42 | { |
foxbrianr | 1:86f6ebbe4fd1 | 43 | while (!cmd_queue.empty()) { |
foxbrianr | 1:86f6ebbe4fd1 | 44 | |
foxbrianr | 1:86f6ebbe4fd1 | 45 | uint8_t e; |
foxbrianr | 1:86f6ebbe4fd1 | 46 | |
foxbrianr | 1:86f6ebbe4fd1 | 47 | cmd_queue.pop(e); |
foxbrianr | 1:86f6ebbe4fd1 | 48 | |
foxbrianr | 1:86f6ebbe4fd1 | 49 | printf("\n<%02x>", e); |
foxbrianr | 1:86f6ebbe4fd1 | 50 | |
foxbrianr | 1:86f6ebbe4fd1 | 51 | switch(e) { |
foxbrianr | 1:86f6ebbe4fd1 | 52 | case 0x01://up |
foxbrianr | 1:86f6ebbe4fd1 | 53 | activeMenu->pressUp(); |
foxbrianr | 1:86f6ebbe4fd1 | 54 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 55 | |
foxbrianr | 1:86f6ebbe4fd1 | 56 | case 0x02://down |
foxbrianr | 1:86f6ebbe4fd1 | 57 | activeMenu->pressDown(); |
foxbrianr | 1:86f6ebbe4fd1 | 58 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 59 | |
foxbrianr | 1:86f6ebbe4fd1 | 60 | case 0x04://clear |
foxbrianr | 1:86f6ebbe4fd1 | 61 | activeMenu->pressClear(); |
foxbrianr | 1:86f6ebbe4fd1 | 62 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 63 | |
foxbrianr | 1:86f6ebbe4fd1 | 64 | case 0x08://mode |
foxbrianr | 1:86f6ebbe4fd1 | 65 | activeMenu->pressMode(); |
foxbrianr | 1:86f6ebbe4fd1 | 66 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 67 | |
foxbrianr | 1:86f6ebbe4fd1 | 68 | case 0x10://set |
foxbrianr | 1:86f6ebbe4fd1 | 69 | activeMenu->pressSet(); |
foxbrianr | 1:86f6ebbe4fd1 | 70 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 71 | default: |
foxbrianr | 1:86f6ebbe4fd1 | 72 | break; |
foxbrianr | 1:86f6ebbe4fd1 | 73 | } |
foxbrianr | 1:86f6ebbe4fd1 | 74 | } |
foxbrianr | 0:b6d729ae4f27 | 75 | } |
foxbrianr | 0:b6d729ae4f27 | 76 | |
foxbrianr | 1:86f6ebbe4fd1 | 77 | |
foxbrianr | 1:86f6ebbe4fd1 | 78 | uint8_t ButtonController::getCurrentState() |
foxbrianr | 0:b6d729ae4f27 | 79 | { |
foxbrianr | 1:86f6ebbe4fd1 | 80 | currentValue = spi_io_exp->readGpioPort(); |
foxbrianr | 1:86f6ebbe4fd1 | 81 | |
foxbrianr | 1:86f6ebbe4fd1 | 82 | return currentValue; |
foxbrianr | 1:86f6ebbe4fd1 | 83 | } |
foxbrianr | 1:86f6ebbe4fd1 | 84 | |
foxbrianr | 1:86f6ebbe4fd1 | 85 | |
foxbrianr | 1:86f6ebbe4fd1 | 86 | void ButtonController::update(void) |
foxbrianr | 1:86f6ebbe4fd1 | 87 | { |
foxbrianr | 1:86f6ebbe4fd1 | 88 | |
foxbrianr | 0:b6d729ae4f27 | 89 | int trigger_action = 0; |
foxbrianr | 1:86f6ebbe4fd1 | 90 | |
foxbrianr | 1:86f6ebbe4fd1 | 91 | currentValue = spi_io_exp->readGpioPort() ; |
foxbrianr | 1:86f6ebbe4fd1 | 92 | currentValue = (~currentValue & 0x1F); // clear bit is stuck |
foxbrianr | 0:b6d729ae4f27 | 93 | |
foxbrianr | 0:b6d729ae4f27 | 94 | // if (raising edge | falling edge ) |
foxbrianr | 0:b6d729ae4f27 | 95 | if (currentValue != prevValue ) { |
foxbrianr | 0:b6d729ae4f27 | 96 | prevValue = currentValue; |
foxbrianr | 1:86f6ebbe4fd1 | 97 | countsSinceChange = 0; |
foxbrianr | 0:b6d729ae4f27 | 98 | trigger_action = 1; |
foxbrianr | 0:b6d729ae4f27 | 99 | |
foxbrianr | 0:b6d729ae4f27 | 100 | } |
foxbrianr | 0:b6d729ae4f27 | 101 | else |
foxbrianr | 0:b6d729ae4f27 | 102 | { //TODO: play with time for what is short or long hold |
foxbrianr | 0:b6d729ae4f27 | 103 | countsSinceChange++; |
foxbrianr | 0:b6d729ae4f27 | 104 | if ( currentValue != NO_BUTTON && countsSinceChange>=10000) |
foxbrianr | 0:b6d729ae4f27 | 105 | { |
foxbrianr | 0:b6d729ae4f27 | 106 | if (isHeld != 2) printf("Button Long Held:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 107 | isHeld = 2; |
foxbrianr | 0:b6d729ae4f27 | 108 | trigger_action = 1; |
foxbrianr | 1:86f6ebbe4fd1 | 109 | countsSinceChange = 0; |
foxbrianr | 0:b6d729ae4f27 | 110 | } |
foxbrianr | 0:b6d729ae4f27 | 111 | else if (currentValue != NO_BUTTON && countsSinceChange>=5000) |
foxbrianr | 0:b6d729ae4f27 | 112 | { |
foxbrianr | 0:b6d729ae4f27 | 113 | if (isHeld != 1) printf("Button Short Held:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 114 | isHeld = 1; |
foxbrianr | 0:b6d729ae4f27 | 115 | trigger_action = 1; |
foxbrianr | 0:b6d729ae4f27 | 116 | countsSinceChange = 0; |
foxbrianr | 0:b6d729ae4f27 | 117 | } |
foxbrianr | 0:b6d729ae4f27 | 118 | else |
foxbrianr | 0:b6d729ae4f27 | 119 | { |
foxbrianr | 0:b6d729ae4f27 | 120 | isHeld =0; |
foxbrianr | 0:b6d729ae4f27 | 121 | } |
foxbrianr | 0:b6d729ae4f27 | 122 | } |
foxbrianr | 0:b6d729ae4f27 | 123 | |
foxbrianr | 0:b6d729ae4f27 | 124 | |
foxbrianr | 1:86f6ebbe4fd1 | 125 | if (trigger_action) |
foxbrianr | 1:86f6ebbe4fd1 | 126 | { |
foxbrianr | 0:b6d729ae4f27 | 127 | if (currentValue && !cmd_queue.full()) { |
foxbrianr | 0:b6d729ae4f27 | 128 | cmd_queue.push(currentValue); |
foxbrianr | 0:b6d729ae4f27 | 129 | } |
foxbrianr | 1:86f6ebbe4fd1 | 130 | trigger_action = 0; |
foxbrianr | 0:b6d729ae4f27 | 131 | |
foxbrianr | 0:b6d729ae4f27 | 132 | } |
foxbrianr | 0:b6d729ae4f27 | 133 | } |
foxbrianr | 0:b6d729ae4f27 | 134 | |
foxbrianr | 0:b6d729ae4f27 | 135 | |
foxbrianr | 0:b6d729ae4f27 | 136 | void ButtonController::pressButtonUp(void) |
foxbrianr | 0:b6d729ae4f27 | 137 | { |
foxbrianr | 0:b6d729ae4f27 | 138 | printf("Up Button Pressed:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 139 | } |
foxbrianr | 0:b6d729ae4f27 | 140 | |
foxbrianr | 0:b6d729ae4f27 | 141 | void ButtonController::pressButtonDown(void) |
foxbrianr | 0:b6d729ae4f27 | 142 | { |
foxbrianr | 0:b6d729ae4f27 | 143 | printf("Down Button Pressed:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 144 | } |
foxbrianr | 0:b6d729ae4f27 | 145 | |
foxbrianr | 0:b6d729ae4f27 | 146 | void ButtonController::pressButtonSet(void) |
foxbrianr | 0:b6d729ae4f27 | 147 | { |
foxbrianr | 0:b6d729ae4f27 | 148 | printf("Set Button Pressed:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 149 | } |
foxbrianr | 0:b6d729ae4f27 | 150 | |
foxbrianr | 0:b6d729ae4f27 | 151 | void ButtonController::pressButtonMode(void) |
foxbrianr | 0:b6d729ae4f27 | 152 | { |
foxbrianr | 0:b6d729ae4f27 | 153 | printf("Mode Button Pressed:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 154 | } |
foxbrianr | 0:b6d729ae4f27 | 155 | |
foxbrianr | 0:b6d729ae4f27 | 156 | void ButtonController::pressButtonClear(void) |
foxbrianr | 0:b6d729ae4f27 | 157 | { |
foxbrianr | 0:b6d729ae4f27 | 158 | printf("Clear Button Pressed:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 159 | } |
foxbrianr | 0:b6d729ae4f27 | 160 | |
foxbrianr | 0:b6d729ae4f27 | 161 | void ButtonController::releaseButton(void) |
foxbrianr | 0:b6d729ae4f27 | 162 | { |
foxbrianr | 0:b6d729ae4f27 | 163 | //printf("Button Released:%d\n\r",currentValue); |
foxbrianr | 0:b6d729ae4f27 | 164 | isHeld = 0; |
foxbrianr | 0:b6d729ae4f27 | 165 | countsSinceChange = 0; |
foxbrianr | 0:b6d729ae4f27 | 166 | } |