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

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

Update Arduino-IDE to 1.8.10

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