Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of GEO_COUNTER_L432KC by
RotarySwitch.cpp
00001 /** 00002 * @author Charles Young 00003 * 00004 */ 00005 00006 #include "RotarySwitch.hpp" 00007 00008 // QEI class supports the mode wheel. In its most basic function it will tell 00009 // us which direction the wheel is moving (right or left) so that we can 00010 // use it to select the current mode. 00011 QEI Wheel(D12, D11, NC, 16); // Quadrature encoder 00012 00013 RotarySwitch::RotarySwitch() 00014 { 00015 LED_status_index = 0; 00016 LED_status_reported = LED_status_index; 00017 WheelCurrent = 0; 00018 WheelPrevious = 0; 00019 QEPBpressed = false; 00020 WheelStateTimer = 0; 00021 00022 LEDs_write(0x00); // initialize LEDs (CPM and CNT1 on) 00023 Wheel.reset(); 00024 } 00025 00026 int RotarySwitch::GetPosition(void) 00027 { 00028 return LED_status_reported; 00029 } 00030 00031 //--------------------------------------------------------------------------- 00032 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219 00033 00034 void RotarySwitch::LEDs_write(unsigned short data_val) 00035 { 00036 #define DT 1 // delay time in us for SPI emulation 00037 00038 // Update 74HC595 shift registers 00039 unsigned short mask; 00040 00041 SCK = 0; 00042 wait_us(DT); 00043 CS2 = 0; 00044 00045 for(mask = 0x80; mask!= 0; mask>>= 1) 00046 { 00047 wait_us(DT); 00048 SCK = 0; 00049 if(mask & data_val) 00050 MOSI = 0; 00051 else 00052 MOSI = 1; 00053 wait_us(DT); 00054 SCK = 1; 00055 } 00056 00057 SCK = 0; 00058 wait_us(DT); 00059 CS2 = 1; 00060 00061 return; 00062 } 00063 00064 void RotarySwitch::UpdateOutput() 00065 { 00066 // Blink mode LED 00067 if (WHEEL_SUBMODE_SELECT == currentWheelState) 00068 LEDs_write(0); 00069 00070 if (WheelStateTimer) 00071 { 00072 WheelStateTimer++; 00073 if (++WheelStateTimer > WheelStateTimeout) 00074 { 00075 WheelStateMachine(WHEEL_Timeout); 00076 WheelStateTimer = 0; 00077 } 00078 } 00079 } 00080 00081 float RotarySwitch::UpdateInput() 00082 { 00083 float direction = 0; // used to return direction of rotation 00084 00085 if (WHEEL_INACTIVE == currentWheelState) 00086 LEDs_write(0); 00087 else 00088 LEDs_write(0x01 << LED_status_index); 00089 00090 // react to wheel if in proper state 00091 WheelCurrent = int(Wheel.getPulses()); 00092 if ( (WHEEL_MODE_SELECT == currentWheelState) 00093 || (WHEEL_SUBMODE_SELECT == currentWheelState)) 00094 { 00095 if (WheelCurrent > WheelPrevious) 00096 { 00097 if (WHEEL_MODE_SELECT == currentWheelState) 00098 LED_status_index = ++LED_status_index % LED_status_index_size; 00099 else 00100 direction = 1; 00101 } 00102 else 00103 if (WheelCurrent < WheelPrevious) 00104 { 00105 if (WHEEL_MODE_SELECT == currentWheelState) 00106 LED_status_index = --LED_status_index % LED_status_index_size; 00107 else 00108 direction = -1; 00109 } 00110 00111 // only report back mode once selected 00112 if (WHEEL_SUBMODE_SELECT == currentWheelState) 00113 LED_status_reported = LED_status_index; 00114 00115 // Keep resetting WheelStateTimer as long as wheel is moving 00116 if (WheelPrevious != WheelCurrent) 00117 WheelStateTimer = 1; 00118 } 00119 WheelPrevious = WheelCurrent; 00120 00121 if (WHEEL_INACTIVE == currentWheelState) 00122 // report invalid if not in submode 00123 LED_status_reported = LED_status_index_size; 00124 00125 // detect when wheel button is pressed but wait until it is released 00126 // before doing anything 00127 bool QEPBbutton = QEPB.read(); 00128 if ( (QEPBbutton) 00129 && (!QEPBpressed)) 00130 { 00131 QEPBpressed = true; 00132 } 00133 else 00134 if ( (!QEPBbutton) 00135 && (QEPBpressed)) 00136 { 00137 QEPBpressed = false; 00138 WheelStateMachine(WHEEL_Pressed); 00139 } 00140 00141 return direction; 00142 } 00143 00144 void RotarySwitch::WheelStateMachine(WheelStateEvent event) 00145 { 00146 switch (currentWheelState) { 00147 case WHEEL_INACTIVE: 00148 if (WHEEL_Pressed == event) 00149 { 00150 currentWheelState = WHEEL_MODE_SELECT; 00151 WheelStateTimer = 1; 00152 } 00153 break; 00154 case WHEEL_MODE_SELECT: 00155 if (WHEEL_Pressed == event) 00156 { 00157 currentWheelState = WHEEL_SUBMODE_SELECT; 00158 WheelStateTimer = 1; 00159 } 00160 else 00161 if (WHEEL_Timeout == event) 00162 currentWheelState = WHEEL_INACTIVE; 00163 break; 00164 case WHEEL_SUBMODE_SELECT: 00165 if (WHEEL_Pressed == event) 00166 { 00167 currentWheelState = WHEEL_MODE_SELECT; 00168 WheelStateTimer = 1; 00169 } 00170 else 00171 if (WHEEL_Timeout == event) 00172 currentWheelState = WHEEL_INACTIVE; 00173 break; 00174 default: 00175 break; 00176 } 00177 } 00178
Generated on Sun Jul 24 2022 06:22:38 by
1.7.2
