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:
Thu Sep 06 05:37:59 2018 -0700
Revision:
34:10550b327e3d
Parent:
26:166dbe6022e8
Child:
36:7fc5487bcc22
Implemented modes switch

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 RotarySwitch::RotarySwitch()
Charles David Young 19:fd3e33641aa7 14 {
charlesdavidyoung 20:fb73eaaf0894 15 LED_status = CPM;
charlesdavidyoung 20:fb73eaaf0894 16 LED_status_index = 0;
charlesdavidyoung 20:fb73eaaf0894 17 WheelCurrent = 0;
charlesdavidyoung 20:fb73eaaf0894 18 WheelPrevious = 0;
charlesdavidyoung 20:fb73eaaf0894 19 QEPBpressed = false;
charlesdavidyoung 20:fb73eaaf0894 20 WheelStateTimer = 0;
charlesdavidyoung 20:fb73eaaf0894 21
charlesdavidyoung 20:fb73eaaf0894 22 LEDs_write(0x00); // initialize LEDs (CPM and CNT1 on)
Charles David Young 19:fd3e33641aa7 23 Wheel.reset();
Charles David Young 19:fd3e33641aa7 24 }
Charles David Young 19:fd3e33641aa7 25
charlesdavidyoung 20:fb73eaaf0894 26 int RotarySwitch::GetPosition(void)
Charles David Young 19:fd3e33641aa7 27 {
Charles David Young 19:fd3e33641aa7 28 return LED_status_index;
Charles David Young 19:fd3e33641aa7 29 }
Charles David Young 19:fd3e33641aa7 30
charlesdavidyoung 20:fb73eaaf0894 31 //---------------------------------------------------------------------------
charlesdavidyoung 20:fb73eaaf0894 32 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
charlesdavidyoung 20:fb73eaaf0894 33
charlesdavidyoung 20:fb73eaaf0894 34 void RotarySwitch::LEDs_write(unsigned short data_val)
charlesdavidyoung 20:fb73eaaf0894 35 {
charlesdavidyoung 20:fb73eaaf0894 36 #define DT 1 // delay time in us for SPI emulation
charlesdavidyoung 20:fb73eaaf0894 37
charlesdavidyoung 20:fb73eaaf0894 38 // Update 74HC595 shift registers
charlesdavidyoung 20:fb73eaaf0894 39 unsigned short mask;
charlesdavidyoung 20:fb73eaaf0894 40
charlesdavidyoung 20:fb73eaaf0894 41 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 42 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 43 CS2 = 0;
charlesdavidyoung 20:fb73eaaf0894 44
charlesdavidyoung 20:fb73eaaf0894 45 for(mask = 0x80; mask!= 0; mask>>= 1)
charlesdavidyoung 20:fb73eaaf0894 46 {
charlesdavidyoung 20:fb73eaaf0894 47 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 48 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 49 if(mask & data_val)
charlesdavidyoung 20:fb73eaaf0894 50 MOSI = 0;
charlesdavidyoung 20:fb73eaaf0894 51 else
charlesdavidyoung 20:fb73eaaf0894 52 MOSI = 1;
charlesdavidyoung 20:fb73eaaf0894 53 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 54 SCK = 1;
charlesdavidyoung 20:fb73eaaf0894 55 }
charlesdavidyoung 20:fb73eaaf0894 56
charlesdavidyoung 20:fb73eaaf0894 57 SCK = 0;
charlesdavidyoung 20:fb73eaaf0894 58 wait_us(DT);
charlesdavidyoung 20:fb73eaaf0894 59 CS2 = 1;
charlesdavidyoung 20:fb73eaaf0894 60
charlesdavidyoung 20:fb73eaaf0894 61 return;
charlesdavidyoung 20:fb73eaaf0894 62 }
charlesdavidyoung 20:fb73eaaf0894 63
charlesdavidyoung 20:fb73eaaf0894 64 void RotarySwitch::UpdateOutput()
Charles David Young 19:fd3e33641aa7 65 {
Charles David Young 19:fd3e33641aa7 66 // Blink mode LED
charlesdavidyoung 24:db7494389c03 67 if (WHEEL_SUBMODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 68 LEDs_write(0);
Charles David Young 19:fd3e33641aa7 69
Charles David Young 19:fd3e33641aa7 70 if (WheelStateTimer)
Charles David Young 19:fd3e33641aa7 71 {
Charles David Young 19:fd3e33641aa7 72 WheelStateTimer++;
Charles David Young 19:fd3e33641aa7 73 if (++WheelStateTimer > WheelStateTimeout)
Charles David Young 19:fd3e33641aa7 74 {
Charles David Young 19:fd3e33641aa7 75 WheelStateMachine(WHEEL_Timeout);
Charles David Young 19:fd3e33641aa7 76 WheelStateTimer = 0;
Charles David Young 19:fd3e33641aa7 77 }
Charles David Young 19:fd3e33641aa7 78 }
Charles David Young 19:fd3e33641aa7 79 }
Charles David Young 19:fd3e33641aa7 80
charlesdavidyoung 24:db7494389c03 81 float RotarySwitch::UpdateInput()
charlesdavidyoung 24:db7494389c03 82 {
charlesdavidyoung 24:db7494389c03 83 float direction = 0; // used to return direction of rotation
charlesdavidyoung 24:db7494389c03 84
charlesdavidyoung 24:db7494389c03 85 if (WHEEL_INACTIVE == currentWheelState)
Charles David Young 23:41260ab0f49d 86 LEDs_write(0);
Charles David Young 23:41260ab0f49d 87 else
Charles David Young 34:10550b327e3d 88 LEDs_write(0x01 << LED_status_index);
Charles David Young 19:fd3e33641aa7 89
Charles David Young 19:fd3e33641aa7 90 // react to wheel if in proper state
Charles David Young 19:fd3e33641aa7 91 WheelCurrent = int(Wheel.getPulses());
Charles David Young 19:fd3e33641aa7 92 if ( (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 93 || (WHEEL_SUBMODE_SELECT == currentWheelState))
Charles David Young 19:fd3e33641aa7 94 {
Charles David Young 19:fd3e33641aa7 95 if (WheelCurrent > WheelPrevious)
charlesdavidyoung 24:db7494389c03 96 {
charlesdavidyoung 26:166dbe6022e8 97 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 34:10550b327e3d 98 LED_status_index = ++LED_status_index % LED_status_index_size;
charlesdavidyoung 24:db7494389c03 99 direction = 1;
charlesdavidyoung 24:db7494389c03 100 }
Charles David Young 19:fd3e33641aa7 101 else
Charles David Young 19:fd3e33641aa7 102 if (WheelCurrent < WheelPrevious)
charlesdavidyoung 24:db7494389c03 103 {
charlesdavidyoung 26:166dbe6022e8 104 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 34:10550b327e3d 105 LED_status_index = --LED_status_index % LED_status_index_size;
charlesdavidyoung 24:db7494389c03 106 direction = -1;
charlesdavidyoung 24:db7494389c03 107 }
charlesdavidyoung 26:166dbe6022e8 108
Charles David Young 19:fd3e33641aa7 109 // Keep resetting WheelStateTimer as long as wheel is moving
Charles David Young 19:fd3e33641aa7 110 if (WheelPrevious != WheelCurrent)
Charles David Young 19:fd3e33641aa7 111 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 112 }
Charles David Young 19:fd3e33641aa7 113 WheelPrevious = WheelCurrent;
Charles David Young 19:fd3e33641aa7 114
Charles David Young 19:fd3e33641aa7 115 // detect when wheel button is pressed but wait until it is released
Charles David Young 19:fd3e33641aa7 116 // before doing anything
Charles David Young 19:fd3e33641aa7 117 bool QEPBbutton = QEPB.read();
Charles David Young 19:fd3e33641aa7 118 if ( (QEPBbutton)
Charles David Young 19:fd3e33641aa7 119 && (!QEPBpressed))
Charles David Young 19:fd3e33641aa7 120 {
Charles David Young 19:fd3e33641aa7 121 QEPBpressed = true;
Charles David Young 19:fd3e33641aa7 122 }
Charles David Young 19:fd3e33641aa7 123 else
Charles David Young 19:fd3e33641aa7 124 if ( (!QEPBbutton)
Charles David Young 19:fd3e33641aa7 125 && (QEPBpressed))
Charles David Young 19:fd3e33641aa7 126 {
Charles David Young 19:fd3e33641aa7 127 QEPBpressed = false;
Charles David Young 19:fd3e33641aa7 128 WheelStateMachine(WHEEL_Pressed);
Charles David Young 19:fd3e33641aa7 129 }
Charles David Young 19:fd3e33641aa7 130
charlesdavidyoung 24:db7494389c03 131 return direction;
Charles David Young 19:fd3e33641aa7 132 }
Charles David Young 19:fd3e33641aa7 133
charlesdavidyoung 20:fb73eaaf0894 134 void RotarySwitch::WheelStateMachine(WheelStateEvent event)
Charles David Young 19:fd3e33641aa7 135 {
Charles David Young 19:fd3e33641aa7 136 switch (currentWheelState) {
Charles David Young 19:fd3e33641aa7 137 case WHEEL_INACTIVE:
Charles David Young 19:fd3e33641aa7 138 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 139 {
Charles David Young 19:fd3e33641aa7 140 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 141 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 142 }
Charles David Young 19:fd3e33641aa7 143 break;
Charles David Young 19:fd3e33641aa7 144 case WHEEL_MODE_SELECT:
Charles David Young 19:fd3e33641aa7 145 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 146 {
Charles David Young 19:fd3e33641aa7 147 currentWheelState = WHEEL_SUBMODE_SELECT;
Charles David Young 19:fd3e33641aa7 148 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 149 }
Charles David Young 19:fd3e33641aa7 150 else
Charles David Young 19:fd3e33641aa7 151 if (WHEEL_Timeout == event)
Charles David Young 19:fd3e33641aa7 152 currentWheelState = WHEEL_INACTIVE;
Charles David Young 19:fd3e33641aa7 153 break;
Charles David Young 19:fd3e33641aa7 154 case WHEEL_SUBMODE_SELECT:
Charles David Young 19:fd3e33641aa7 155 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 156 {
Charles David Young 19:fd3e33641aa7 157 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 158 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 159 }
Charles David Young 19:fd3e33641aa7 160 else
Charles David Young 19:fd3e33641aa7 161 if (WHEEL_Timeout == event)
Charles David Young 19:fd3e33641aa7 162 currentWheelState = WHEEL_INACTIVE;
Charles David Young 19:fd3e33641aa7 163 break;
Charles David Young 19:fd3e33641aa7 164 default:
Charles David Young 19:fd3e33641aa7 165 break;
Charles David Young 19:fd3e33641aa7 166 }
Charles David Young 19:fd3e33641aa7 167 }
Charles David Young 19:fd3e33641aa7 168