source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Adafruit_CircuitPlayground/Adafruit_CircuitPlayground.cpp @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 6.0 KB
Line 
1#include <Adafruit_CircuitPlayground.h>
2
3
4boolean Adafruit_CircuitPlayground::begin(uint8_t brightness) {
5  pinMode(CPLAY_REDLED, OUTPUT);
6  pinMode(CPLAY_SLIDESWITCHPIN, INPUT);
7  pinMode(CPLAY_LEFTBUTTON, INPUT);
8  pinMode(CPLAY_RIGHTBUTTON, INPUT);
9  pinMode(CPLAY_BUZZER, OUTPUT);
10  pinMode(CPLAY_CAPSENSE_SHARED, OUTPUT);
11
12  strip = Adafruit_CPlay_NeoPixel(); 
13  strip.updateType(NEO_GRB + NEO_KHZ800);
14  strip.updateLength(10);
15  strip.setPin(CPLAY_NEOPIXELPIN);
16
17  lis = Adafruit_CPlay_LIS3DH(CPLAY_LIS3DH_CS);
18  mic = Adafruit_CPlay_Mic();
19
20  strip.begin();
21  strip.show(); // Initialize all pixels to 'off'
22  strip.setBrightness(brightness);
23
24  cap[0] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 0);
25  cap[1] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 1);
26  cap[2] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 2);
27  cap[3] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 3);
28  cap[4] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 6);
29  cap[5] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 9);
30  cap[6] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 10);
31  cap[7] = CPlay_CapacitiveSensor(CPLAY_CAPSENSE_SHARED, 12);
32
33  if (! lis.begin(0x18)) {   // change this to 0x19 for alternative i2c address
34    return false;
35  }
36  return true;
37}
38
39uint16_t Adafruit_CircuitPlayground::readCap(uint8_t p, uint8_t samples) {
40  switch (p) {
41  case 0:
42    return cap[0].capacitiveSensor(samples);
43  case 1:
44    return cap[1].capacitiveSensor(samples);
45  case 2:
46    return cap[2].capacitiveSensor(samples);
47  case 3:
48    return cap[3].capacitiveSensor(samples);
49  case 6:
50    return cap[4].capacitiveSensor(samples);
51  case 9:
52    return cap[5].capacitiveSensor(samples);
53  case 10:
54    return cap[6].capacitiveSensor(samples);
55  case 12:
56    return cap[7].capacitiveSensor(samples);
57  default:
58    return 0;
59  }
60}
61
62
63// just turn on/off the red #13 LED
64void Adafruit_CircuitPlayground::redLED(boolean v) {
65  digitalWrite(CPLAY_REDLED, v);
66}
67
68// just read the slide switch
69boolean Adafruit_CircuitPlayground::slideSwitch(void) {
70  return digitalRead(CPLAY_SLIDESWITCHPIN);
71}
72
73// just read the left button
74boolean Adafruit_CircuitPlayground::leftButton(void) {
75  return digitalRead(CPLAY_LEFTBUTTON);
76}
77
78// just read the right button
79boolean Adafruit_CircuitPlayground::rightButton(void) {
80  return digitalRead(CPLAY_RIGHTBUTTON);
81}
82
83void Adafruit_CircuitPlayground::playTone(uint16_t freq, uint16_t time, boolean wait) {
84  tone(CPLAY_BUZZER, freq, time);
85  if (wait) delay(time);
86}
87
88uint16_t Adafruit_CircuitPlayground::lightSensor(void) {
89  return analogRead(CPLAY_LIGHTSENSOR);
90}
91
92uint16_t Adafruit_CircuitPlayground::soundSensor(void) {
93  return analogRead(CPLAY_SOUNDSENSOR);
94}
95
96
97float Adafruit_CircuitPlayground::motionX(void) {
98  sensors_event_t event;
99  CircuitPlayground.lis.getEvent(&event);
100  return event.acceleration.x;
101}
102
103float Adafruit_CircuitPlayground::motionY(void) {
104  sensors_event_t event;
105  CircuitPlayground.lis.getEvent(&event);
106  return event.acceleration.y;
107}
108
109float Adafruit_CircuitPlayground::motionZ(void) {
110  sensors_event_t event;
111  CircuitPlayground.lis.getEvent(&event);
112  return event.acceleration.z;
113}
114
115float Adafruit_CircuitPlayground::temperature(void) {
116   // Thermistor test
117  float reading;
118
119  reading = analogRead(CPLAY_THERMISTORPIN);
120
121  //Serial.print("Thermistor reading: "); Serial.println(reading);
122
123  // convert the value to resistance
124  reading = ((1023.0 * SERIESRESISTOR) / reading);
125  reading -= SERIESRESISTOR;
126
127  //Serial.print("Thermistor resistance: "); Serial.println(reading);
128
129  float steinhart;
130  steinhart = reading / THERMISTORNOMINAL;     // (R/Ro)
131  steinhart = log(steinhart);                  // ln(R/Ro)
132  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
133  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
134  steinhart = 1.0 / steinhart;                 // Invert
135  steinhart -= 273.15;                         // convert to C
136
137  return steinhart;
138}
139
140// Get the temperature in degrees Fahrenheit
141float Adafruit_CircuitPlayground::temperatureF(void) {
142  float tempF = CircuitPlayground.temperature() * 1.8 + 32;
143  return tempF;
144}
145
146
147// Input a value 0 to 255 to get a color value.
148// The colours are a transition r - g - b - back to r.
149uint32_t Adafruit_CircuitPlayground::colorWheel(uint8_t WheelPos) {
150  WheelPos = 255 - WheelPos;
151  if (WheelPos < 85) {
152    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
153  }
154  if (WheelPos < 170) {
155    WheelPos -= 85;
156    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
157  }
158  WheelPos -= 170;
159  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
160}
161
162void Adafruit_CircuitPlayground::senseColor(uint8_t& red, uint8_t& green, uint8_t& blue) {
163  // Save the current pixel brightness so it can later be restored.  Then bump
164  // the brightness to max to make sure the LED is as bright as possible for
165  // the color readings.
166  uint8_t old_brightness = strip.getBrightness();
167  strip.setBrightness(255);
168  // Set pixel 1 (next to the light sensor) to full red, green, blue
169  // color and grab a light sensor reading.  Make sure to wait a bit
170  // after changing pixel colors to let the light sensor change
171  // resistance!
172  setPixelColor(1, 255, 0, 0);  // Red
173  delay(LIGHT_SETTLE_MS);
174  uint16_t raw_red = lightSensor();
175  setPixelColor(1, 0, 255, 0);  // Green
176  delay(LIGHT_SETTLE_MS);
177  uint16_t raw_green = lightSensor();
178  setPixelColor(1, 0, 0, 255);  // Blue
179  delay(LIGHT_SETTLE_MS);
180  uint16_t raw_blue = lightSensor();
181  // Turn off the pixel and restore brightness, we're done with readings.
182  setPixelColor(1, 0);
183  strip.setBrightness(old_brightness);
184  // Now scale down each of the raw readings to be within
185  // 0 to 255.  Remember each sensor reading is from the ADC
186  // which has 10 bits of resolution (0 to 1023), so dividing
187  // by 4 will change the range from 0-1023 to 0-255.  Also
188  // use the min function to clamp the value to 255 at most (just
189  // to prevent overflow from 255.xx to 0).
190  red = min(255, raw_red/4);
191  green = min(255, raw_green/4);
192  blue = min(255, raw_blue/4);
193}
194
195// instantiate static
196Adafruit_CircuitPlayground CircuitPlayground;
Note: See TracBrowser for help on using the repository browser.