added arduino, modified build

This commit is contained in:
2020-02-02 15:28:36 -08:00
parent 0189d519c6
commit 6480bc593f
3583 changed files with 1305025 additions and 247 deletions

View File

@@ -0,0 +1,95 @@
#ifndef CAPTOUCHDEMO_H
#define CAPTOUCHDEMO_H
#include "Demo.h"
#define CAP_SAMPLES 20 // Number of samples to take for a capacitive touch read.
#define TONE_DURATION_MS 100 // Duration in milliseconds to play a tone when touched.
class CapTouchDemo: public Demo {
public:
uint16_t CAP_THRESHOLD = 200; // Threshold for a capacitive touch (higher = less sensitive).
CapTouchDemo() {
playSound = false;
if (CircuitPlayground.isExpress()) {
CAP_THRESHOLD = 800;
} else {
CAP_THRESHOLD = 200;
}
}
~CapTouchDemo() {}
virtual void loop() {
// Clear all the neopixels.
for (int i=0; i<10; ++i) {
CircuitPlayground.strip.setPixelColor(i, 0);
}
// Check if any of the cap touch inputs are pressed and turn on those pixels.
// Also play a tone if in tone playback mode.
if (CircuitPlayground.readCap(0, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(3, CircuitPlayground.colorWheel(256/10*3));
if (playSound) {
CircuitPlayground.playTone(330, TONE_DURATION_MS); // 330hz = E4
}
}
if (CircuitPlayground.readCap(1, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(4, CircuitPlayground.colorWheel(256/10*4));
if (playSound) {
CircuitPlayground.playTone(349, TONE_DURATION_MS); // 349hz = F4
}
}
if (CircuitPlayground.readCap(2, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(1, CircuitPlayground.colorWheel(256/10));
if (playSound) {
CircuitPlayground.playTone(294, TONE_DURATION_MS); // 294hz = D4
}
}
if (CircuitPlayground.readCap(3, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(0, CircuitPlayground.colorWheel(0));
if (playSound) {
CircuitPlayground.playTone(262, TONE_DURATION_MS); // 262hz = C4
}
}
if (CircuitPlayground.readCap(6, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(6, CircuitPlayground.colorWheel(256/10*6));
if (playSound) {
CircuitPlayground.playTone(440, TONE_DURATION_MS); // 440hz = A4
}
}
if (CircuitPlayground.readCap(9, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(8, CircuitPlayground.colorWheel(256/10*8));
if (playSound) {
CircuitPlayground.playTone(494, TONE_DURATION_MS); // 494hz = B4
}
}
if (CircuitPlayground.readCap(10, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(9, CircuitPlayground.colorWheel(256/10*9));
if (playSound) {
CircuitPlayground.playTone(523, TONE_DURATION_MS); // 523hz = C5
}
}
if (CircuitPlayground.readCap(12, CAP_SAMPLES) >= CAP_THRESHOLD) {
CircuitPlayground.strip.setPixelColor(5, CircuitPlayground.colorWheel(256/10*5));
if (playSound) {
CircuitPlayground.playTone(392, TONE_DURATION_MS); // 392hz = G4
}
}
// Light up the pixels.
CircuitPlayground.strip.show();
}
virtual void modePress() {
// Turn sound on/off.
playSound = !playSound;
}
private:
bool playSound;
};
#endif

View File

@@ -0,0 +1,27 @@
#ifndef DEMO_H
#define DEMO_H
// Define each mode with the following interface for a loop and modePress
// function that will be called during the main loop and if the mode button
// was pressed respectively. It's up to each mode implementation to fill
// these in and add their logic.
class Demo {
public:
virtual ~Demo() {}
virtual void loop() = 0;
virtual void modePress() = 0;
};
// Linear interpolation function is handy for all the modes to use.
float lerp(float x, float xmin, float xmax, float ymin, float ymax) {
if (x >= xmax) {
return ymax;
}
if (x <= xmin) {
return ymin;
}
return ymin + (ymax-ymin)*((x-xmin)/(xmax-xmin));
}
#endif

View File

@@ -0,0 +1,40 @@
#ifndef RAINBOWCYCLEDEMO_H
#define RAINBOWCYCLEDEMO_H
#include "Demo.h"
// Animation speeds to loop through with mode presses. The current milliseconds
// are divided by this value so the smaller the value the faster the animation.
static int speeds[] = { 5, 10, 50, 100 };
class RainbowCycleDemo: public Demo {
public:
RainbowCycleDemo() { currentSpeed = 0; }
~RainbowCycleDemo() {}
virtual void loop() {
// Make an offset based on the current millisecond count scaled by the current speed.
uint32_t offset = millis() / speeds[currentSpeed];
// Loop through each pixel and set it to an incremental color wheel value.
for(int i=0; i<10; ++i) {
CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(((i * 256 / 10) + offset) & 255));
}
// Show all the pixels.
CircuitPlayground.strip.show();
}
virtual void modePress() {
// Increment through the available speeds.
currentSpeed += 1;
if (currentSpeed >= sizeof(speeds)/sizeof(int)) {
currentSpeed = 0;
}
}
private:
int currentSpeed;
};
#endif

View File

@@ -0,0 +1,65 @@
#ifndef SENSORDEMO_H
#define SENSORDEMO_H
#include "Demo.h"
// Define small, medium, large range of light sensor values.
static int minLight[] = { 0, 0, 0};
static int maxLight[] = { 50, 255, 1023};
// Define small, medium, large range of temp sensor values (in Fahrenheit).
static float minTempF[] = { 80.0, 70.0, -32.0};
static float maxTempF[] = { 85.0, 90.0, 212.0};
// Define color for light sensor pixels.
#define LIGHT_RED 0xFF
#define LIGHT_GREEN 0x00
#define LIGHT_BLUE 0x00
// Define color for temp sensor pixels.
#define TEMP_RED 0x00
#define TEMP_GREEN 0x00
#define TEMP_BLUE 0xFF
class SensorDemo: public Demo {
public:
SensorDemo() { mode = 0; }
~SensorDemo() {}
virtual void loop() {
// Reset all lights to off.
for (int i=0; i<10; ++i) {
CircuitPlayground.strip.setPixelColor(i, 0);
}
// Measure the light level and use it to light up its LEDs (right half).
uint16_t light = CircuitPlayground.lightSensor();
int level = (int)lerp(light, minLight[mode], maxLight[mode], 0.0, 5.0);
for (int i=9; i>9-level; --i) {
CircuitPlayground.strip.setPixelColor(i, LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE);
}
// Measure the temperatue and use it to light up its LEDs (left half).
float tempF = CircuitPlayground.temperatureF();
level = (int)lerp(tempF, minTempF[mode], maxTempF[mode], 0.0, 5.0);
for (int i=0; i<level; ++i) {
CircuitPlayground.strip.setPixelColor(i, TEMP_RED, TEMP_GREEN, TEMP_BLUE);
}
// Light up the pixels!
CircuitPlayground.strip.show();
}
virtual void modePress() {
// Switch to one of three modes for small, medium, big ranges of measurements.
mode += 1;
if (mode > 2) {
mode = 0;
}
}
private:
int mode;
};
#endif

View File

@@ -0,0 +1,71 @@
#ifndef TILTDEMO_H
#define TILTDEMO_H
#include "Demo.h"
// Define range of possible acceleration values.
#define MIN_ACCEL -10.0
#define MAX_ACCEL 10.0
// Define range of colors (min color and max color) using their red, green, blue components.
// First the min color:
#define MIN_COLOR_RED 0xFF
#define MIN_COLOR_GREEN 0x00
#define MIN_COLOR_BLUE 0x00
// Then the max color:
#define MAX_COLOR_RED 0x00
#define MAX_COLOR_GREEN 0x00
#define MAX_COLOR_BLUE 0xFF
class TiltDemo: public Demo {
public:
TiltDemo() { mode = 0; }
~TiltDemo() {}
virtual void loop() {
// Grab the acceleration for the current mode's axis.
float accel = 0;
switch (mode) {
case 0:
accel = CircuitPlayground.motionX();
break;
case 1:
accel = CircuitPlayground.motionY();
break;
case 2:
accel = CircuitPlayground.motionZ();
break;
}
// Now interpolate the acceleration into a color for the pixels.
uint8_t red = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_RED, MAX_COLOR_RED);
uint8_t green = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_GREEN, MAX_COLOR_GREEN);
uint8_t blue = (uint8_t)lerp(accel, MIN_ACCEL, MAX_ACCEL, MIN_COLOR_BLUE, MAX_COLOR_BLUE);
// Gamma correction makes LED brightness appear more linear
red = CircuitPlayground.gamma8(red);
green = CircuitPlayground.gamma8(green);
blue = CircuitPlayground.gamma8(blue);
// Light up all the pixels the interpolated color.
for (int i=0; i<10; ++i) {
CircuitPlayground.strip.setPixelColor(i, red, green, blue);
}
CircuitPlayground.strip.show();
}
virtual void modePress() {
// Change the mode (axis being displayed) to a value inside 0-2 for X, Y, Z.
mode += 1;
if (mode > 2) {
mode = 0;
}
}
private:
int mode;
};
#endif

View File

@@ -0,0 +1,100 @@
// This demo is based on the vumeter demo in the Adafruit Circuit Playground library.
#ifndef VUMETERDEMO_H
#define VUMETERDEMO_H
#include <math.h>
#include "Demo.h"
#define SAMPLE_WINDOW 10 // Sample window for average level
#define PEAK_HANG 24 // Time of pause before peak dot falls
#define PEAK_FALL 4 // Rate of falling peak dot
#define INPUT_FLOOR 56 // Lower range of mic sensitivity in dB SPL
#define INPUT_CEILING 110 // Upper range of mic sensitivity in db SPL
static byte peak = 16; // Peak level of column; used for falling dots
static unsigned int sample;
static byte dotCount = 0; //Frame counter for peak dot
static byte dotHangCount = 0; //Frame counter for holding peak dot
static float mapf(float x, float in_min, float in_max, float out_min, float out_max);
static void drawLine(uint8_t from, uint8_t to, uint32_t c);
class VUMeterDemo: public Demo {
public:
VUMeterDemo() { currentCeiling = 0; }
~VUMeterDemo() {}
virtual void loop() {
int numPixels = CircuitPlayground.strip.numPixels();
float peakToPeak = 0; // peak-to-peak level
unsigned int c, y;
//get peak sound pressure level over the sample window
peakToPeak = CircuitPlayground.mic.soundPressureLevel(SAMPLE_WINDOW);
//limit to the floor value
peakToPeak = max(INPUT_FLOOR, peakToPeak);
// Serial.println(peakToPeak);
//Fill the strip with rainbow gradient
for (int i=0;i<=numPixels-1;i++){
CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(map(i,0,numPixels-1,30,150)));
}
c = mapf(peakToPeak, INPUT_FLOOR, INPUT_CEILING, numPixels, 0);
// Turn off pixels that are below volume threshold.
if(c < peak) {
peak = c; // Keep dot on top
dotHangCount = 0; // make the dot hang before falling
}
if (c <= numPixels) { // Fill partial column with off pixels
drawLine(numPixels, numPixels-c, CircuitPlayground.strip.Color(0, 0, 0));
}
// Set the peak dot to match the rainbow gradient
y = numPixels - peak;
CircuitPlayground.strip.setPixelColor(y-1,CircuitPlayground.colorWheel(map(y,0,numPixels-1,30,150)));
CircuitPlayground.strip.show();
// Frame based peak dot animation
if(dotHangCount > PEAK_HANG) { //Peak pause length
if(++dotCount >= PEAK_FALL) { //Fall rate
peak++;
dotCount = 0;
}
}
else {
dotHangCount++;
}
}
virtual void modePress() {
}
private:
int currentCeiling;
};
static float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//Used to draw a line between two points of a given color
static void drawLine(uint8_t from, uint8_t to, uint32_t c) {
uint8_t fromTemp;
if (from > to) {
fromTemp = from;
from = to;
to = fromTemp;
}
for(int i=from; i<=to; i++){
CircuitPlayground.strip.setPixelColor(i, c);
}
}
#endif

View File

@@ -0,0 +1,121 @@
// Circuit Playground MEGA Demo
//
// Showcases interesting demos of all the components on Circuit Playground.
// This is a somewhat advanced sketch that's broken across multiple files (see
// the tabs above for all the files). When run on Circuit Playground you can
// press the left button to cycle through each demo, and the right button to
// cycle through each mode inside a demo. When the slide switch is in the -/right
// position the board will 'turn off' and go into deep sleep (pullin around 5mA
// of current), and when moved into the +/left position it will turn on and run
// the demos. Make sure the switch is on the +/left side after you upload or else
// the board won't do anything!
//
// NOTE: This example requires the Adafruit SleepyDog sleep & watchdog library
// be installed. Use the library manager to install, or grab it from its home:
// - https://github.com/adafruit/Adafruit_SleepyDog
// - https://github.com/adafruit/Adafruit_ASFcore (for Express)
//
// The following demos are available (in order):
// - NeoPixel color rainbow cycle
// This animates all 10 neopixels with a rainbow cycle. Changing the mode
// changes the speed of animation.
// - Volume level meter
// This uses the microphone to display a volume level meter. The louder the
// volume the more lights light up (and a peak level dot animates back down).
// Changing mode changes the sensitivity from medium to very high to very low
// and back to medium again (3 levels).
// - Capacitive touch instrument
// This uses the capacitive touch pads (0, 1, 2, 3, 6, 9, 10, 12) and lights up
// a nearby pixel when a pad is touched. Pressing the mode toggles between
// playing a different tone for each pad too (8 notes total covering the basic
// C4 scale).
// - Accelerometer tilt demo
// This lights up all the pixels to a color between blue and red depending on
// the value of the accelerometer. Try tilting the board around axes to see
// how it behaves. Pressing mode toggles between the X, Y, Z axis as the one
// under measurements (starts with X axis).
// - Temperature & light sensor demo
// The left half of the pixels light up blue proportional to the temperature
// of the thermistor, and the right half of the pixels light up red proportional
// to the light sensor. Pressing mode toggles between small, medium, and big
// ranges of measurements for each sensor.
//
// Author: Tony DiCola
// License: MIT License (https://opensource.org/licenses/MIT)
#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>
#include "Adafruit_SleepyDog.h"
// Include all the demos, note that each demo is defined in a separate class to keep the sketch
// code below clean and simple.
#include "Demo.h"
#include "RainbowCycleDemo.h"
#include "VUMeterDemo.h"
#include "CapTouchDemo.h"
#include "TiltDemo.h"
#include "SensorDemo.h"
// Create an instance of each demo class.
RainbowCycleDemo rainbowCycleDemo;
VUMeterDemo vuMeterDemo;
CapTouchDemo capTouchDemo;
TiltDemo tiltDemo;
SensorDemo sensorDemo;
// Make a list of all demo class instances and keep track of the currently selected one.
int currentDemo = 0;
Demo* demos[] = {
&rainbowCycleDemo,
&vuMeterDemo,
&capTouchDemo,
&tiltDemo,
&sensorDemo
};
void setup() {
// Initialize serial port and circuit playground library.
Serial.begin(115200);
Serial.println("Circuit Playground MEGA Demo!");
CircuitPlayground.begin();
}
void loop() {
// Check if slide switch is on the left (false) and go to sleep.
while (!CircuitPlayground.slideSwitch()) {
// Turn off the pixels, then go into deep sleep for a second.
CircuitPlayground.clearPixels();
Watchdog.sleep(1000);
}
// Check for any button presses by checking their state twice with
// a delay inbetween. If the first press state is different from the
// second press state then something was pressed/released!
bool leftFirst = CircuitPlayground.leftButton();
bool rightFirst = CircuitPlayground.rightButton();
delay(10);
// Run current demo's main loop.
demos[currentDemo]->loop();
// Now check for buttons that were released.
bool leftSecond = CircuitPlayground.leftButton();
bool rightSecond = CircuitPlayground.rightButton();
// Left button will change the current demo.
if (leftFirst && !leftSecond) {
// Turn off all the pixels when entering new mode.
CircuitPlayground.clearPixels();
// Increment the current demo (looping back to zero if at end).
currentDemo += 1;
if (currentDemo >= (sizeof(demos)/sizeof(Demo*))) {
currentDemo = 0;
}
Serial.print("Changed to demo: "); Serial.println(currentDemo, DEC);
}
// Right button will change the mode of the current demo.
if (rightFirst && !rightSecond) {
demos[currentDemo]->modePress();
}
}