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 10:59:42 2018 -0700
Revision:
43:f330ec39b8b4
Parent:
42:204c99cf3fde
debug

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_index = 0;
Charles David Young 38:1642320d83a0 16 LED_status_reported = LED_status_index;
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 38:1642320d83a0 28 return LED_status_reported;
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 38:1642320d83a0 95 if (WheelCurrent > WheelPrevious)
Charles David Young 38:1642320d83a0 96 {
Charles David Young 38:1642320d83a0 97 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 38:1642320d83a0 98 LED_status_index = ++LED_status_index % LED_status_index_size;
Charles David Young 38:1642320d83a0 99 else
Charles David Young 38:1642320d83a0 100 direction = 1;
Charles David Young 38:1642320d83a0 101 }
Charles David Young 38:1642320d83a0 102 else
Charles David Young 38:1642320d83a0 103 if (WheelCurrent < WheelPrevious)
Charles David Young 38:1642320d83a0 104 {
Charles David Young 38:1642320d83a0 105 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 38:1642320d83a0 106 LED_status_index = --LED_status_index % LED_status_index_size;
Charles David Young 38:1642320d83a0 107 else
charlesdavidyoung 24:db7494389c03 108 direction = -1;
Charles David Young 38:1642320d83a0 109 }
Charles David Young 38:1642320d83a0 110
Charles David Young 38:1642320d83a0 111 // only report back mode once selected
Charles David Young 38:1642320d83a0 112 if (WHEEL_SUBMODE_SELECT == currentWheelState)
Charles David Young 38:1642320d83a0 113 LED_status_reported = LED_status_index;
Charles David Young 42:204c99cf3fde 114
Charles David Young 19:fd3e33641aa7 115 // Keep resetting WheelStateTimer as long as wheel is moving
Charles David Young 19:fd3e33641aa7 116 if (WheelPrevious != WheelCurrent)
Charles David Young 19:fd3e33641aa7 117 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 118 }
Charles David Young 19:fd3e33641aa7 119 WheelPrevious = WheelCurrent;
Charles David Young 19:fd3e33641aa7 120
Charles David Young 43:f330ec39b8b4 121 if (WHEEL_INACTIVE == currentWheelState)
Charles David Young 43:f330ec39b8b4 122 // report invalid if not in submode
Charles David Young 43:f330ec39b8b4 123 LED_status_reported = LED_status_index_size;
Charles David Young 43:f330ec39b8b4 124
Charles David Young 19:fd3e33641aa7 125 // detect when wheel button is pressed but wait until it is released
Charles David Young 19:fd3e33641aa7 126 // before doing anything
Charles David Young 19:fd3e33641aa7 127 bool QEPBbutton = QEPB.read();
Charles David Young 19:fd3e33641aa7 128 if ( (QEPBbutton)
Charles David Young 19:fd3e33641aa7 129 && (!QEPBpressed))
Charles David Young 19:fd3e33641aa7 130 {
Charles David Young 19:fd3e33641aa7 131 QEPBpressed = true;
Charles David Young 19:fd3e33641aa7 132 }
Charles David Young 19:fd3e33641aa7 133 else
Charles David Young 19:fd3e33641aa7 134 if ( (!QEPBbutton)
Charles David Young 19:fd3e33641aa7 135 && (QEPBpressed))
Charles David Young 19:fd3e33641aa7 136 {
Charles David Young 19:fd3e33641aa7 137 QEPBpressed = false;
Charles David Young 19:fd3e33641aa7 138 WheelStateMachine(WHEEL_Pressed);
Charles David Young 19:fd3e33641aa7 139 }
Charles David Young 19:fd3e33641aa7 140
charlesdavidyoung 24:db7494389c03 141 return direction;
Charles David Young 19:fd3e33641aa7 142 }
Charles David Young 19:fd3e33641aa7 143
charlesdavidyoung 20:fb73eaaf0894 144 void RotarySwitch::WheelStateMachine(WheelStateEvent event)
Charles David Young 19:fd3e33641aa7 145 {
Charles David Young 19:fd3e33641aa7 146 switch (currentWheelState) {
Charles David Young 19:fd3e33641aa7 147 case WHEEL_INACTIVE:
Charles David Young 19:fd3e33641aa7 148 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 149 {
Charles David Young 19:fd3e33641aa7 150 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 151 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 152 }
Charles David Young 19:fd3e33641aa7 153 break;
Charles David Young 19:fd3e33641aa7 154 case WHEEL_MODE_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_SUBMODE_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 case WHEEL_SUBMODE_SELECT:
Charles David Young 19:fd3e33641aa7 165 if (WHEEL_Pressed == event)
Charles David Young 19:fd3e33641aa7 166 {
Charles David Young 19:fd3e33641aa7 167 currentWheelState = WHEEL_MODE_SELECT;
Charles David Young 19:fd3e33641aa7 168 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 169 }
Charles David Young 19:fd3e33641aa7 170 else
Charles David Young 19:fd3e33641aa7 171 if (WHEEL_Timeout == event)
Charles David Young 19:fd3e33641aa7 172 currentWheelState = WHEEL_INACTIVE;
Charles David Young 19:fd3e33641aa7 173 break;
Charles David Young 19:fd3e33641aa7 174 default:
Charles David Young 19:fd3e33641aa7 175 break;
Charles David Young 19:fd3e33641aa7 176 }
Charles David Young 19:fd3e33641aa7 177 }
Charles David Young 19:fd3e33641aa7 178