Release 1.01

Committer:
foxbrianr
Date:
Tue Sep 17 13:48:28 2019 +0000
Revision:
2:1d5204d29bc5
Parent:
1:86f6ebbe4fd1
Beta 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foxbrianr 2:1d5204d29bc5 1 /**************************************************************************//**
foxbrianr 2:1d5204d29bc5 2 * @file ButtonController.cpp
foxbrianr 2:1d5204d29bc5 3 * @brief Base class for wrapping the interface with the GPIO Extender.
foxbrianr 2:1d5204d29bc5 4 * @version: V1.0
foxbrianr 2:1d5204d29bc5 5 * @date: 9/17/2019
foxbrianr 2:1d5204d29bc5 6
foxbrianr 2:1d5204d29bc5 7 *
foxbrianr 2:1d5204d29bc5 8 * @note
foxbrianr 2:1d5204d29bc5 9 * Copyright (C) 2019 E3 Design. All rights reserved.
foxbrianr 2:1d5204d29bc5 10 *
foxbrianr 2:1d5204d29bc5 11 * @par
foxbrianr 2:1d5204d29bc5 12 * E3 Designers LLC is supplying this software for use with Cortex-M3 LPC1768
foxbrianr 2:1d5204d29bc5 13 * processor based microcontroller for the ESCM 2000 Monitor and Display.
foxbrianr 2:1d5204d29bc5 14 * *
foxbrianr 2:1d5204d29bc5 15 * @par
foxbrianr 2:1d5204d29bc5 16 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
foxbrianr 2:1d5204d29bc5 17 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
foxbrianr 2:1d5204d29bc5 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
foxbrianr 2:1d5204d29bc5 19 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
foxbrianr 2:1d5204d29bc5 20 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
foxbrianr 2:1d5204d29bc5 21 *
foxbrianr 2:1d5204d29bc5 22 ******************************************************************************/
foxbrianr 0:b6d729ae4f27 23 #include "ButtonController.h"
foxbrianr 0:b6d729ae4f27 24
foxbrianr 0:b6d729ae4f27 25
foxbrianr 2:1d5204d29bc5 26 /******************************************************************************/
foxbrianr 1:86f6ebbe4fd1 27 ButtonController::ButtonController()
foxbrianr 0:b6d729ae4f27 28 {
foxbrianr 0:b6d729ae4f27 29
foxbrianr 0:b6d729ae4f27 30 spi_io_exp = new mcp23s08 (p5, p6, p7, p26, 0x20);
foxbrianr 0:b6d729ae4f27 31
foxbrianr 1:86f6ebbe4fd1 32 // ---------------------------------------
foxbrianr 0:b6d729ae4f27 33 // set pin modes
foxbrianr 0:b6d729ae4f27 34 // ---------------------------------------
foxbrianr 0:b6d729ae4f27 35 spi_io_exp->gpioPinMode(0,0);
foxbrianr 0:b6d729ae4f27 36 spi_io_exp->gpioPinMode(1,0);
foxbrianr 0:b6d729ae4f27 37 spi_io_exp->gpioPinMode(2,0);
foxbrianr 0:b6d729ae4f27 38 spi_io_exp->gpioPinMode(3,0);
foxbrianr 0:b6d729ae4f27 39 spi_io_exp->gpioPinMode(4,0);
foxbrianr 0:b6d729ae4f27 40 spi_io_exp->gpioPinMode(5,1);
foxbrianr 0:b6d729ae4f27 41 spi_io_exp->gpioPinMode(6,1);
foxbrianr 0:b6d729ae4f27 42 spi_io_exp->gpioPinMode(7,1);
foxbrianr 0:b6d729ae4f27 43
foxbrianr 1:86f6ebbe4fd1 44 // ---------------------------------------
foxbrianr 0:b6d729ae4f27 45 // enable the audio amplifier
foxbrianr 0:b6d729ae4f27 46 // ---------------------------------------
foxbrianr 0:b6d729ae4f27 47 spi_io_exp->gpioPinMode(5,1);
foxbrianr 0:b6d729ae4f27 48 spi_io_exp->gpioDigitalWrite(5,1);
foxbrianr 0:b6d729ae4f27 49 spi_io_exp->gpioDigitalWrite(6,0);
foxbrianr 0:b6d729ae4f27 50 spi_io_exp->gpioDigitalWrite(7,0);
foxbrianr 0:b6d729ae4f27 51
foxbrianr 0:b6d729ae4f27 52 }
foxbrianr 0:b6d729ae4f27 53
foxbrianr 2:1d5204d29bc5 54 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 55 ButtonController::~ButtonController()
foxbrianr 0:b6d729ae4f27 56 {
foxbrianr 0:b6d729ae4f27 57
foxbrianr 0:b6d729ae4f27 58 }
foxbrianr 0:b6d729ae4f27 59
foxbrianr 2:1d5204d29bc5 60 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 61 void ButtonController::init(void)
foxbrianr 0:b6d729ae4f27 62 {
foxbrianr 0:b6d729ae4f27 63
foxbrianr 0:b6d729ae4f27 64 }
foxbrianr 0:b6d729ae4f27 65
foxbrianr 2:1d5204d29bc5 66 /******************************************************************************/
foxbrianr 1:86f6ebbe4fd1 67 void ButtonController::processCmdQueue(Menu * activeMenu)
foxbrianr 0:b6d729ae4f27 68 {
foxbrianr 1:86f6ebbe4fd1 69 while (!cmd_queue.empty()) {
foxbrianr 1:86f6ebbe4fd1 70
foxbrianr 1:86f6ebbe4fd1 71 uint8_t e;
foxbrianr 1:86f6ebbe4fd1 72
foxbrianr 1:86f6ebbe4fd1 73 cmd_queue.pop(e);
foxbrianr 1:86f6ebbe4fd1 74
foxbrianr 1:86f6ebbe4fd1 75 printf("\n<%02x>", e);
foxbrianr 1:86f6ebbe4fd1 76
foxbrianr 1:86f6ebbe4fd1 77 switch(e) {
foxbrianr 1:86f6ebbe4fd1 78 case 0x01://up
foxbrianr 1:86f6ebbe4fd1 79 activeMenu->pressUp();
foxbrianr 1:86f6ebbe4fd1 80 break;
foxbrianr 1:86f6ebbe4fd1 81
foxbrianr 1:86f6ebbe4fd1 82 case 0x02://down
foxbrianr 1:86f6ebbe4fd1 83 activeMenu->pressDown();
foxbrianr 1:86f6ebbe4fd1 84 break;
foxbrianr 1:86f6ebbe4fd1 85
foxbrianr 1:86f6ebbe4fd1 86 case 0x04://clear
foxbrianr 1:86f6ebbe4fd1 87 activeMenu->pressClear();
foxbrianr 1:86f6ebbe4fd1 88 break;
foxbrianr 1:86f6ebbe4fd1 89
foxbrianr 1:86f6ebbe4fd1 90 case 0x08://mode
foxbrianr 1:86f6ebbe4fd1 91 activeMenu->pressMode();
foxbrianr 1:86f6ebbe4fd1 92 break;
foxbrianr 1:86f6ebbe4fd1 93
foxbrianr 1:86f6ebbe4fd1 94 case 0x10://set
foxbrianr 1:86f6ebbe4fd1 95 activeMenu->pressSet();
foxbrianr 1:86f6ebbe4fd1 96 break;
foxbrianr 1:86f6ebbe4fd1 97 default:
foxbrianr 1:86f6ebbe4fd1 98 break;
foxbrianr 1:86f6ebbe4fd1 99 }
foxbrianr 1:86f6ebbe4fd1 100 }
foxbrianr 0:b6d729ae4f27 101 }
foxbrianr 0:b6d729ae4f27 102
foxbrianr 1:86f6ebbe4fd1 103
foxbrianr 2:1d5204d29bc5 104 /******************************************************************************/
foxbrianr 1:86f6ebbe4fd1 105 uint8_t ButtonController::getCurrentState()
foxbrianr 0:b6d729ae4f27 106 {
foxbrianr 1:86f6ebbe4fd1 107 currentValue = spi_io_exp->readGpioPort();
foxbrianr 1:86f6ebbe4fd1 108
foxbrianr 1:86f6ebbe4fd1 109 return currentValue;
foxbrianr 1:86f6ebbe4fd1 110 }
foxbrianr 1:86f6ebbe4fd1 111
foxbrianr 1:86f6ebbe4fd1 112
foxbrianr 2:1d5204d29bc5 113 /******************************************************************************/
foxbrianr 1:86f6ebbe4fd1 114 void ButtonController::update(void)
foxbrianr 1:86f6ebbe4fd1 115 {
foxbrianr 1:86f6ebbe4fd1 116
foxbrianr 0:b6d729ae4f27 117 int trigger_action = 0;
foxbrianr 1:86f6ebbe4fd1 118
foxbrianr 1:86f6ebbe4fd1 119 currentValue = spi_io_exp->readGpioPort() ;
foxbrianr 1:86f6ebbe4fd1 120 currentValue = (~currentValue & 0x1F); // clear bit is stuck
foxbrianr 0:b6d729ae4f27 121
foxbrianr 0:b6d729ae4f27 122 // if (raising edge | falling edge )
foxbrianr 0:b6d729ae4f27 123 if (currentValue != prevValue ) {
foxbrianr 0:b6d729ae4f27 124 prevValue = currentValue;
foxbrianr 1:86f6ebbe4fd1 125 countsSinceChange = 0;
foxbrianr 0:b6d729ae4f27 126 trigger_action = 1;
foxbrianr 0:b6d729ae4f27 127
foxbrianr 0:b6d729ae4f27 128 }
foxbrianr 0:b6d729ae4f27 129 else
foxbrianr 0:b6d729ae4f27 130 { //TODO: play with time for what is short or long hold
foxbrianr 0:b6d729ae4f27 131 countsSinceChange++;
foxbrianr 0:b6d729ae4f27 132 if ( currentValue != NO_BUTTON && countsSinceChange>=10000)
foxbrianr 0:b6d729ae4f27 133 {
foxbrianr 0:b6d729ae4f27 134 if (isHeld != 2) printf("Button Long Held:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 135 isHeld = 2;
foxbrianr 0:b6d729ae4f27 136 trigger_action = 1;
foxbrianr 1:86f6ebbe4fd1 137 countsSinceChange = 0;
foxbrianr 0:b6d729ae4f27 138 }
foxbrianr 0:b6d729ae4f27 139 else if (currentValue != NO_BUTTON && countsSinceChange>=5000)
foxbrianr 0:b6d729ae4f27 140 {
foxbrianr 0:b6d729ae4f27 141 if (isHeld != 1) printf("Button Short Held:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 142 isHeld = 1;
foxbrianr 0:b6d729ae4f27 143 trigger_action = 1;
foxbrianr 0:b6d729ae4f27 144 countsSinceChange = 0;
foxbrianr 0:b6d729ae4f27 145 }
foxbrianr 0:b6d729ae4f27 146 else
foxbrianr 0:b6d729ae4f27 147 {
foxbrianr 0:b6d729ae4f27 148 isHeld =0;
foxbrianr 0:b6d729ae4f27 149 }
foxbrianr 0:b6d729ae4f27 150 }
foxbrianr 0:b6d729ae4f27 151
foxbrianr 0:b6d729ae4f27 152
foxbrianr 1:86f6ebbe4fd1 153 if (trigger_action)
foxbrianr 1:86f6ebbe4fd1 154 {
foxbrianr 0:b6d729ae4f27 155 if (currentValue && !cmd_queue.full()) {
foxbrianr 0:b6d729ae4f27 156 cmd_queue.push(currentValue);
foxbrianr 0:b6d729ae4f27 157 }
foxbrianr 1:86f6ebbe4fd1 158 trigger_action = 0;
foxbrianr 0:b6d729ae4f27 159
foxbrianr 0:b6d729ae4f27 160 }
foxbrianr 0:b6d729ae4f27 161 }
foxbrianr 0:b6d729ae4f27 162
foxbrianr 0:b6d729ae4f27 163
foxbrianr 2:1d5204d29bc5 164 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 165 void ButtonController::pressButtonUp(void)
foxbrianr 0:b6d729ae4f27 166 {
foxbrianr 0:b6d729ae4f27 167 printf("Up Button Pressed:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 168 }
foxbrianr 0:b6d729ae4f27 169
foxbrianr 2:1d5204d29bc5 170 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 171 void ButtonController::pressButtonDown(void)
foxbrianr 0:b6d729ae4f27 172 {
foxbrianr 0:b6d729ae4f27 173 printf("Down Button Pressed:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 174 }
foxbrianr 0:b6d729ae4f27 175
foxbrianr 2:1d5204d29bc5 176 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 177 void ButtonController::pressButtonSet(void)
foxbrianr 0:b6d729ae4f27 178 {
foxbrianr 0:b6d729ae4f27 179 printf("Set Button Pressed:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 180 }
foxbrianr 0:b6d729ae4f27 181
foxbrianr 2:1d5204d29bc5 182 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 183 void ButtonController::pressButtonMode(void)
foxbrianr 0:b6d729ae4f27 184 {
foxbrianr 0:b6d729ae4f27 185 printf("Mode Button Pressed:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 186 }
foxbrianr 0:b6d729ae4f27 187
foxbrianr 2:1d5204d29bc5 188 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 189 void ButtonController::pressButtonClear(void)
foxbrianr 0:b6d729ae4f27 190 {
foxbrianr 0:b6d729ae4f27 191 printf("Clear Button Pressed:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 192 }
foxbrianr 0:b6d729ae4f27 193
foxbrianr 2:1d5204d29bc5 194 /******************************************************************************/
foxbrianr 0:b6d729ae4f27 195 void ButtonController::releaseButton(void)
foxbrianr 0:b6d729ae4f27 196 {
foxbrianr 0:b6d729ae4f27 197 //printf("Button Released:%d\n\r",currentValue);
foxbrianr 0:b6d729ae4f27 198 isHeld = 0;
foxbrianr 0:b6d729ae4f27 199 countsSinceChange = 0;
foxbrianr 0:b6d729ae4f27 200 }
foxbrianr 2:1d5204d29bc5 201 /******************************************************************************/