Charles Young's development fork. Going forward I only want to push mature code to main repository.
Fork of GEO_COUNTER_L432KC by
RotarySwitch.hpp@19:fd3e33641aa7, 2018-09-04 (annotated)
- Committer:
- Charles David Young
- Date:
- Tue Sep 04 08:28:34 2018 -0700
- Revision:
- 19:fd3e33641aa7
- Child:
- 20:fb73eaaf0894
Initial implementation of RotarySwitch. Needs debugging and integration with main.
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 | * This class encapsulates the behavior of the rotary switch to change modes |
Charles David Young |
19:fd3e33641aa7 | 4 | */ |
Charles David Young |
19:fd3e33641aa7 | 5 | |
Charles David Young |
19:fd3e33641aa7 | 6 | #ifndef RotarySwitch_H |
Charles David Young |
19:fd3e33641aa7 | 7 | #define RotarySwitch_H |
Charles David Young |
19:fd3e33641aa7 | 8 | |
Charles David Young |
19:fd3e33641aa7 | 9 | /** |
Charles David Young |
19:fd3e33641aa7 | 10 | * Includes |
Charles David Young |
19:fd3e33641aa7 | 11 | */ |
Charles David Young |
19:fd3e33641aa7 | 12 | #include "mbed.h" |
Charles David Young |
19:fd3e33641aa7 | 13 | #include "QEI.h" // Quadrature Encoder functions |
Charles David Young |
19:fd3e33641aa7 | 14 | |
Charles David Young |
19:fd3e33641aa7 | 15 | /** |
Charles David Young |
19:fd3e33641aa7 | 16 | * Quadrature Encoder Interface. |
Charles David Young |
19:fd3e33641aa7 | 17 | */ |
Charles David Young |
19:fd3e33641aa7 | 18 | class RotarySwitch { |
Charles David Young |
19:fd3e33641aa7 | 19 | |
Charles David Young |
19:fd3e33641aa7 | 20 | public: |
Charles David Young |
19:fd3e33641aa7 | 21 | |
Charles David Young |
19:fd3e33641aa7 | 22 | /** |
Charles David Young |
19:fd3e33641aa7 | 23 | * Constructor. |
Charles David Young |
19:fd3e33641aa7 | 24 | * |
Charles David Young |
19:fd3e33641aa7 | 25 | * @param channelA mbed pin for channel A input. |
Charles David Young |
19:fd3e33641aa7 | 26 | * @param channelB mbed pin for channel B input. |
Charles David Young |
19:fd3e33641aa7 | 27 | */ |
Charles David Young |
19:fd3e33641aa7 | 28 | RotarySwitch(PinName channelA, PinName channelB); |
Charles David Young |
19:fd3e33641aa7 | 29 | |
Charles David Young |
19:fd3e33641aa7 | 30 | /** |
Charles David Young |
19:fd3e33641aa7 | 31 | * Reset the encoder. |
Charles David Young |
19:fd3e33641aa7 | 32 | * |
Charles David Young |
19:fd3e33641aa7 | 33 | * Sets the pulses and revolutions count to zero. |
Charles David Young |
19:fd3e33641aa7 | 34 | */ |
Charles David Young |
19:fd3e33641aa7 | 35 | void GetPosition(void); |
Charles David Young |
19:fd3e33641aa7 | 36 | |
Charles David Young |
19:fd3e33641aa7 | 37 | private: |
Charles David Young |
19:fd3e33641aa7 | 38 | |
Charles David Young |
19:fd3e33641aa7 | 39 | PinName channelA_; |
Charles David Young |
19:fd3e33641aa7 | 40 | PinName channelB_; |
Charles David Young |
19:fd3e33641aa7 | 41 | Ticker SecTenth_Beat; // .1 second ticker |
Charles David Young |
19:fd3e33641aa7 | 42 | Ticker Sec_Beat; // 1 second ticker |
Charles David Young |
19:fd3e33641aa7 | 43 | |
Charles David Young |
19:fd3e33641aa7 | 44 | /** |
Charles David Young |
19:fd3e33641aa7 | 45 | * Defines |
Charles David Young |
19:fd3e33641aa7 | 46 | */ |
Charles David Young |
19:fd3e33641aa7 | 47 | #define CPM 0x01 |
Charles David Young |
19:fd3e33641aa7 | 48 | #define CPS 0x02 |
Charles David Young |
19:fd3e33641aa7 | 49 | #define PLS 0x04 |
Charles David Young |
19:fd3e33641aa7 | 50 | #define VOLTS 0x08 |
Charles David Young |
19:fd3e33641aa7 | 51 | #define CNT1 0x10 |
Charles David Young |
19:fd3e33641aa7 | 52 | #define CNT2 0x20 |
Charles David Young |
19:fd3e33641aa7 | 53 | #define HV 0x40 |
Charles David Young |
19:fd3e33641aa7 | 54 | #define MENU 0x80 |
Charles David Young |
19:fd3e33641aa7 | 55 | uint8_t LED_statuses[] = {CPM, CPS, PLS, VOLTS, CNT1, CNT2, HV, MENU}; |
Charles David Young |
19:fd3e33641aa7 | 56 | uint8_t LED_status = CPM; |
Charles David Young |
19:fd3e33641aa7 | 57 | uint8_t LED_status_index = 0; |
Charles David Young |
19:fd3e33641aa7 | 58 | |
Charles David Young |
19:fd3e33641aa7 | 59 | DigitalIn QEPB (D9); // Quadrature encoder pushbutton |
Charles David Young |
19:fd3e33641aa7 | 60 | |
Charles David Young |
19:fd3e33641aa7 | 61 | // QEI class supports the mode wheel. In its most basic function it will tell |
Charles David Young |
19:fd3e33641aa7 | 62 | // us which direction the wheel is moving (right or left) so that we can |
Charles David Young |
19:fd3e33641aa7 | 63 | // use it to select the current mode. |
Charles David Young |
19:fd3e33641aa7 | 64 | QEI Wheel(D12, D11, NC, 16); // Quadrature encoder |
Charles David Young |
19:fd3e33641aa7 | 65 | int WheelCurrent = 0; |
Charles David Young |
19:fd3e33641aa7 | 66 | int WheelPrevious = 0; |
Charles David Young |
19:fd3e33641aa7 | 67 | bool QEPBpressed = false; // only react to button when pressed |
Charles David Young |
19:fd3e33641aa7 | 68 | int WheelStateTimer = 0; |
Charles David Young |
19:fd3e33641aa7 | 69 | const int WheelStateTimeout = 5; //timeout for wheel button pushed |
Charles David Young |
19:fd3e33641aa7 | 70 | enum WheelState { |
Charles David Young |
19:fd3e33641aa7 | 71 | WHEEL_INACTIVE = 0, |
Charles David Young |
19:fd3e33641aa7 | 72 | WHEEL_MODE_SELECT = 1, |
Charles David Young |
19:fd3e33641aa7 | 73 | WHEEL_SUBMODE_SELECT = 2 |
Charles David Young |
19:fd3e33641aa7 | 74 | }; |
Charles David Young |
19:fd3e33641aa7 | 75 | enum WheelStateEvent { |
Charles David Young |
19:fd3e33641aa7 | 76 | WHEEL_Pressed = 0, |
Charles David Young |
19:fd3e33641aa7 | 77 | WHEEL_Timeout = 1 |
Charles David Young |
19:fd3e33641aa7 | 78 | }; |
Charles David Young |
19:fd3e33641aa7 | 79 | WheelState currentWheelState; |
Charles David Young |
19:fd3e33641aa7 | 80 | |
Charles David Young |
19:fd3e33641aa7 | 81 | /** |
Charles David Young |
19:fd3e33641aa7 | 82 | * Update the pulse count. |
Charles David Young |
19:fd3e33641aa7 | 83 | * |
Charles David Young |
19:fd3e33641aa7 | 84 | * Called on every rising/falling edge of channels A/B. |
Charles David Young |
19:fd3e33641aa7 | 85 | * |
Charles David Young |
19:fd3e33641aa7 | 86 | * Reads the state of the channels and determines whether a pulse forward |
Charles David Young |
19:fd3e33641aa7 | 87 | * or backward has occured, updating the count appropriately. |
Charles David Young |
19:fd3e33641aa7 | 88 | */ |
Charles David Young |
19:fd3e33641aa7 | 89 | void WheelStateMachine(WheelStateEvent event); |
Charles David Young |
19:fd3e33641aa7 | 90 | |
Charles David Young |
19:fd3e33641aa7 | 91 | void RotatySwitch::UpdateOutput(); |
Charles David Young |
19:fd3e33641aa7 | 92 | void RotatySwitch::UpdateInput(); |
Charles David Young |
19:fd3e33641aa7 | 93 | }; |
Charles David Young |
19:fd3e33641aa7 | 94 | |
Charles David Young |
19:fd3e33641aa7 | 95 | #endif /* RotarySwitch_H */ |