source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/Adafruit_CircuitPlayground.cpp @ 7

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

Setup project structure

File size: 14.7 KB
Line 
1/*!
2 * @file Adafruit_CircuitPlayground.cpp
3 *
4 * @mainpage Adafruit CircuitPlayground Library
5 *
6 * @section intro_sec Introduction
7 *
8 * This is the documentation for Adafruit's CircuitPlayground driver for the
9 * Arduino platform.  It is designed specifically to work with the
10 * Adafruit CircuitPlayground boards:
11 *  - https://www.adafruit.com/products/3000
12 *  - https://www.adafruit.com/products/3333
13 *
14 *
15 * Adafruit invests time and resources providing this open source code,
16 * please support Adafruit and open-source hardware by purchasing
17 * products from Adafruit!
18 *
19 *
20 * @section author Author
21 *
22 * Written by Ladyada and others for Adafruit Industries.
23 *
24 * @section license License
25 *
26 * BSD license, all text here must be included in any redistribution.
27 *
28 */
29
30#include <Adafruit_Circuit_Playground.h>
31
32/**************************************************************************/
33/*!
34    @brief  Set up the CircuitPlayground hardware
35    @param  brightness Optional brightness to set the neopixels to
36    @returns True if device is set up, false on any failure
37*/
38/**************************************************************************/
39bool Adafruit_CircuitPlayground::begin(uint8_t brightness) {
40  pinMode(CPLAY_REDLED, OUTPUT);
41  pinMode(CPLAY_BUZZER, OUTPUT);
42#ifdef __AVR__
43  pinMode(CPLAY_CAPSENSE_SHARED, OUTPUT);
44  pinMode(CPLAY_LEFTBUTTON, INPUT);
45  pinMode(CPLAY_RIGHTBUTTON, INPUT);
46  pinMode(CPLAY_SLIDESWITCHPIN, INPUT);
47#else // Circuit Playground Express
48  pinMode(CPLAY_LEFTBUTTON, INPUT_PULLDOWN);
49  pinMode(CPLAY_RIGHTBUTTON, INPUT_PULLDOWN);
50  pinMode(CPLAY_SLIDESWITCHPIN, INPUT_PULLUP);
51  irReceiver=IRrecvPCI(CPLAY_IR_RECEIVER);
52  irDecoder=IRdecode();
53#endif
54
55
56  strip = Adafruit_CPlay_NeoPixel();
57  strip.updateType(NEO_GRB + NEO_KHZ800);
58  strip.updateLength(10);
59  strip.setPin(CPLAY_NEOPIXELPIN);
60
61  lis = Adafruit_CPlay_LIS3DH(CPLAY_LIS3DH_CS);
62  mic = Adafruit_CPlay_Mic();
63
64  strip.begin();
65  strip.show(); // Initialize all pixels to 'off'
66  strip.setBrightness(brightness);
67
68#ifdef __AVR__
69  cap[0] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 0);
70  cap[1] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 1);
71  cap[2] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 2);
72  cap[3] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 3);
73  cap[4] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 6);
74  cap[5] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 9);
75  cap[6] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 10);
76  cap[7] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 12);
77#else // Circuit Playground Express // Circuit Playground Express
78  for(int i=0; i<7; i++) {
79    cap[i] = Adafruit_CPlay_FreeTouch(A1+i, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
80    if (! cap[i].begin()) return false;
81  }
82#endif
83
84  return lis.begin(CPLAY_LIS3DH_ADDRESS);
85}
86
87/**************************************************************************/
88/*!
89    @brief  read capacitive touch sensor
90    @param  p the pin to read. Must be a captouch enabled pin.
91    @param  samples Optional number of samples to take. Defaults to 10.
92    @returns measured captouch value
93*/
94/**************************************************************************/
95uint16_t Adafruit_CircuitPlayground::readCap(uint8_t p, uint8_t samples) {
96#ifdef __AVR__  // Circuit Playground Classic
97  switch (p) {
98    case 0:    return cap[0].capacitiveSensor(samples);
99    case 1:    return cap[1].capacitiveSensor(samples);
100    case 2:    return cap[2].capacitiveSensor(samples);
101    case 3:    return cap[3].capacitiveSensor(samples);
102    case 6:    return cap[4].capacitiveSensor(samples);
103    case 9:    return cap[5].capacitiveSensor(samples);
104    case 10:   return cap[6].capacitiveSensor(samples);
105    case 12:   return cap[7].capacitiveSensor(samples);
106    default:   return 0;
107  }
108#else // Circuit Playground Express // Circuit Playground Express
109  // analog pins r ez!
110  if ((p >= A1) && (p <= A7)) {
111    return cap[p - A1].measure();
112  }
113  // oof digital pins
114  switch (p) {
115    case 0:    return cap[A6 - A1].measure();
116    case 1:    return cap[A7 - A1].measure();
117    case 2:    return cap[A5 - A1].measure();
118    case 3:    return cap[A4 - A1].measure();
119    case 6:    return cap[A1 - A1].measure();
120    case 9:    return cap[A2 - A1].measure();
121    case 10:   return cap[A3 - A1].measure();
122    default:   return 0;
123  }
124#endif
125}
126
127/**************************************************************************/
128/*!
129    @brief turn on or off the red LED on pin #13
130    @param  v pass true to turn LED on, false to turn LED off
131*/
132/**************************************************************************/
133void Adafruit_CircuitPlayground::redLED(bool v) {
134  digitalWrite(CPLAY_REDLED, v);
135}
136
137/**************************************************************************/
138/*!
139    @brief read the slide switch
140    @returns true if slide switch in set, false if not
141*/
142/**************************************************************************/
143bool Adafruit_CircuitPlayground::slideSwitch(void) {
144  return digitalRead(CPLAY_SLIDESWITCHPIN);
145}
146
147/**************************************************************************/
148/*!
149    @brief read the left button
150    @returns true if button is pressed, false if not
151*/
152/**************************************************************************/
153bool Adafruit_CircuitPlayground::leftButton(void) {
154  return digitalRead(CPLAY_LEFTBUTTON);
155}
156
157/**************************************************************************/
158/*!
159    @brief read the right button
160    @returns true if button is pressed, false if not
161*/
162/**************************************************************************/
163bool Adafruit_CircuitPlayground::rightButton(void) {
164  return digitalRead(CPLAY_RIGHTBUTTON);
165}
166
167/**************************************************************************/
168/*!
169    @brief play a tone on the onboard buzzer
170    @param  freq the frequency to play
171    @param  time the duration of the tone in milliseconds
172    @param  wait Optional flag to wait for time milliseconds after playing the tone. Defaults to true.
173    @note The driver circuitry is an on/off transistor driver, so you will only be able to play square waves.
174    It is also not the same loudness over all frequencies but is designed to be the loudest at around 4 KHz
175*/
176/**************************************************************************/
177void Adafruit_CircuitPlayground::playTone(
178  uint16_t freq, uint16_t time, bool wait) {
179#ifdef __AVR__
180#define F_PLL 48000000
181  if(!freq) return;
182  uint32_t ocr;
183  uint16_t prescale  = 1;
184  uint8_t  scalebits = 0;
185  uint8_t  hi1, lo1, hi2, lo2;
186
187  // Determine best prescaler setting for 10-bit timer
188  do {
189    scalebits++;
190    ocr       = F_PLL / freq / prescale - 1;
191    prescale *= 2;
192    if(prescale >= 16384) {
193      ocr       = 1023;
194      scalebits = 0b1111;
195    }
196  } while(ocr > 1023);
197
198  // Set up Timer4 for fast PWM on !OC4A
199  PLLFRQ  = (PLLFRQ & 0xCF) | 0x30;   // Route PLL to async clk
200  TCCR4A  = _BV(COM4A0) | _BV(PWM4A); // Clear on match, PWMA on
201  TCCR4B  = _BV(PWM4X)  | scalebits;  // PWM invert
202  TCCR4D  = 0;                        // Fast PWM mode
203  TCCR4E  = 0;                        // Not enhanced mode
204  DT4     = 0;                        // No dead time
205  hi1     = ocr >> 8;
206  lo1     = ocr & 0xFF;
207  hi2     = ocr >> 9;
208  lo2     = (ocr >> 1) & 0xFF;
209  noInterrupts();                     // TC4H accesses MUST be atomic
210  TC4H    = hi1;     
211  OCR4C   = lo1;                      // TOP
212  TC4H    = hi2;     
213  OCR4A   = lo2;                      // 50% duty
214  interrupts();
215  pinMode(5, OUTPUT);                 // Enable output
216  delay(time);
217  pinMode(5, INPUT);                  // Disable output
218  TCCR4A  = 0;                        // PWMA off
219#else
220  tone(CPLAY_BUZZER, freq, time);
221  delay(time); // time argument to tone() isn't working, so...
222#endif
223  if(wait) delay(time);
224}
225
226/**************************************************************************/
227/*!
228    @brief read the onboard lightsensor
229    @returns value between 0 and 1023 read from the light sensor
230    @note 1000 Lux will roughly read as 2 Volts (or about 680 as a raw analog reading).
231      A reading of about 300 is common for most indoor light levels.
232      Note that outdoor daylight is 10,000 Lux or even higher, so this sensor is best
233      suited for indoor light levels!
234*/
235/**************************************************************************/
236uint16_t Adafruit_CircuitPlayground::lightSensor(void) {
237  return analogRead(CPLAY_LIGHTSENSOR);
238}
239
240/**************************************************************************/
241/*!
242    @brief read the onboard sound sensor. A reading of ~0 is silent, and
243    loud audio will result in a reading between -500 and 500 or so.
244    @returns value of the sound sensor
245*/
246/**************************************************************************/
247int16_t Adafruit_CircuitPlayground::soundSensor(void) {
248  int16_t x;
249  mic.capture(&x, 1);
250  return x;
251}
252
253/**************************************************************************/
254/*!
255    @brief read the X parameter of the onboard accelerometer. Value returned is
256     defined by setAccelRange().
257    @returns X value of the accelerometer
258*/
259/**************************************************************************/
260float Adafruit_CircuitPlayground::motionX(void) {
261  sensors_event_t event;
262  CircuitPlayground.lis.getEvent(&event);
263  return event.acceleration.x;
264}
265
266/**************************************************************************/
267/*!
268    @brief read the Y parameter of the onboard accelerometer. Value returned is
269     defined by setAccelRange().
270    @returns Y value of the accelerometer
271*/
272/**************************************************************************/
273float Adafruit_CircuitPlayground::motionY(void) {
274  sensors_event_t event;
275  CircuitPlayground.lis.getEvent(&event);
276  return event.acceleration.y;
277}
278
279/**************************************************************************/
280/*!
281    @brief read the Z parameter of the onboard accelerometer. Value returned is
282     defined by setAccelRange().
283    @returns the Z value of the onboard accelerometer
284*/
285/**************************************************************************/
286float Adafruit_CircuitPlayground::motionZ(void) {
287  sensors_event_t event;
288  CircuitPlayground.lis.getEvent(&event);
289  return event.acceleration.z;
290}
291
292/**************************************************************************/
293/*!
294    @brief read the onboard thermistor.
295    @returns temperature reading in Centigrade.
296*/
297/**************************************************************************/
298float Adafruit_CircuitPlayground::temperature(void) {
299   // Thermistor test
300  float reading;
301
302  reading = analogRead(CPLAY_THERMISTORPIN);
303
304  //Serial.print("Thermistor reading: "); Serial.println(reading);
305
306  // convert the value to resistance
307  reading = ((1023.0 * SERIESRESISTOR) / reading);
308  reading -= SERIESRESISTOR;
309
310  //Serial.print("Thermistor resistance: "); Serial.println(reading);
311
312  float steinhart;
313  steinhart = reading / THERMISTORNOMINAL;     // (R/Ro)
314  steinhart = log(steinhart);                  // ln(R/Ro)
315  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
316  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
317  steinhart = 1.0 / steinhart;                 // Invert
318  steinhart -= 273.15;                         // convert to C
319
320  return steinhart;
321}
322
323/**************************************************************************/
324/*!
325    @brief read the onboard thermistor.
326    @returns temperature reading in Farenheight.
327*/
328/**************************************************************************/
329float Adafruit_CircuitPlayground::temperatureF(void) {
330  float tempF = CircuitPlayground.temperature() * 1.8 + 32;
331  return tempF;
332}
333
334/**************************************************************************/
335/*!
336    @brief get a color value from the color wheel.
337    @param WheelPos a value 0 to 255
338    @returns a color value. The colours are a transition r - g - b - back to r.
339*/
340/**************************************************************************/
341uint32_t Adafruit_CircuitPlayground::colorWheel(uint8_t WheelPos) {
342  WheelPos = 255 - WheelPos;
343  if (WheelPos < 85) {
344    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
345  }
346  if (WheelPos < 170) {
347    WheelPos -= 85;
348    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
349  }
350  WheelPos -= 170;
351  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
352}
353
354/**************************************************************************/
355/*!
356    @brief detect a color value from the light sensor
357    @param red the pointer to where the red component should be stored.
358    @param green the pointer to where the green component should be stored.
359    @param blue the pointer to where the blue component should be stored.   
360    @returns the components of the detected colors in the passed pointers.
361*/
362/**************************************************************************/
363void Adafruit_CircuitPlayground::senseColor(uint8_t& red, uint8_t& green, uint8_t& blue) {
364  // Save the current pixel brightness so it can later be restored.  Then bump
365  // the brightness to max to make sure the LED is as bright as possible for
366  // the color readings.
367  uint8_t old_brightness = strip.getBrightness();
368  strip.setBrightness(255);
369  // Set pixel 1 (next to the light sensor) to full red, green, blue
370  // color and grab a light sensor reading.  Make sure to wait a bit
371  // after changing pixel colors to let the light sensor change
372  // resistance!
373  setPixelColor(1, 255, 0, 0);  // Red
374  delay(LIGHT_SETTLE_MS);
375  uint16_t raw_red = lightSensor();
376  setPixelColor(1, 0, 255, 0);  // Green
377  delay(LIGHT_SETTLE_MS);
378  uint16_t raw_green = lightSensor();
379  setPixelColor(1, 0, 0, 255);  // Blue
380  delay(LIGHT_SETTLE_MS);
381  uint16_t raw_blue = lightSensor();
382  // Turn off the pixel and restore brightness, we're done with readings.
383  setPixelColor(1, 0);
384  strip.setBrightness(old_brightness);
385  // Now scale down each of the raw readings to be within
386  // 0 to 255.  Remember each sensor reading is from the ADC
387  // which has 10 bits of resolution (0 to 1023), so dividing
388  // by 4 will change the range from 0-1023 to 0-255.  Also
389  // use the min function to clamp the value to 255 at most (just
390  // to prevent overflow from 255.xx to 0).
391  red = min(255, raw_red/4);
392  green = min(255, raw_green/4);
393  blue = min(255, raw_blue/4);
394}
395
396/**************************************************************************/
397/*!
398    @brief check whether or not this device is a CircuitPlayground Express.
399    @returns True if the device is a CircuitPlayground Express, false if it is a 'classic'.
400*/
401/**************************************************************************/
402bool Adafruit_CircuitPlayground::isExpress(void) {
403#ifdef __AVR__
404  return false;
405#else
406  return true;
407#endif
408}
409
410// instantiate static
411Adafruit_CircuitPlayground CircuitPlayground;
Note: See TracBrowser for help on using the repository browser.