Add proximity

This commit is contained in:
Neale Pickett 2020-12-06 22:07:19 -07:00
parent 4ac13c534e
commit 15302ef6d8
1 changed files with 135 additions and 28 deletions

View File

@ -1,17 +1,21 @@
#include <SPI.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h> #include <Adafruit_PCD8544.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <FastLED.h> #include <FastLED.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <SPI.h>
#include "morse.h" #include "morse.h"
#include "musicplayer.h"
#include "paj7620.h"
#include "pulse.h" #include "pulse.h"
#include "riddler.h" #include "riddler.h"
#include "musicplayer.h"
#define NUM_PUZZLES 6
// WS2812 LEDs // WS2812 LEDs
#define LEDS_PIN 1 #define LEDS_PIN 1
#define NUM_LEDS 2 #define NUM_LEDS NUM_PUZZLES
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
CHSV ColorSolved = CHSV(32, 200, 40);
// Laser // Laser
#define LASER_PIN 0 #define LASER_PIN 0
@ -37,29 +41,39 @@ Adafruit_PCD8544 display = Adafruit_PCD8544(DISPLAY_DC, DISPLAY_SCE, DISPLAY_RST
void setup() { void setup() {
FastLED.addLeds<WS2812, LEDS_PIN, GRB>(leds, NUM_LEDS); FastLED.addLeds<WS2812, LEDS_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(96);
// Turn on backlight // Turn on backlight
pinMode(DISPLAY_LED, OUTPUT); pinMode(DISPLAY_LED, OUTPUT);
analogWrite(9, 64); analogWrite(9, 64);
// Turn on display // Turn on display
display.begin(); display.begin();
display.setContrast(50); display.setContrast(50);
display.clearDisplay();
display.drawBitmap( // Gesture sensor
(DISPLAY_WIDTH - riddler_width)/2, while (paj7620Init()) {
(DISPLAY_HEIGHT - riddler_height)/2, display.clearDisplay();
riddler_bits, display.print("PAJ7260?");
riddler_width, display.display();
riddler_height, }
0xffff
);
display.display();
pinMode(LASER_PIN, OUTPUT); pinMode(LASER_PIN, OUTPUT);
pinMode(PHOTO_PIN, INPUT); pinMode(PHOTO_PIN, INPUT);
mp.Play(120*4, TUNE_BOO); // Riddler symbol
display.clearDisplay();
display.drawBitmap(
(DISPLAY_WIDTH - riddler_width) / 2,
(DISPLAY_HEIGHT - riddler_height) / 2,
riddler_bits,
riddler_width,
riddler_height,
0xffff);
display.display();
// Hello!
mp.Play(120 * 4, TUNE_YAY);
} }
CHSV loop_morse(bool fg) { CHSV loop_morse(bool fg) {
@ -70,20 +84,20 @@ CHSV loop_morse(bool fg) {
int recv = analogRead(PHOTO_PIN); int recv = analogRead(PHOTO_PIN);
bool error = ((recv >= 512) != enc.Transmitting); bool error = ((recv >= 512) != enc.Transmitting);
CHSV color; CHSV color;
if (solved) { if (solved) {
color = CHSV(128, 40, 64); return ColorSolved;
} else if (error) { } else if (error) {
++errors; ++errors;
color = CHSV(0, 255, recv>>2); color = CHSV(0, 255, recv >> 2);
} else { } else {
color = CHSV(128, 255, recv>>2); color = CHSV(128, 255, recv >> 2);
} }
if (! pulse.Tick()) { if (!pulse.Tick()) {
return color; return color;
} }
if (fg) { if (fg) {
display.clearDisplay(); display.clearDisplay();
display.setFont(); display.setFont();
@ -91,7 +105,7 @@ CHSV loop_morse(bool fg) {
display.print("The Morse One"); display.print("The Morse One");
display.display(); display.display();
} }
if (enc.Tick()) { if (enc.Tick()) {
digitalWrite(LASER_PIN, enc.Transmitting); digitalWrite(LASER_PIN, enc.Transmitting);
} else { } else {
@ -103,12 +117,73 @@ CHSV loop_morse(bool fg) {
enc.Quiet(30); enc.Quiet(30);
errors = 0; errors = 0;
} }
return color;
}
CHSV loop_konami(bool fg) {
static Pulse pulse = Pulse(100);
CHSV color;
uint8_t gesture;
uint8_t prox = 0;
if (!paj7620ReadReg(0x6c, 1, &prox)) {
display.fillRect(0, 0, 84, 4, 0);
display.fillRect(0, 0, min(prox / 3, 84), 4, 1);
}
color = CHSV(0, 255, prox);
if (!pulse.Tick()) {
return color;
}
if (!paj7620ReadReg(0x43, 1, &gesture)) {
char out = 0;
switch (gesture) {
case 0:
break;
case GES_UP_FLAG:
out = 26; // right
break;
case GES_DOWN_FLAG:
out = 27; // left
break;
case GES_RIGHT_FLAG:
out = 25; // down
break;
case GES_LEFT_FLAG:
out = 24; // up
break;
case GES_FORWARD_FLAG:
out = 15;
break;
case GES_BACKWARD_FLAG:
out = 9;
break;
default:
out = '?';
display.fillRect(0, 32, 15, 8, 0);
display.setCursor(0, 32);
display.print(gesture);
break;
}
if (out) {
display.fillRect(0, 40, 5, 8, 0);
display.setCursor(0, 40);
display.print(out);
}
}
display.display();
return color;
} }
void beHappy() { void beHappy() {
if (!mp.KeepPlaying()) { if (!mp.KeepPlaying()) {
mp.Play(80*4, TUNE_JINGLEBELLS); mp.Play(76 * 4, TUNE_JINGLEBELLS);
display.clearDisplay(); display.clearDisplay();
display.setFont(&FreeSerif9pt7b); display.setFont(&FreeSerif9pt7b);
display.setCursor(0, 12); display.setCursor(0, 12);
@ -122,9 +197,41 @@ void beHappy() {
} }
void loop() { void loop() {
leds[0] = loop_morse(true); static int current = -1;
static bool solved[NUM_PUZZLES] = {0};
static CHSV lastColors[NUM_PUZZLES];
bool writeLEDs = false;
bool allSolved = true;
beHappy(); mp.KeepPlaying();
FastLED.show(); for (int i = 0; i < NUM_PUZZLES; ++i) {
CHSV color = CHSV(0, 0, 0);
bool fg = (current == i);
switch (i) {
case 0:
color = loop_morse(fg);
break;
case 1:
color = loop_konami(fg);
break;
}
if (color != lastColors[i]) {
lastColors[i] = color;
writeLEDs = true;
}
if (color != ColorSolved) {
allSolved = false;
}
leds[i] = color;
}
if (allSolved) {
beHappy();
}
if (writeLEDs) {
FastLED.show();
}
} }