Skip to content

Commit 2b4827c

Browse files
committed
M4_Eyes.ino: add a set of user_callable functions to control the eyes: eyesWide, eyesBlink, eyesToCorner, eyesNormal
file.cpp: add additional user_setup function that passes in the configuration file.cpp: add some accessor functions that use dwim() globals.h: declarations of new functions user_watch.cpp: fix case on HeatSensor.h for case-sensitive compilation systems user_watch.cpp: convert to using new eyesToCorner() user_*.cpp: add user_setup(doc) function new user_wiichuck.cpp: connects to WiiChuck that controls eyes and NeoPixel strip
1 parent 1ffa172 commit 2b4827c

File tree

10 files changed

+329
-5
lines changed

10 files changed

+329
-5
lines changed

M4_Eyes/M4_Eyes.ino

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ uint32_t availableRAM(void) {
133133
return &top - (char *)sbrk(0); // Top of stack minus end of heap
134134
}
135135

136+
// USER CALLABLE FUNCTIONS
137+
138+
// set the booped flag
139+
void eyesWide(bool t) {
140+
booped = t;
141+
}
142+
143+
// start a blink
144+
void eyesBlink() {
145+
timeToNextBlink = 0;
146+
}
147+
148+
// force the eyes to a position on the screen
149+
void eyesToCorner(float x, float y, bool immediate) {
150+
moveEyesRandomly = false;
151+
eyeTargetX = x;
152+
eyeTargetY = y;
153+
if (immediate)
154+
eyeMoveDuration = 0;
155+
}
156+
157+
// return the eyes to normal random movement
158+
void eyesNormal() {
159+
moveEyesRandomly = true;
160+
}
161+
162+
136163
// SETUP FUNCTION - CALLED ONCE AT PROGRAM START ---------------------------
137164

138165
void setup() {
@@ -884,7 +911,8 @@ void loop() {
884911
lightSensorPin = -1; // Stop trying to use the light sensor
885912
} else {
886913
lastLightReadTime = t - LIGHT_INTERVAL + 30000; // Try again in 30 ms
887-
} }
914+
}
915+
}
888916
}
889917
irisValue = (irisValue * 0.97) + (lastLightValue * 0.03); // Filter response for smooth reaction
890918
} else {

M4_Eyes/file.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ void loadConfig(char *filename) {
283283
#endif // ADAFRUIT_MONSTER_M4SK_EXPRESS
284284
}
285285
file.close();
286+
user_setup(doc);
286287
} else {
287288
Serial.println("Can't open config file, using default settings");
288289
}
@@ -454,3 +455,15 @@ ImageReturnCode loadTexture(char *filename, uint16_t **data,
454455

455456
return status;
456457
}
458+
459+
// Utility functions for use by user functions to grab an integer value from the config file
460+
// using dwim() and providing a default value.
461+
int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, int32_t def) {
462+
return dwim(doc[nm], def);
463+
}
464+
int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, const char *nm2, int32_t def) {
465+
return dwim(doc[nm][nm2], def);
466+
}
467+
int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, const char *nm2, const char *nm3, int32_t def) {
468+
return dwim(doc[nm][nm2][nm3], def);
469+
}

M4_Eyes/globals.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ GLOBAL_VAR float trackFactor GLOBAL_INIT(0.5);
7272

7373
// Random eye motion: provided by the base project, but overridable by user code.
7474
GLOBAL_VAR bool moveEyesRandomly GLOBAL_INIT(true); // Clear to suppress random eye motion and let user code control it
75-
GLOBAL_VAR float eyeTargetX GLOBAL_INIT(0.0); // THen set these continuously in user_loop.
75+
GLOBAL_VAR float eyeTargetX GLOBAL_INIT(0.0); // Then set these continuously in user_loop.
7676
GLOBAL_VAR float eyeTargetY GLOBAL_INIT(0.0); // Range is from -1.0 to +1.0.
7777

7878
// Pin definition stuff will go here
@@ -210,6 +210,9 @@ extern bool filesystem_change_flag GLOBAL_INIT(true);
210210
extern void loadConfig(char *filename);
211211
extern ImageReturnCode loadEyelid(char *filename, uint8_t *minArray, uint8_t *maxArray, uint8_t init, uint32_t maxRam);
212212
extern ImageReturnCode loadTexture(char *filename, uint16_t **data, uint16_t *width, uint16_t *height, uint32_t maxRam);
213+
extern int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, int32_t def);
214+
extern int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, const char *nm2, int32_t def);
215+
extern int32_t getDocInt(StaticJsonDocument<2048> &doc, const char *nm, const char *nm2, const char *nm3, int32_t def);
213216

