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