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