ESP_IOT v2.5
IOT ESP Coding
StepperModule.cpp
Go to the documentation of this file.
1/**
2* \link StepperModule
3*/
4
5#include "StepperModule.h"
6
7
8//! 3.16.25 using a class version (thisi is for ESP_32 and ESP_M5 now)
9
10#include "PTStepperClass.h"
11#include "DCMotorStepperClass.h"
13//! 3.24.25 warm raining day
14#include "HDriverStepperClass.h"
15//! 3.31..25
17//! 5.18.25 45 years Mt St Hellens Eruption
19
20//! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
22
23
24//! instances of the PTStepeprClass
25//! 1
27//! 2
29//! 3
31//! 4
32//! 3.31.25 try Servo ..
34//! 5
35//! 5.18.25 45 years Mt St Hellens Eruption
37//! 6
38//! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
40
41//! MAKE SURE THIS IS UPDATED...
42#define NUM_STEPPER_CLASS 6
43
44//! use this one...
46
47//! 3.31.25 create array of plugs
49
51{
52 int whichStepperClasssIndex = 0;
53 SerialDebug.println("setup_StepperClasses");
54
55 //! instances of the PTStepeprClass
56 //! NOTE: These are the "identity' so that is also the "message" sent around. So SELF defining.
57 _DCMotorStepperClass = new DCMotorStepperClass((char*)"DCMotorStepperClass");
58 _motorSteppers[whichStepperClasssIndex++] = _DCMotorStepperClass;
59
60 _PTStepperClass = new PTStepperClass((char*)"PTStepperClass");
61 _motorSteppers[whichStepperClasssIndex++] = _PTStepperClass;
62
63 _HDriverStepperClass = new HDriverStepperClass((char*)"HDriverStepperClass");
64 _motorSteppers[whichStepperClasssIndex++] = _HDriverStepperClass;
65
66 //! 3.31.25 try Servo ..
67 _SG90ServoStepperClass = new SG90ServoStepperClass((char*)"SG90ServoStepperClass");
68 _motorSteppers[whichStepperClasssIndex++] = _SG90ServoStepperClass;
69
70 //! 5.18.25 45 years Mt St Hellens Eruption
71 _L9110S_DCStepperClass = new L9110S_DCStepperClass((char*)"L9110S_DCStepperClass");
72 _motorSteppers[whichStepperClasssIndex++] = _L9110S_DCStepperClass;
73
74 //! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
75 _ULN2003_StepperClass = new ULN2003_StepperClass((char*)"ULN2003_StepperClass");
76 _motorSteppers[whichStepperClasssIndex++] = _ULN2003_StepperClass;
77
78 //! just check if we haven't goofed with NUM_STEPPER_CLASS not being the actual number whichStepperClasssIndex
79 if (whichStepperClasssIndex != NUM_STEPPER_CLASS)
80 {
81 SerialError.printf("stepperIndex %d not %d\n", whichStepperClasssIndex, NUM_STEPPER_CLASS);
82 }
83 //!TODO: create an array of plug options .. then call their "identity" method
84 SerialDebug.println("Done setup_StepperClasses");
85}
86
87//! called to set a preference (which will be an identifier and a string, which can be converted to a number or boolean)
88void stepperModule_savePreference(int preferenceID, String preferenceValue);
89
90/*feederState*/
91#define FEED_STOPPED 0
92#define SINGLE_FEED 1
93#define AUTO_FEED 2
94#define JACKPOT_FEED 3
95
97int _feedsPerJackpot = 3; //this will be set by user at some point
98int _feedCount = _feedsPerJackpot; //init to max feeds per Jackpot
99
100#define FEED_INTERVAL 1000 // make this a variable determined by user input and stored in JSON
101unsigned long feedPreviousMillis = 0;
102
103//proteced
104// someone else the "savePreferenceBoolean_mainModule(PREFERENCE_STEPPER_BUZZER_VALUE) knows where to save it..
105
106//!returns the buzzer status
108{
110 return buzzStatus;
111}
112
113//! retreives the feeder type (versus grabbing a global variable)
115{
117}
118
119//!called on setup()
121{
122
124 //! 3.28.24 feed on startup..
125#ifdef FEED_ON_STARTUP
127#endif
128
129 //! 3.16.25 use the class ..
131
132 //! NOTE: this is only re-evaluated on reboot .. for now
133 //! 3.16.25 for now this is compile option ..
134 //! but no reason the same build cannot support it..
135 //! sensor kind (see the M5AtomSensor .. as example)
137 SerialDebug.printf("sensorPlug = %s\n", sensorPlug);
138 _whichMotorStepper = NULL;
139
140 //! find it dynamically
141 //! 3.31.25
142 for (int i=0; i< NUM_STEPPER_CLASS; i++)
143 {
144 if (!_motorSteppers[i])
145 {
146 SerialDebug.printf("sensorPlug[%d] == null\n", i);
147 continue;
148 }
149 if (strcmp(sensorPlug,_motorSteppers[i]->classIdentity())==0)
150 {
152 SerialDebug.printf("Match motorSteppers[%d] = %s\n", i, sensorPlug);
153 break;
154 }
155 }
156 //! if nothing found .. use default
158 {
159#ifdef ESP_32
160 //! DEFAULT .. if (strcmp(sensorPlug,"PTStepperClass")==0)
161 //! use this one...
163#else
164 //! the motorStepper can be nil
165 //_whichMotorStepper = NULL;
166 //! 5.2.25 default to HDriver..
167 //_whichMotorStepper = _HDriverStepperClass;
168
169 //! 7.31.25 use default..
171#endif
172 }
173
174#define SETUP_FIRST_TIME
175#ifdef SETUP_FIRST_TIME
178#endif
179
180}
181//! 4.1.25 April Fools day. Mt Peak hike (162 bmp strange spike)
182//! get the identity of the SenasorPlug
183//! nil if non
185{
186 char *identity = NULL;
188 {
189 identity = _whichMotorStepper->classIdentity();
190 }
191 return identity;
192}
193//TODO: this isn't directly called.. but called from the main processsClientCommand(String cmd)
194////Keep ProcessClientCmd short to let the callback run. instead change the feeder state flag
196 SerialLots.printf("***** ProcessClientCmd(%c) from client*****\n", cmd);
197
198 if ((cmd == 0x00) || (cmd == 's') || (cmd == 'c'))
199 {
200 SerialLots.println("Setting FeedState = SINGLE_FEED");
201 _feedState = SINGLE_FEED; // Gen 3 Pet Tutor used 0x00 to feed so 0x00 is to make it backward compatible 's' is the new version
202 //not used .. for awhile.. So let's stop writing to EPROM all the time.
203 }
204 else if (cmd == 'a')
205 {
206 SerialLots.println("Setting FeedState = AUTO_FEED");
208
209 }
210 else if (cmd == 'j')
211 {
212 SerialLots.println("Setting FeedState = JACKPOT_FEED");
214
215 }
216 else if (cmd == 'B')
217 {
218 SerialLots.println("1.Setting buzzStatus = BUZZON");
219
220 //set the flag, then buz.. (or don't care, and the value is stored in mainModule.. TODO: maybe that should cache the result)
221#ifdef USE_UI_MODULE
222 //make a buzz sound..
225#endif
226
227 }
228 else if (cmd == 'b')
229 {
230 SerialLots.println("2.Setting buzzStatus = BUZZOFF");
231#ifdef USE_UI_MODULE
233#endif
234 }
235
236 //!NOTE: There are other commands, but these are only the ones that require a action.
237 //!TODO: make these "events" on the setting of those values. eg. register for stepperValue true or false, etc.. They would be invoked from the 'savePreferencesBoolean' call.
238}
239//void FeederStateMachine() {
240
241//!the main loop fro the StepperModule. This used be called FeederStateMachine()
243{
244 //! 3.24.25 warm rain all day.. no ski
245 //! call the loop (which might have a timer to stop a motor)
248 // FeedState is NO LONGER public and can be set by BLE client via the GATT for the characteristic
249
250 switch (_feedState)
251 {
252 case SINGLE_FEED:
253 {
254#ifdef USE_UI_MODULE
256#endif
257 SerialDebug.println("SINGLE Feed"); //this should write a 0x01 for feed ack
258
261
262 _feedState = FEED_STOPPED; //this will cancel Continous feed if it is set
263
264 //! 5.3.24 Issue #332 if TUMBLER .. reverse direction
266 {
267 //! 8.2.24 have the auto a mode as well.. for older Tumblers. Default is ON (so we can change the old ones),
268 //! message: {"set":"autoMotorDirection","val":"true"}
270 if (autoMotorDirection)
271 {
272 // reverse direction, the Q command
273 //! note: reboot not needed as the next time a feed happens, it reads this value
274 // motor direction == (reverse)
276 currentDirection = !currentDirection;
278 }
279 }
280
281 break;
282 }
283
284 //NOT USED..
285 case JACKPOT_FEED: //note: the only difference in this case is the FEED_STOPPED does NOT get set
286 {
287 if (_feedCount > 0) {
288 unsigned long feedCurrentMillis = millis();
289 if (feedCurrentMillis - feedPreviousMillis >= FEED_INTERVAL) { // changed this to check a timer to see when to fire the feeder
290#ifdef USE_UI_MODULE
291 blinkLED_UIModule(); // Moved 021622 WJL
292#endif
293
296
297 SerialDebug.println("JACKPOT Feed");
298 _feedCount--;
299 feedPreviousMillis = feedCurrentMillis;
300 }
301 }
302 else {
303 _feedCount = _feedsPerJackpot; //reset counter after all feeds for Jackpot completed
305 }
306 break;
307 }
308 case FEED_STOPPED:
309 {
310 //SerialDebug.println("Feed Stopped -- ");
311 //do nothing
312 break;
313 }
314 default:
315 {
317 break;
318 }
319 }
320}
321
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:361
void savePreferenceBoolean_mainModule(int preferenceID, boolean flag)
save a boolean preference
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
char * getPreference_mainModule(int preferenceID)
#define PREFERENCE_SENSOR_PLUGS_SETTING
#define PREFERENCE_STEPPER_BUZZER_VALUE
stepper preferences
#define STEPPER_IS_TUMBLER
#define PREFERENCE_STEPPER_AUTO_MOTOR_DIRECTION_SETTING
#define PREFERENCE_STEPPER_CLOCKWISE_MOTOR_DIRECTION_SETTING
#define AUTO_FEED
void setup_StepperClasses()
#define NUM_STEPPER_CLASS
MAKE SURE THIS IS UPDATED...
void loop_StepperModule()
the main loop fro the StepperModule. This used be called FeederStateMachine()
SG90ServoStepperClass * _SG90ServoStepperClass
char * stepperIdentity_StepperModule()
ULN2003_StepperClass * _ULN2003_StepperClass
#define FEED_STOPPED
MotorStepperClassType * _motorSteppers[NUM_STEPPER_CLASS]
3.31.25 create array of plugs
unsigned long feedPreviousMillis
#define JACKPOT_FEED
void setup_StepperModule()
called on setup()
#define SINGLE_FEED
int getFeederType()
retreives the feeder type (versus grabbing a global variable)
void stepperModule_ProcessClientCmdFinal(char cmd)
the Blink the LED - and it will use the latest BUZZER status (so MQTT could set buzzer on....
int _feedsPerJackpot
HDriverStepperClass * _HDriverStepperClass
3
int _feedCount
void stepperModule_savePreference(int preferenceID, String preferenceValue)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define FEED_INTERVAL
MotorStepperClassType * _whichMotorStepper
use this one...
int _feedState
L9110S_DCStepperClass * _L9110S_DCStepperClass
boolean getBuzzStatus_StepperModule()
returns the buzzer status
DCMotorStepperClass * _DCMotorStepperClass
3.16.25 using a class version (thisi is for ESP_32 and ESP_M5 now)
PTStepperClass * _PTStepperClass
2
unsigned long millis()
Definition: TinyGPS.cpp:35
void setBuzzerLight_UIModule(boolean onFlag)
UI specific actions.
Definition: UI.cpp:17
void blinkLED_UIModule()
blink the LED
Definition: UI.cpp:23
An concrete class.
An concrete class.
An mostly virtual class.
virtual void loop_MotorStepper()=0
loop the PTStepper (so timer can run)
virtual void setup_MotorStepper()=0
setup the PTStepper
virtual void start_MotorStepper()=0
An concrete class.
An concrete class.