source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/Adafruit_Circuit_Playground.h @ 32

Last change on this file since 32 was 30, checked in by markus, 7 years ago

Update Arduino-IDE to 1.8.10

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