Skip to content

Commit bfaafdd

Browse files
Merge pull request #873 from ladyada/master
fix compiler complaints, add HID example
2 parents 33a3c1b + 3c029f0 commit bfaafdd

5 files changed

Lines changed: 134 additions & 17 deletions

File tree

M4_Eyes/M4_Eyes.ino

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ SPISettings settings(DISPLAY_FREQ, MSBFIRST, SPI_MODE0);
105105
// below, to allow for caching/scheduling fudge). If so, that's our signal
106106
// that something is likely amiss and we take evasive maneuvers, resetting
107107
// the affected DMA channel (DMAbuddy::fix()).
108-
#define DMA_TIMEOUT ((DISPLAY_SIZE * 16 * 4000) / (DISPLAY_FREQ / 1000))
108+
#define DMA_TIMEOUT (uint32_t)((DISPLAY_SIZE * 16 * 4000) / (DISPLAY_FREQ / 1000))
109109

110110
static inline uint16_t readBoop(void) {
111111
uint16_t counter = 0;
@@ -142,6 +142,9 @@ void setup() {
142142
#else
143143
if(!arcada.filesysBegin()) fatal("No filesystem found!", 250);
144144
#endif
145+
146+
user_setup();
147+
145148
arcada.displayBegin();
146149

147150
DISPLAY_SIZE = min(ARCADA_TFT_WIDTH, ARCADA_TFT_HEIGHT);
@@ -163,15 +166,15 @@ void setup() {
163166
// config1.eye, config2.eye or config3.eye instead). Keep fingers clear
164167
// of the nose booper when doing this...it self-calibrates on startup.
165168
// DO THIS BEFORE THE SPLASH SO IT DOESN'T REQUIRE A LENGTHY HOLD.
166-
char *filename = "config.eye";
169+
char *filename = (char *)"config.eye";
167170

168171
uint32_t buttonState = arcada.readButtons();
169172
if((buttonState & ARCADA_BUTTONMASK_UP) && arcada.exists("config1.eye")) {
170-
filename = "config1.eye";
173+
filename = (char *)"config1.eye";
171174
} else if((buttonState & ARCADA_BUTTONMASK_A) && arcada.exists("config2.eye")) {
172-
filename = "config2.eye";
175+
filename = (char *)"config2.eye";
173176
} else if((buttonState & ARCADA_BUTTONMASK_DOWN) && arcada.exists("config3.eye")) {
174-
filename = "config3.eye";
177+
filename = (char *)"config3.eye";
175178
}
176179

177180
yield();
@@ -184,11 +187,11 @@ void setup() {
184187
#endif
185188

186189
yield();
187-
if (arcada.drawBMP("/splash.bmp", 0, 0, (eye[0].display)) == IMAGE_SUCCESS) {
190+
if (arcada.drawBMP((char *)"/splash.bmp", 0, 0, (eye[0].display)) == IMAGE_SUCCESS) {
188191
Serial.println("Splashing");
189192
if (NUM_EYES > 1) { // other eye
190193
yield();
191-
arcada.drawBMP("/splash.bmp", 0, 0, (eye[1].display));
194+
arcada.drawBMP((char *)"/splash.bmp", 0, 0, (eye[1].display));
192195
}
193196
// backlight on for a bit
194197
for (int bl=0; bl<=250; bl+=20) {
@@ -295,7 +298,6 @@ void setup() {
295298
// leave some RAM for the stack to operate over the lifetime of this
296299
// program and to handle small heap allocations.
297300

298-
ImageReturnCode status;
299301
uint32_t maxRam = availableRAM() - stackReserve;
300302

301303
// Load texture maps for eyes
@@ -356,6 +358,7 @@ void setup() {
356358

357359
// Load eyelid graphics.
358360
yield();
361+
ImageReturnCode status;
359362

360363
status = loadEyelid(upperEyelidFilename ?
361364
upperEyelidFilename : (char *)"upper.bmp",
@@ -415,8 +418,6 @@ void setup() {
415418
boopThreshold = boopThreshold * 110 / 100; // 10% overhead
416419
}
417420

418-
user_setup();
419-
420421
lastLightReadTime = micros() + 2000000; // Delay initial light reading
421422
}
422423

M4_Eyes/file.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static int32_t dwim(JsonVariant v, int32_t def = 0) { // "Do What I Mean"
6464
}
6565
}
6666

67+
/*
6768
static void getFilename(JsonVariant v, char **ptr) {
6869
if(*ptr) { // If string already allocated,
6970
free(*ptr); // delete old value...
@@ -73,6 +74,7 @@ static void getFilename(JsonVariant v, char **ptr) {
7374
*ptr = strdup(v); // Make a copy of string, save that
7475
}
7576
}
77+
*/
7678

7779
void loadConfig(char *filename) {
7880
File file;
@@ -135,7 +137,7 @@ void loadConfig(char *filename) {
135137
if(pMin > pMax) {
136138
float temp = pMin;
137139
pMin = pMax;
138-
pMax = pMin;
140+
pMax = temp;
139141
}
140142
irisMin = (1.0 - pMax);
141143
irisRange = (pMax - pMin);
@@ -337,7 +339,7 @@ ImageReturnCode loadEyelid(char *filename,
337339
if(reader->bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
338340
tempBytes = ((w + 7) / 8) * h; // Bitmap size in bytes
339341
if (maxRam > tempBytes) {
340-
if(tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) {
342+
if((tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) != NULL) {
341343
// Make SOME tempPtr reference, or optimizer removes the alloc!
342344
tempPtr[0] = 0;
343345
}
@@ -420,7 +422,7 @@ ImageReturnCode loadTexture(char *filename, uint16_t **data,
420422
if(reader->bmpDimensions(filename, &w, &h) == IMAGE_SUCCESS) {
421423
tempBytes = w * h * 2; // Image size in bytes (converted to 16bpp)
422424
if (maxRam > tempBytes) {
423-
if(tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) {
425+
if((tempPtr = (uint8_t *)malloc(maxRam - tempBytes)) != NULL) {
424426
// Make SOME tempPtr reference, or optimizer removes the alloc!
425427
tempPtr[0] = 0;
426428
}

M4_Eyes/pdmvoice.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void voiceGain(float g) {
191191
void voiceMod(uint32_t freq, uint8_t waveform) {
192192
if(modBuf) { // Ignore if no modulation buffer allocated
193193
if(freq < MOD_MIN) freq = MOD_MIN;
194-
modLen = (int)(actualPlaybackRate / freq + 0.5);
194+
modLen = (uint32_t)(actualPlaybackRate / freq + 0.5);
195195
if(modLen < 2) modLen = 2;
196196
if(waveform > 4) waveform = 4;
197197
modWave = waveform;
@@ -204,17 +204,17 @@ void voiceMod(uint32_t freq, uint8_t waveform) {
204204
memset(&modBuf[modLen / 2], 0, modLen - modLen / 2);
205205
break;
206206
case 2: // Sine
207-
for(int i=0; i<modLen; i++) {
207+
for(uint32_t i=0; i<modLen; i++) {
208208
modBuf[i] = (int)((sin(M_PI * 2.0 * (float)i / (float)modLen) + 1.0) * 0.5 * 255.0 + 0.5);
209209
}
210210
break;
211211
case 3: // Triangle
212-
for(int i=0; i<modLen; i++) {
212+
for(uint32_t i=0; i<modLen; i++) {
213213
modBuf[i] = (int)(fabs(0.5 - (float)i / (float)modLen) * 2.0 * 255.0 + 0.5);
214214
}
215215
break;
216216
case 4: // Sawtooth (increasing)
217-
for(int i=0; i<modLen; i++) {
217+
for(uint32_t i=0; i<modLen; i++) {
218218
modBuf[i] = (int)((float)i / (float)(modLen - 1) * 255.0 + 0.5);
219219
}
220220
break;

M4_Eyes/user_hid.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
2+
3+
#include <Arduino.h>
4+
#include "Adafruit_TinyUSB.h"
5+
#include "globals.h"
6+
7+
#define UP_BUTTON_KEYCODE_TO_SEND HID_KEY_U
8+
#define A_BUTTON_KEYCODE_TO_SEND HID_KEY_A
9+
#define DOWN_BUTTON_KEYCODE_TO_SEND HID_KEY_D
10+
#define SHAKE_KEYCODE_TO_SEND HID_KEY_SPACE
11+
12+
// HID report descriptor using TinyUSB's template
13+
// Single Report (no ID) descriptor
14+
uint8_t const desc_hid_report[] =
15+
{
16+
TUD_HID_REPORT_DESC_KEYBOARD(),
17+
};
18+
19+
Adafruit_USBD_HID usb_hid;
20+
21+
void user_setup(void) {
22+
usb_hid.setPollInterval(20);
23+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
24+
25+
usb_hid.begin();
26+
27+
pinMode(LED_BUILTIN, OUTPUT);
28+
digitalWrite(LED_BUILTIN, LOW);
29+
arcada.accel.setClick(1, 50);
30+
31+
// wait until device mounted
32+
while( !USBDevice.mounted() ) delay(1);
33+
}
34+
35+
void user_loop(void) {
36+
if ( !usb_hid.ready() ) {
37+
Serial.println("not ready");
38+
return;
39+
}
40+
41+
uint8_t keycode[6] = { 0 };
42+
43+
uint32_t buttonState = arcada.readButtons();
44+
if (buttonState & ARCADA_BUTTONMASK_UP) {
45+
Serial.println("Up");
46+
keycode[0] = UP_BUTTON_KEYCODE_TO_SEND;
47+
}
48+
if (buttonState & ARCADA_BUTTONMASK_A) {
49+
Serial.println("A");
50+
keycode[1] = A_BUTTON_KEYCODE_TO_SEND;
51+
}
52+
if (buttonState & ARCADA_BUTTONMASK_DOWN) {
53+
Serial.println("Down");
54+
keycode[2] = DOWN_BUTTON_KEYCODE_TO_SEND;
55+
}
56+
57+
uint8_t shake = arcada.accel.getClick();
58+
if (shake & 0x30) {
59+
Serial.print("shake detected (0x"); Serial.print(shake, HEX); Serial.print("): ");
60+
if (shake & 0x10) Serial.println(" single shake");
61+
keycode[3] = SHAKE_KEYCODE_TO_SEND;
62+
}
63+
64+
bool anypressed = false;
65+
for (int k=0; k<sizeof(keycode); k++) {
66+
if (keycode[k] != 0) {
67+
anypressed = true;
68+
break;
69+
}
70+
}
71+
if (anypressed) {
72+
digitalWrite(LED_BUILTIN, HIGH);
73+
usb_hid.keyboardReport(0, 0, keycode);
74+
} else {
75+
digitalWrite(LED_BUILTIN, LOW);
76+
usb_hid.keyboardRelease(0);
77+
}
78+
}
79+
80+
#endif

M4_Eyes/user_neopixel.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
2+
3+
#include <Adafruit_NeoPixel.h>
4+
5+
#define LED_PIN 8
6+
#define LED_COUNT 4
7+
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
8+
9+
void user_setup(void) {
10+
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
11+
strip.show(); // Turn OFF all pixels ASAP
12+
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
13+
}
14+
15+
long firstPixelHue = 0;
16+
17+
void user_loop(void) {
18+
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
19+
// Offset pixel hue by an amount to make one full revolution of the
20+
// color wheel (range of 65536) along the length of the strip
21+
// (strip.numPixels() steps):
22+
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
23+
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
24+
// optionally add saturation and value (brightness) (each 0 to 255).
25+
// Here we're using just the single-argument hue variant. The result
26+
// is passed through strip.gamma32() to provide 'truer' colors
27+
// before assigning to each pixel:
28+
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
29+
}
30+
strip.show(); // Update strip with new contents
31+
firstPixelHue += 256;
32+
}
33+
34+
#endif // 0

0 commit comments

Comments
 (0)