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

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

Updated Arduino libraries

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