Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatSimulatedQuadEnc.h
Go to the documentation of this file.
1#pragma once
3
4#include <stdint.h>
5#include "SerialWombat.h"
6
7 typedef enum {
8 SIMQE_IDLE,
9 SIMQE_1ST_TRANSITION_1ST_PIN_COMPLETED,
10 SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED,
11 SIMQE_2ND_TRANSITION_1ST_PIN_COMPLETED,
12 SIMQE_2ND_TRANSITION_2ND_PIN_COMPLETED
13}SIMQUE_STATE_t;
14
21 class SerialWombatSimulatedQuadEnc
22{
23 public:
24 SerialWombatSimulatedQuadEnc(SerialWombatChip& serialWombat0, SerialWombatChip& serialWombat1, uint8_t _pin0, uint8_t _pin1, bool openDrain, bool doubleTransition): sw0(serialWombat0), sw1(serialWombat1)
25 {
26 pin0State = true;
27 pin1State = true;
28 pin0 = _pin0;
29 pin1 = _pin1;
30 this->openDrain = openDrain;
31 this->doubleTransition = doubleTransition;
32 initialize();
33 }
34
35 void bothPinsHigh()
36 {
37 pin0High();
38 pin1High();
39 }
40 void togglePin0()
41 {
42 if (pin0State)
43 {
44 pin0Low();
45 }
46 else
47 {
48 pin0High();
49 }
50 }
51
52 void togglePin1()
53 {
54 if (pin1State)
55 {
56 pin1Low();
57 }
58 else
59 {
60 pin1High();
61 }
62 }
63 void pin0High()
64 {
65 sw0.digitalWrite(pin0, HIGH);
66 pin0State = true;
67 }
68
69 void pin1High()
70 {
71 sw1.digitalWrite(pin1, HIGH);
72 pin1State = true;
73 }
74
75
76 void pin0Low()
77 {
78 sw0.digitalWrite(pin0, LOW);
79 pin0State = false;
80 }
81
82 void pin1Low()
83 {
84 sw1.digitalWrite(pin1, LOW);
85 pin1State = false;
86 }
87
88 bool service()
89 {
90 if (currentPosition != targetPosition)
91 {
92 switch (state)
93 {
94 case SIMQE_IDLE:
95 {
96 if (currentPosition != targetPosition)
97 {
98 lastTransitionTime_millis = millis();
99 if (currentPosition < targetPosition)
100 {
101 togglePin0();
102 }
103 else
104 {
105 togglePin1();
106 }
107
108 state = SIMQE_1ST_TRANSITION_1ST_PIN_COMPLETED;
109 }
110
111 }
112 break;
113
114 case SIMQE_1ST_TRANSITION_1ST_PIN_COMPLETED:
115 {
116 if (millis() > lastTransitionTime_millis + delayAfterFirstPinFirstTransition_mS)
117 {
118 lastTransitionTime_millis = millis();
119 if (currentPosition < targetPosition)
120 {
121 togglePin1();
122 if (!doubleTransition)
123 {
124 ++currentPosition;
125 }
126 }
127 else
128 {
129 togglePin0();
130 if (!doubleTransition)
131 {
132 --currentPosition;
133 }
134
135 }
136
137
138 state = SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED;
139 }
140
141 }
142 break;
143
144 case SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED:
145 {
146 if (millis() > lastTransitionTime_millis + delayAfterFirstPinSecondTransition_mS)
147 {
148 if (doubleTransition)
149 {
150 lastTransitionTime_millis = millis();
151 if (currentPosition < targetPosition)
152 {
153 togglePin0();
154 }
155 else
156 {
157 togglePin1();
158 }
159
160 state = SIMQE_2ND_TRANSITION_2ND_PIN_COMPLETED;
161 }
162 else
163 {
164 state = SIMQE_IDLE;
165 }
166 }
167
168 }
169 break;
170 case SIMQE_2ND_TRANSITION_1ST_PIN_COMPLETED:
171 {
172 if (millis() > lastTransitionTime_millis + delayAfterSecondPinFirstTransition_mS)
173 {
174 lastTransitionTime_millis = millis();
175 if (currentPosition < targetPosition)
176 {
177 togglePin1();
178 ++currentPosition;
179
180 }
181 else
182 {
183 togglePin0();
184 --currentPosition;
185
186 }
187 state = SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED;
188 }
189
190 }
191 break;
192
193 case SIMQE_2ND_TRANSITION_2ND_PIN_COMPLETED:
194 {
195 if (millis() > lastTransitionTime_millis + delayAfterFirstPinSecondTransition_mS)
196 {
197
198 state = SIMQE_IDLE;
199 }
200
201 }
202 break;
203 }
204 }
205 return state == SIMQE_IDLE;
206 }
207 void initialize()
208 {
209 sw0.pinMode(pin0, OUTPUT, false, openDrain);
210 sw1.pinMode(pin1, OUTPUT, false, openDrain);
211 bothPinsHigh();
212 currentPosition = 0;
213 targetPosition = 0;
214 lastTransitionTime_millis = 0;
215 }
216
217
218 bool doubleTransition = false;
219 SIMQUE_STATE_t state = SIMQE_IDLE;
220
221 int32_t currentPosition = 0;
222 int32_t targetPosition = 0;
223 uint32_t delayAfterFirstPinFirstTransition_mS = 20;
224 uint32_t delayAfterSecondPinFirstTransition_mS = 20;
225 uint32_t delayAfterFirstPinSecondTransition_mS = 20;
226 uint32_t delayAfterSecondPinSecondTransition_mS = 20;
227 bool openDrain = true;
228 uint32_t lastTransitionTime_millis = 0;
229
230 private:
231 bool pin0State = true;
232 bool pin1State = true;
233 SerialWombatChip& sw0;
234 SerialWombatChip& sw1;
235 uint8_t pin0;
236 uint8_t pin1;
237
238
239
240};
SerialWombat.h
SIMQUE_STATE_t
SIMQUE_STATE_t
Definition SerialWombatSimulatedQuadEnc.h:7
SIMQE_2ND_TRANSITION_1ST_PIN_COMPLETED
@ SIMQE_2ND_TRANSITION_1ST_PIN_COMPLETED
Definition SerialWombatSimulatedQuadEnc.h:11
SIMQE_2ND_TRANSITION_2ND_PIN_COMPLETED
@ SIMQE_2ND_TRANSITION_2ND_PIN_COMPLETED
Definition SerialWombatSimulatedQuadEnc.h:12
SIMQE_1ST_TRANSITION_1ST_PIN_COMPLETED
@ SIMQE_1ST_TRANSITION_1ST_PIN_COMPLETED
Definition SerialWombatSimulatedQuadEnc.h:9
SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED
@ SIMQE_1ST_TRANSITION_2ND_PIN_COMPLETED
Definition SerialWombatSimulatedQuadEnc.h:10
SIMQE_IDLE
@ SIMQE_IDLE
Definition SerialWombatSimulatedQuadEnc.h:8
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:315
SerialWombatSimulatedQuadEnc::pin1Low
void pin1Low()
Definition SerialWombatSimulatedQuadEnc.h:82
SerialWombatSimulatedQuadEnc::togglePin1
void togglePin1()
Definition SerialWombatSimulatedQuadEnc.h:52
SerialWombatSimulatedQuadEnc::delayAfterSecondPinSecondTransition_mS
uint32_t delayAfterSecondPinSecondTransition_mS
Definition SerialWombatSimulatedQuadEnc.h:226
SerialWombatSimulatedQuadEnc::pin0Low
void pin0Low()
Definition SerialWombatSimulatedQuadEnc.h:76
SerialWombatSimulatedQuadEnc::bothPinsHigh
void bothPinsHigh()
Definition SerialWombatSimulatedQuadEnc.h:35
SerialWombatSimulatedQuadEnc::SerialWombatSimulatedQuadEnc
SerialWombatSimulatedQuadEnc(SerialWombatChip &serialWombat0, SerialWombatChip &serialWombat1, uint8_t _pin0, uint8_t _pin1, bool openDrain, bool doubleTransition)
Definition SerialWombatSimulatedQuadEnc.h:24
SerialWombatSimulatedQuadEnc::state
SIMQUE_STATE_t state
Definition SerialWombatSimulatedQuadEnc.h:219
SerialWombatSimulatedQuadEnc::initialize
void initialize()
Definition SerialWombatSimulatedQuadEnc.h:207
SerialWombatSimulatedQuadEnc::delayAfterSecondPinFirstTransition_mS
uint32_t delayAfterSecondPinFirstTransition_mS
Definition SerialWombatSimulatedQuadEnc.h:224
SerialWombatSimulatedQuadEnc::lastTransitionTime_millis
uint32_t lastTransitionTime_millis
Definition SerialWombatSimulatedQuadEnc.h:228
SerialWombatSimulatedQuadEnc::currentPosition
int32_t currentPosition
Definition SerialWombatSimulatedQuadEnc.h:221
SerialWombatSimulatedQuadEnc::togglePin0
void togglePin0()
Definition SerialWombatSimulatedQuadEnc.h:40
SerialWombatSimulatedQuadEnc::openDrain
bool openDrain
Definition SerialWombatSimulatedQuadEnc.h:227
SerialWombatSimulatedQuadEnc::delayAfterFirstPinFirstTransition_mS
uint32_t delayAfterFirstPinFirstTransition_mS
Definition SerialWombatSimulatedQuadEnc.h:223
SerialWombatSimulatedQuadEnc::pin1High
void pin1High()
Definition SerialWombatSimulatedQuadEnc.h:69
SerialWombatSimulatedQuadEnc::delayAfterFirstPinSecondTransition_mS
uint32_t delayAfterFirstPinSecondTransition_mS
Definition SerialWombatSimulatedQuadEnc.h:225
SerialWombatSimulatedQuadEnc::targetPosition
int32_t targetPosition
Definition SerialWombatSimulatedQuadEnc.h:222
SerialWombatSimulatedQuadEnc::pin0High
void pin0High()
Definition SerialWombatSimulatedQuadEnc.h:63
SerialWombatSimulatedQuadEnc::service
bool service()
Definition SerialWombatSimulatedQuadEnc.h:88
SerialWombatSimulatedQuadEnc::doubleTransition
bool doubleTransition
Definition SerialWombatSimulatedQuadEnc.h:218