| 1 | /*! |
|---|
| 2 | * @file Adafruit_Circuit_Playground.h |
|---|
| 3 | * |
|---|
| 4 | * This is part of Adafruit's CircuitPlayground driver for the Arduino platform. It is |
|---|
| 5 | * designed specifically to work with the Adafruit CircuitPlayground boards. |
|---|
| 6 | * |
|---|
| 7 | * Adafruit invests time and resources providing this open source code, |
|---|
| 8 | * please support Adafruit and open-source hardware by purchasing |
|---|
| 9 | * products from Adafruit! |
|---|
| 10 | * |
|---|
| 11 | * Written by Ladyada and others for Adafruit Industries. |
|---|
| 12 | * |
|---|
| 13 | * BSD license, all text here must be included in any redistribution. |
|---|
| 14 | * |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #ifndef _ADAFRUIT_CIRCUITPLAYGROUND_H_ |
|---|
| 18 | #define _ADAFRUIT_CIRCUITPLAYGROUND_H_ |
|---|
| 19 | |
|---|
| 20 | #include <Arduino.h> |
|---|
| 21 | #include "utility/Adafruit_CPlay_NeoPixel.h" |
|---|
| 22 | #include "utility/Adafruit_CPlay_LIS3DH.h" |
|---|
| 23 | #include "utility/Adafruit_CPlay_Mic.h" |
|---|
| 24 | #include "utility/Adafruit_CPlay_Speaker.h" |
|---|
| 25 | #include "utility/CP_Firmata.h" |
|---|
| 26 | |
|---|
| 27 | #ifdef __AVR__ // Circuit Playground 'classic' |
|---|
| 28 | #include "utility/CPlay_CapacitiveSensor.h" |
|---|
| 29 | #else |
|---|
| 30 | #include "utility/Adafruit_CPlay_FreeTouch.h" |
|---|
| 31 | #include "utility/IRLibCPE.h" |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #ifndef NOT_AN_INTERRUPT // Not defined in Arduino 1.0.5 |
|---|
| 35 | #define NOT_AN_INTERRUPT -1 ///< Pin is not on an interrupt |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | #ifdef __AVR__ // Circuit Playground 'classic' |
|---|
| 39 | #define CPLAY_CAPSENSE_SHARED 30 ///< capacitive sense pin |
|---|
| 40 | #define CPLAY_REDLED 13 ///< red LED pin |
|---|
| 41 | #define CPLAY_NEOPIXELPIN 17 ///< neopixel pin |
|---|
| 42 | #define CPLAY_SLIDESWITCHPIN 21 ///< slide switch pin |
|---|
| 43 | #define CPLAY_LEFTBUTTON 4 ///< left button pin |
|---|
| 44 | #define CPLAY_RIGHTBUTTON 19 ///< right button pin |
|---|
| 45 | #define CPLAY_LIGHTSENSOR A5 ///< light sensor pin |
|---|
| 46 | #define CPLAY_THERMISTORPIN A0 ///< thermistor pin |
|---|
| 47 | #define CPLAY_SOUNDSENSOR A4 ///< sound sensor pin |
|---|
| 48 | #define CPLAY_BUZZER 5 ///< buzzer pin |
|---|
| 49 | #define CPLAY_LIS3DH_CS 8 ///< LIS3DH chip select pin |
|---|
| 50 | #define CPLAY_LIS3DH_INTERRUPT 7 ///< LIS3DH interrupt pin |
|---|
| 51 | #define CPLAY_LIS3DH_ADDRESS 0x18 ///< LIS3DH I2C address |
|---|
| 52 | #else // Circuit Playground Express |
|---|
| 53 | #define CPLAY_LEFTBUTTON 4 ///< left button pin |
|---|
| 54 | #define CPLAY_RIGHTBUTTON 5 ///< right button pin |
|---|
| 55 | #define CPLAY_SLIDESWITCHPIN 7 ///< slide switch pin |
|---|
| 56 | #define CPLAY_NEOPIXELPIN 8 ///< neopixel pin |
|---|
| 57 | #define CPLAY_REDLED 13 ///< red led pin |
|---|
| 58 | #define CPLAY_IR_EMITTER 25 ///< IR emmitter pin |
|---|
| 59 | #define CPLAY_IR_RECEIVER 26 ///< IR receiver pin |
|---|
| 60 | #define CPLAY_BUZZER A0 ///< buzzer pin |
|---|
| 61 | #define CPLAY_LIGHTSENSOR A8 ///< light sensor pin |
|---|
| 62 | #define CPLAY_THERMISTORPIN A9 ///< thermistor pin |
|---|
| 63 | #define CPLAY_SOUNDSENSOR A4 ///< TBD I2S |
|---|
| 64 | #define CPLAY_LIS3DH_CS -1 ///< LIS3DH chip select pin |
|---|
| 65 | #define CPLAY_LIS3DH_INTERRUPT 27 ///< LIS3DH interrupt pin |
|---|
| 66 | #define CPLAY_LIS3DH_ADDRESS 0x19 ///< LIS3DH I2C address |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | #define SERIESRESISTOR 10000 ///< series resistor for thermistor |
|---|
| 70 | #define THERMISTORNOMINAL 10000 ///< resistance of thermistor at 25 degrees C |
|---|
| 71 | #define TEMPERATURENOMINAL 25 ///< temp. for nominal resistance (almost always 25 C) |
|---|
| 72 | |
|---|
| 73 | #define BCOEFFICIENT 3380 ///< The beta coefficient of the thermistor (usually 3000-4000) |
|---|
| 74 | |
|---|
| 75 | /*! |
|---|
| 76 | @brief Configuration to tune the color sensing logic: |
|---|
| 77 | Amount of time (in milliseconds) to wait between |
|---|
| 78 | changing the pixel color and reading the light |
|---|
| 79 | sensor. |
|---|
| 80 | */ |
|---|
| 81 | #define LIGHT_SETTLE_MS 100 |
|---|
| 82 | |
|---|
| 83 | /**************************************************************************/ |
|---|
| 84 | /*! |
|---|
| 85 | @brief Class that stores state and functions for interacting with CircuitPlayground hardware |
|---|
| 86 | */ |
|---|
| 87 | /**************************************************************************/ |
|---|
| 88 | class Adafruit_CircuitPlayground { |
|---|
| 89 | public: |
|---|
| 90 | bool begin(uint8_t brightness=20); |
|---|
| 91 | |
|---|
| 92 | Adafruit_CPlay_NeoPixel strip; ///< the neopixel strip object |
|---|
| 93 | Adafruit_CPlay_LIS3DH lis; ///< the accelerometer object |
|---|
| 94 | Adafruit_CPlay_Mic mic; ///< the microphone object |
|---|
| 95 | Adafruit_CPlay_Speaker speaker; ///< the speaker object |
|---|
| 96 | |
|---|
| 97 | #ifdef __AVR__ // Circuit Playground 'classic' |
|---|
| 98 | CPlay_CapacitiveSensor cap[8]; ///< the array of capacitive touch sensors |
|---|
| 99 | #else |
|---|
| 100 | Adafruit_CPlay_FreeTouch cap[7]; ///< the array of capacitive touch sensors |
|---|
| 101 | IRrecvPCI irReceiver; ///< the IR receiver object |
|---|
| 102 | IRdecode irDecoder; ///< the IR decoder object |
|---|
| 103 | IRsend irSend; ///< the IR send object |
|---|
| 104 | #endif |
|---|
| 105 | |
|---|
| 106 | bool slideSwitch(void); |
|---|
| 107 | void redLED(bool v); |
|---|
| 108 | void playTone(uint16_t freq, uint16_t time, bool wait=true); |
|---|
| 109 | bool leftButton(void); |
|---|
| 110 | bool rightButton(void); |
|---|
| 111 | uint16_t lightSensor(void); |
|---|
| 112 | int16_t soundSensor(void); |
|---|
| 113 | float temperature(void); |
|---|
| 114 | float temperatureF(void); |
|---|
| 115 | |
|---|
| 116 | uint16_t readCap(uint8_t p, uint8_t samples=10); |
|---|
| 117 | |
|---|
| 118 | // Accelerometer |
|---|
| 119 | float motionX(void); |
|---|
| 120 | float motionY(void); |
|---|
| 121 | float motionZ(void); |
|---|
| 122 | |
|---|
| 123 | /**************************************************************************/ |
|---|
| 124 | /*! |
|---|
| 125 | @brief set the range of the MEMS accelerometer. |
|---|
| 126 | @param range the range to set the accelerometer to. LIS3DH_RANGE_2_G |
|---|
| 127 | is the smallest (+-2G) but will give the greatest precision, while LIS3DH_RANGE_8_G |
|---|
| 128 | is the largest (+-8G) but with the lease precision. LIS3DH_RANGE_4_G is in the middle. |
|---|
| 129 | */ |
|---|
| 130 | /**************************************************************************/ |
|---|
| 131 | void setAccelRange(lis3dh_range_t range) { lis.setRange(range); } |
|---|
| 132 | /**************************************************************************/ |
|---|
| 133 | /*! |
|---|
| 134 | @brief turn on tap detection. Tap detection can detect single taps or 'double taps' |
|---|
| 135 | (like a double-click). |
|---|
| 136 | @param c If c is 1 you will only detect single taps, one at a time. |
|---|
| 137 | If c is 2, you will be able to detect both single taps and double taps. |
|---|
| 138 | @param clickthresh the threshold over which to register a tap |
|---|
| 139 | */ |
|---|
| 140 | /**************************************************************************/ |
|---|
| 141 | void setAccelTap(uint8_t c, uint8_t clickthresh) |
|---|
| 142 | { lis.setClick(c, clickthresh, 10, 20, 255); } |
|---|
| 143 | |
|---|
| 144 | /**************************************************************************/ |
|---|
| 145 | /*! |
|---|
| 146 | @brief test whether or not a tap has been detected |
|---|
| 147 | @return 0 if no tap is detected, 1 if a single tap is detected, and 2 or 3 if double tap is detected. |
|---|
| 148 | */ |
|---|
| 149 | /**************************************************************************/ |
|---|
| 150 | uint8_t getAccelTap(void) { return (lis.getClick() >> 8) & 0x3; } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /**************************************************************************/ |
|---|
| 154 | /*! |
|---|
| 155 | @brief turn off all neopixels on the board |
|---|
| 156 | */ |
|---|
| 157 | /**************************************************************************/ |
|---|
| 158 | void clearPixels(void) { strip.clear(); strip.show(); } |
|---|
| 159 | |
|---|
| 160 | /**************************************************************************/ |
|---|
| 161 | /*! |
|---|
| 162 | @brief set the color of a neopixel on the board |
|---|
| 163 | @param p the pixel to set. Pixel 0 is above the pad labeled 'GND' right next to the |
|---|
| 164 | USB connector, while pixel 9 is above the pad labeled '3.3V' on the other side of |
|---|
| 165 | the USB connector. |
|---|
| 166 | @param c a 24bit color value to set the pixel to |
|---|
| 167 | */ |
|---|
| 168 | /**************************************************************************/ |
|---|
| 169 | void setPixelColor(uint8_t p, uint32_t c) {strip.setPixelColor(p, c); strip.show();} |
|---|
| 170 | |
|---|
| 171 | /**************************************************************************/ |
|---|
| 172 | /*! |
|---|
| 173 | @brief set the color of a neopixel on the board |
|---|
| 174 | @param p the pixel to set. Pixel 0 is above the pad labeled 'GND' right next to the |
|---|
| 175 | USB connector, while pixel 9 is above the pad labeled '3.3V' on the other side of |
|---|
| 176 | the USB connector. |
|---|
| 177 | @param r a 0 to 255 value corresponding to the red component of the desired color. |
|---|
| 178 | @param g a 0 to 255 value corresponding to the green component of the desired color. |
|---|
| 179 | @param b a 0 to 255 value corresponding to the blue component of the desired color. |
|---|
| 180 | */ |
|---|
| 181 | /**************************************************************************/ |
|---|
| 182 | void setPixelColor(uint8_t p, uint8_t r, uint8_t g, uint8_t b) {strip.setPixelColor(p, r, g, b); strip.show();} |
|---|
| 183 | |
|---|
| 184 | /*! @brief set the global brightness of all neopixels. |
|---|
| 185 | @param b a 0 to 255 value corresponding to the desired brightness. The default brightness |
|---|
| 186 | of all neopixels is 30. */ |
|---|
| 187 | void setBrightness(uint16_t b){strip.setBrightness(b);} |
|---|
| 188 | |
|---|
| 189 | /*! @brief Get a sinusoidal value from a sine table |
|---|
| 190 | @param x a 0 to 255 value corresponding to an index to the sine table |
|---|
| 191 | @returns An 8-bit sinusoidal value back */ |
|---|
| 192 | uint8_t sine8(uint8_t x) { return strip.sine8(x); } |
|---|
| 193 | |
|---|
| 194 | /*! @brief Get a gamma-corrected value from a gamma table |
|---|
| 195 | @param x a 0 to 255 value corresponding to an index to the gamma table |
|---|
| 196 | @returns An 8-bit gamma-corrected value back */ |
|---|
| 197 | uint8_t gamma8(uint8_t x) { return strip.gamma8(x); } |
|---|
| 198 | |
|---|
| 199 | uint32_t colorWheel(uint8_t x); |
|---|
| 200 | |
|---|
| 201 | // Basic RGB color sensing with the light sensor and nearby neopixel. |
|---|
| 202 | // Both functions do the same thing and just differ in how they return the |
|---|
| 203 | // result, either as explicit RGB bytes or a 24-bit RGB color value. |
|---|
| 204 | void senseColor(uint8_t& red, uint8_t& green, uint8_t& blue); |
|---|
| 205 | |
|---|
| 206 | /**************************************************************************/ |
|---|
| 207 | /*! |
|---|
| 208 | @brief detect a color using the onboard light sensor |
|---|
| 209 | @return a 24 bit color. The most significant byte is red, followed by green, and |
|---|
| 210 | the least significant byte is blue. |
|---|
| 211 | */ |
|---|
| 212 | /**************************************************************************/ |
|---|
| 213 | uint32_t senseColor() { |
|---|
| 214 | // Use the individual color component color sense function and then recombine |
|---|
| 215 | // tbe components into a 24-bit color value. |
|---|
| 216 | uint8_t red, green, blue; |
|---|
| 217 | senseColor(red, green, blue); |
|---|
| 218 | return ((uint32_t)red << 16) | ((uint32_t)green << 8) | blue; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | bool isExpress(void); |
|---|
| 222 | |
|---|
| 223 | private: |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | }; |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | extern Adafruit_CircuitPlayground CircuitPlayground; ///< instantiated by default |
|---|
| 230 | |
|---|
| 231 | #endif |
|---|