214217
// Functions in memory.cpp
215218
extern uint32_t availableRAM(void);
@@ -232,5 +235,13 @@ extern float screen2map(int in);
232235
extern float map2screen(int in);
233236

234237
// Functions in user.cpp
238+
#include <ArduinoJson.h> // JSON config file functions
235239
extern void user_setup(void);
240+
extern void user_setup(StaticJsonDocument<2048> &doc);
236241
extern void user_loop(void);
242+
243+
// user callable functions
244+
extern void eyesWide(bool);
245+
extern void eyesBlink();
246+
extern void eyesToCorner(float x, float y, bool immediate);
247+
extern void eyesNormal();

M4_Eyes/user.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
void user_setup(void) {
1616
}
1717

18+
// Called once after the processing of the configuration file. This allows
19+
// user configuration to also be done based on the config file.
20+
#include <ArduinoJson.h> // JSON config file functions
21+
void user_setup(StaticJsonDocument<2048> &doc) {
22+
}
23+
1824
// Called periodically during eye animation. This is invoked in the
1925
// interval before starting drawing on the last eye (left eye on MONSTER
2026
// M4SK, sole eye on HalloWing M0) so it won't exacerbate visible tearing

M4_Eyes/user_fizzgig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ void user_setup(void) {
6060
}
6161
}
6262

63+
// Called once after the processing of the configuration file. This allows
64+
// user configuration to also be done based on the config file.
65+
#include <ArduinoJson.h> // JSON config file functions
66+
void user_setup(StaticJsonDocument<2048> &doc) {
67+
}
68+
6369
void user_loop(void) {
6470
if(playing) {
6571
// While WAV is playing, wiggle servo between middle and open-mouth positions:

M4_Eyes/user_hid.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ void user_setup(void) {
3838
while( !USBDevice.mounted() ) delay(1);
3939
}
4040

41+
// Called once after the processing of the configuration file. This allows
42+
// user configuration to also be done based on the config file.
43+
#include <ArduinoJson.h> // JSON config file functions
44+
void user_setup(StaticJsonDocument<2048> &doc) {
45+
}
46+
4147
void user_loop(void) {
4248
if ( !usb_hid.ready() ) {
4349
Serial.println("not ready");

M4_Eyes/user_neopixel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void user_setup(void) {
1414

1515
long firstPixelHue = 0;
1616

17+
// Called once after the processing of the configuration file. This allows
18+
// user configuration to also be done based on the config file.
19+
#include <ArduinoJson.h> // JSON config file functions
20+
void user_setup(StaticJsonDocument<2048> &doc) {
21+
}
22+
1723
void user_loop(void) {
1824
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
1925
// Offset pixel hue by an amount to make one full revolution of the

M4_Eyes/user_touchneopixels.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ void user_setup(void) {
245245
changeBehavior(0, micros());
246246
}
247247

248+
// Called once after the processing of the configuration file. This allows
249+
// user configuration to also be done based on the config file.
250+
#include <ArduinoJson.h> // JSON config file functions
251+
void user_setup(StaticJsonDocument<2048> &doc) {
252+
}
253+
248254
void user_loop(void) {
249255
uint32_t elapsedSince = micros();
250256
if(elapsedSince - lastTouchSample > TOUCH_SAMPLE_TIME) {

M4_Eyes/user_watch.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// CORRESPONDING LINE IN HeatSensor.cpp MUST ALSO BE ENABLED!
33

44
#include "globals.h"
5-
#include "heatSensor.h"
5+
#include "HeatSensor.h"
66

77
// For heat sensing
88
HeatSensor heatSensor;
@@ -25,6 +25,11 @@ void user_setup(void) {
2525
heatSensor.setup();
2626
}
2727

28+
// Called once after the processing of the configuration file. This allows
29+
// user configuration to also be done via the config file.
30+
void user_setup(StaticJsonDocument<2048> &doc) {
31+
}
32+
2833
// Called periodically during eye animation. This is invoked in the
2934
// interval before starting drawing on the last eye (left eye on MONSTER
3035
// M4SK, sole eye on HalloWing M0) so it won't exacerbate visible tearing
@@ -35,8 +40,7 @@ void user_loop(void) {
3540
heatSensor.find_focus();
3641

3742
// Set values for the new X and Y.
38-
eyeTargetX = heatSensor.x;
39-
eyeTargetY = -heatSensor.y;
43+
eyesToCorner(heatSensor.x, -heatSensor.y, false);
4044
}
4145

4246
#endif // 0

0 commit comments

Comments
 (0)