Charles Young's development fork. Going forward I only want to push mature code to main repository.

Dependencies:   mbed

Fork of GEO_COUNTER_L432KC by Geo Electronics "Geo Counter"

Committer:
Charles David Young
Date:
Tue Sep 04 17:51:33 2018 -0700
Revision:
23:41260ab0f49d
Parent:
20:fb73eaaf0894
Child:
24:db7494389c03
Turn off mode LEDs if not selecting mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charles David Young 19:fd3e33641aa7 1 /**
Charles David Young 19:fd3e33641aa7 2 * @author Charles Young
Charles David Young 19:fd3e33641aa7 3 *
Charles David Young 19:fd3e33641aa7 4 */
Charles David Young 19:fd3e33641aa7 5
charlesdavidyoung 20:fb73eaaf0894 6 #include "RotarySwitch.hpp"
charlesdavidyoung 20:fb73eaaf0894 7
charlesdavidyoung 20:fb73eaaf0894 8 // QEI class supports the mode wheel. In its most basic function it will tell
charlesdavidyoung 20:fb73eaaf0894 9 // us which direction the wheel is moving (right or left) so that we can
charlesdavidyoung 20:fb73eaaf0894 10 // use it to select the current mode.
charlesdavidyoung 20:fb73eaaf0894 11 QEI Wheel(D12, D11, NC, 16); // Quadrature encoder
Charles David Young 19:fd3e33641aa7 12
charlesdavidyoung 20:fb73eaaf0894 13 #define CPM 0x01
charlesdavidyoung 20:fb73eaaf0894 14 #define CPS 0x02
charlesdavidyoung 20:fb73eaaf0894 15 #define PLS 0x04
charlesdavidyoung 20:fb73eaaf0894 16 #define VOLTS 0x08
charlesdavidyoung 20:fb73eaaf0894 17 #define CNT1 0x10
charlesdavidyoung 20:fb73eaaf0894 18 #define CNT2 0x20
charlesdavidyoung 20:fb73eaaf0894 19 #define HV 0x40
charlesdavidyoung 20:fb73eaaf0894 20 #define MENU 0x80
charlesdavidyoung 20:fb73eaaf0894 21 uint8_t LED_statuses[] = {CPM, CPS, PLS, VOLTS, CNT1, CNT2, HV, MENU};
charlesdavidyoung 20:fb73eaaf0894 22
charlesdavidyoung 20:fb73eaaf0894 23 RotarySwitch::RotarySwitch()
Charles David Young 19:fd3e33641aa7 24 {
charlesdavidyoung 20:fb73eaaf0894 25 LED_status = CPM;
charlesdavidyoung 20:fb73eaaf0894 26 LED_status_index = 0;
charlesdavidyoung 20:fb73eaaf0894 27 WheelCurrent = 0;
charlesdavidyoung 20:fb73eaaf0894 28 WheelPrevious = 0;
charlesdavidyoung 20:fb73eaaf0894 29 QEPBpressed = false;
charlesdavidyoung 20:fb73eaaf0894 30 WheelStateTimer = 0;
charlesdavidyoung 20:fb73eaaf0894 31
charlesdavidyoung 20:fb73eaaf0894 32 LEDs_write(0x00); // initialize LEDs (CPM and CNT1 on)
Charles David Young 19:fd3e33641aa7 33 Wheel.reset();
Charles David Young 19:fd3e33641aa7 34 }
Charles David Young 19:fd3e33641aa7 35
charlesdavidyoung 20:fb73eaaf0894 36 int RotarySwitch::GetPosition(void)
Charles David Young 19:fd3e33641aa7 37 {
Charles David Young 19:fd3e33641aa7 38 return LED_status_index;
Charles David Young 19:fd3e33641aa7 39 }
Charles David Young 19:fd3e33641aa7 40
charlesdavidyoung 20:fb73eaaf0894 41 //---------------------------------------------------------------------------
charlesdavidyoung 20:fb73eaaf0894 42 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
charlesdavidyoung 20:fb73eaaf0894 43
charlesdavidyoung 20:fb73eaaf0894 44 void RotarySwitch::LEDs_write(unsigned short data_val)
charlesdavidyoung 20:fb73eaaf0894 45 {
charlesdavidyoung 20:fb73eaaf0894 46 #define DT 1 // delay time in us for SPI emulation
charlesdavidyoung 20:fb73eaaf0894 47
charlesdavidyoung 20:fb73eaaf0894 48 // Update 74HC595 shift registers
charlesdavidyoung 20:fb73eaaf0894 49 unsigned short mask;
charlesdavidyoung 20:fb73eaaf0894 50
charlesdavidyoung 20:fb73eaaf0894 51 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 52 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 53 CS2 = 0;
charlesdavidyoung 20:fb73eaaf0894 54
charlesdavidyoung 20:fb73eaaf0894 55 for(mask = 0x80; mask!= 0; mask>>= 1)
charlesdavidyoung 20:fb73eaaf0894 56 {
charlesdavidyoung 20:fb73eaaf0894 57 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 58 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 59 if(mask & data_val)
charlesdavidyoung 20:fb73eaaf0894 60 MOSI = 0;
charlesdavidyoung 20:fb73eaaf0894 61 else
charlesdavidyoung 20:fb73eaaf0894 62 MOSI = 1;
charlesdavidyoung 20:fb73eaaf0894 63 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 64 SCK = 1;
charlesdavidyoung 20:fb73eaaf0894 65 }
charlesdavidyoung 20:fb73eaaf0894 66
charlesdavidyoung 20:fb73eaaf0894 67 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 68 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 69 CS2 = 1;
charlesdavidyoung 20:fb73eaaf0894 70
charlesdavidyoung 20:fb73eaaf0894 71 return;
charlesdavidyoung 20:fb73eaaf0894 72 }
charlesdavidyoung 20:fb73eaaf0894 73
charlesdavidyoung 20:fb73eaaf0894 74 void RotarySwitch::UpdateOutput()
Charles David Young 19:fd3e33641aa7 75 {
Charles David Young 19:fd3e33641aa7 76 // Blink mode LED
Charles David Young 19:fd3e33641aa7 77 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 78 LEDs_write(0);
Charles David Young 19:fd3e33641aa7 79
Charles David Young 19:fd3e33641aa7 80 if (WheelStateTimer)
Charles David Young 19:fd3e33641aa7 81 {
Charles David Young 19:fd3e33641aa7 82 WheelStateTimer++;
Charles David Young 19:fd3e33641aa7 83 if (++WheelStateTimer > WheelStateTimeout)
Charles David Young 19:fd3e33641aa7 84 {
Charles David Young 19:fd3e33641aa7 85 WheelStateMachine(WHEEL_Timeout);
Charles David Young 19:fd3e33641aa7 86 WheelStateTimer = 0;
Charles David Young 19:fd3e33641aa7 87 }
Charles David Young 19:fd3e33641aa7 88 }
Charles David Young 19:fd3e33641aa7 89 }
Charles David Young 19:fd3e33641aa7 90
charlesdavidyoung 20:fb73eaaf0894 91 void RotarySwitch::UpdateInput()
Charles David Young 19:fd3e33641aa7 92 {
Charles David Young 23:41260ab0f49d 93 if (WHEEL_INACTIVE_SELECT == currentWheelState)
Charles David Young 23:41260ab0f49d 94 LEDs_write(0);
Charles David Young 23:41260ab0f49d 95 else
Charles David Young 23:41260ab0f49d 96 LEDs_write(LED_statuses[LED_status_index]);
Charles David Young 19:fd3e33641aa7 97
Charles David Young 19:fd3e33641aa7 98 // react to wheel if in proper state
Charles David Young 19:fd3e33641aa7 99 WheelCurrent = int(Wheel.getPulses());
Charles David Young 19:fd3e33641aa7 100 if ( (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 101 || (WHEEL_SUBMODE_SELECT == currentWheelState))
Charles David Young 19:fd3e33641aa7 102 {
Charles David Young 19:fd3e33641aa7 103 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 104 {
Charles David Young 19:fd3e33641aa7 105 if (WheelCurrent > WheelPrevious)
Charles David Young 19:fd3e33641aa7 106 LED_status_index = ++LED_status_index % sizeof(LED_statuses);
Charles David Young 19:fd3e33641aa7 107 else
Charles David Young 19:fd3e33641aa7 108 if (WheelCurrent < WheelPrevious)
Charles David Young 19:fd3e33641aa7 109 LED_status_index = --LED_status_index % sizeof(LED_statuses);
Charles David Young 19:fd3e33641aa7 110 }
Charles David Young 19:fd3e33641aa7 111 // Keep resetting WheelStateTimer as long as wheel is moving
Charles David Young 19:fd3e33641aa7 112 if (WheelPrevious != WheelCurrent)
Charles David Young 19:fd3e33641aa7 113 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 114 }
Charles David Young 19:fd3e33641aa7 115 WheelPrevious = WheelCurrent;
Charles David Young 19:fd3e33641aa7 116
Charles David Young 19:fd3e33641aa7 117 // detect when wheel button is pressed but wait until it is released
Charles David Young 19:fd3e33641aa7 118 // before doing anything
Charles David Young 19:fd3e33641aa7 119 bool QEPBbutton = QEPB.read();
Charles David Young 19:fd3e33641aa7 120 if ( (QEPBbutton)
Charles David Young 19:fd3e33641aa7 121 && (!QEPBpressed))
Charles David Young 19:fd3e33641aa7 122 {
Charles David Young 19:fd3e33641aa7 123 QEPBpressed = true;
Charles David Young 19:fd3e33641aa7 124 }
Charles David Young 19:fd3e33641aa7 125 else
Charles David Young 19:fd3e33641aa7 126 if ( (!QEPBbutton)
Charles David Young 19:fd3e33641aa7 127 && (QEPBpressed))
Charles David Young 19:fd3e33641aa7 128 {
Charles David Young 19:fd3e33641aa7 129 QEPBpressed = false;
Charles David Young 19:fd3e33641aa7 130 WheelStateMachine(WHEEL_Pressed);
Charles David Young 19:fd3e33641aa7 131 }
Charles David Young 19:fd3e33641aa7 132
Charles David Young 19:fd3e33641aa7 133 return;
Charles David Young 19:fd3e33641aa7 134 }
Charles David Young 19:fd3e33641aa7 135
charlesdavidyoung 20:fb73eaaf0894 136 void RotarySwitch::WheelStateMachine(WheelStateEvent event)
Charles David Young 19:fd3e33641aa7 137 {
Charles David Young 19:fd3e33641aa7 138 switch (currentWheelState) {
Charles David Young 19:fd3e33641aa7 139 case WHEEL_INACTIVE:
Charles David Young 19:fd3e33641aa7 140 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 141 {
Charles David Young 19:fd3e33641aa7 142 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 143 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 144 }
Charles David Young 19:fd3e33641aa7 145 break;
Charles David Young 19:fd3e33641aa7 146 case WHEEL_MODE_SELECT:
Charles David Young 19:fd3e33641aa7 147 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 148 {
Charles David Young 19:fd3e33641aa7 149 currentWheelState = WHEEL_SUBMODE_SELECT;
Charles David Young 19:fd3e33641aa7 150 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 151 }
Charles David Young 19:fd3e33641aa7 152 else
Charles David Young 19:fd3e33641aa7 153 if (WHEEL_Timeout == event)
Charles David Young 19:fd3e33641aa7 154 currentWheelState = WHEEL_INACTIVE;
Charles David Young 19:fd3e33641aa7 155 break;
Charles David Young 19:fd3e33641aa7 156 case WHEEL_SUBMODE_SELECT:
Charles David Young 19:fd3e33641aa7 157 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 158 {
Charles David Young 19:fd3e33641aa7 159 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 160 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 161 }
Charles David Young 19:fd3e33641aa7 162 else
Charles David Young 19:fd3e33641aa7 163 if (WHEEL_Timeout == event)
Charles David Young 19:fd3e33641aa7 164 currentWheelState = WHEEL_INACTIVE;
Charles David Young 19:fd3e33641aa7 165 break;
Charles David Young 19:fd3e33641aa7 166 default:
Charles David Young 19:fd3e33641aa7 167 break;
Charles David Young 19:fd3e33641aa7 168 }
Charles David Young 19:fd3e33641aa7 169 }
Charles David Young 19:fd3e33641aa7 170