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:
charlesdavidyoung
Date:
Wed Sep 05 16:05:07 2018 +0000
Revision:
24:db7494389c03
Parent:
23:41260ab0f49d
Child:
26:166dbe6022e8
Still developing mode selection.  LEDs turn off when not in mode selection.  Pressing once enters mode selection. Pressing again enters submode.  Temporarily the rotary switcha adjusts the display brightness.  This is just for testing.

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
charlesdavidyoung 24:db7494389c03 77 if (WHEEL_SUBMODE_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 24:db7494389c03 91 float RotarySwitch::UpdateInput()
charlesdavidyoung 24:db7494389c03 92 {
charlesdavidyoung 24:db7494389c03 93 float direction = 0; // used to return direction of rotation
charlesdavidyoung 24:db7494389c03 94
charlesdavidyoung 24:db7494389c03 95 if (WHEEL_INACTIVE == currentWheelState)
Charles David Young 23:41260ab0f49d 96 LEDs_write(0);
Charles David Young 23:41260ab0f49d 97 else
Charles David Young 23:41260ab0f49d 98 LEDs_write(LED_statuses[LED_status_index]);
Charles David Young 19:fd3e33641aa7 99
Charles David Young 19:fd3e33641aa7 100 // react to wheel if in proper state
Charles David Young 19:fd3e33641aa7 101 WheelCurrent = int(Wheel.getPulses());
Charles David Young 19:fd3e33641aa7 102 if ( (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 103 || (WHEEL_SUBMODE_SELECT == currentWheelState))
Charles David Young 19:fd3e33641aa7 104 {
Charles David Young 19:fd3e33641aa7 105 if (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 19:fd3e33641aa7 106 {
Charles David Young 19:fd3e33641aa7 107 if (WheelCurrent > WheelPrevious)
charlesdavidyoung 24:db7494389c03 108 {
Charles David Young 19:fd3e33641aa7 109 LED_status_index = ++LED_status_index % sizeof(LED_statuses);
charlesdavidyoung 24:db7494389c03 110 direction = 1;
charlesdavidyoung 24:db7494389c03 111 }
Charles David Young 19:fd3e33641aa7 112 else
Charles David Young 19:fd3e33641aa7 113 if (WheelCurrent < WheelPrevious)
charlesdavidyoung 24:db7494389c03 114 {
Charles David Young 19:fd3e33641aa7 115 LED_status_index = --LED_status_index % sizeof(LED_statuses);
charlesdavidyoung 24:db7494389c03 116 direction = -1;
charlesdavidyoung 24:db7494389c03 117 }
Charles David Young 19:fd3e33641aa7 118 }
Charles David Young 19:fd3e33641aa7 119 // Keep resetting WheelStateTimer as long as wheel is moving
Charles David Young 19:fd3e33641aa7 120 if (WheelPrevious != WheelCurrent)
Charles David Young 19:fd3e33641aa7 121 WheelStateTimer = 1;
Charles David Young 19:fd3e33641aa7 122 }
Charles David Young 19:fd3e33641aa7 123 WheelPrevious = WheelCurrent;
Charles David Young 19:fd3e33641aa7 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