source: rtos_arduino/trunk/arduino_lib/libraries/Esplora/examples/Experts/EsploraTable/EsploraTable.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 5.9 KB
Line 
1/*
2 Esplora Table
3
4 Acts like a keyboard that prints sensor
5 data in a table-like text, row by row.
6
7 At startup, it does nothing. It waits for you to open a
8 spreadsheet (e.g. Google Drive spreadsheet) so it can write
9 data. By pressing Switch 1, it starts printing the table
10 headers and the first row of data. It waits a bit, then it
11 will print another row, and so on.
12
13 The amount of time between each row is determined by the slider.
14 If put to full left, the sketch will wait 10 seconds; at
15 full right position, it will wait 5 minutes. An intermediate
16 position will make the sketch wait for some time in-between.
17
18 Clicking the Switch 1 at any time will stop the logging.
19
20 The color LED shows what the sketch is doing:
21 blue = idle, waiting for you to press Switch 1 to start logging
22 green = active; will print soon
23 red = printing data to the PC
24
25 Created on 22 november 2012
26 By Enrico Gueli <enrico.gueli@gmail.com>
27 modified 24 Nov 2012
28 by Tom Igoe
29*/
30
31#include <Esplora.h>
32
33/*
34 * this variable tells if the data-logging is currently active.
35 */
36boolean active = false;
37
38/*
39 * this variable holds the time in the future when the sketch
40 * will "sample" the data (sampling is the act of reading some
41 * input at a known time). This variable is checked continuously
42 * against millis() to know when it's time to sample.
43 */
44unsigned long nextSampleAt = 0;
45
46/*
47 * This variable just holds the millis() value at the time the
48 * logging was activated. This is needed to enter the correct
49 * value in the "Time" column in the printed table.
50 */
51unsigned long startedAt = 0;
52
53
54/*
55 * when the "active" variable is set to true, the same is done
56 * with this variable. This is needed because the code that does
57 * the "just-after-activation" stuff is run some time later than
58 * the code that says "be active now".
59 */
60boolean justActivated = false;
61
62
63/*
64 * this variable holds the last sensed status of the switch press
65 * button. If the code sees a difference between the value of
66 * this variable and the current status of the switch, it means
67 * that the button was either pressed or released.
68 */
69boolean lastStartBtn = HIGH;
70
71/*
72 * Initialization code. The virtual USB keyboard must be
73 * initialized; the Serial class is needed just for debugging.
74 */
75void setup() {
76 Keyboard.begin();
77 Serial.begin(9600);
78}
79
80/*
81 * This code is run continuously.
82 */
83void loop() {
84 /*
85 * note: we don't use Arduino's delay() here, because we can't
86 * normally do anything while delaying. Our own version lets us
87 * check for button presses often enough to not miss any event.
88 */
89 activeDelay(50);
90
91 /*
92 * the justActivated variable may be set to true in the
93 * checkSwitchPress() function. Here we check its status to
94 * print the table headers and configure what's needed to.
95 */
96 if (justActivated == true) {
97 justActivated = false; // do this just once
98 printHeaders();
99 // do next sampling ASAP
100 nextSampleAt = startedAt = millis();
101 }
102
103 if (active == true) {
104 if (nextSampleAt < millis()) {
105 // it's time to sample!
106 int slider = Esplora.readSlider();
107 // the row below maps the slider position to a range between
108 // 10 and 290 seconds.
109 int sampleInterval = map(slider, 0, 1023, 10, 290);
110 nextSampleAt = millis() + sampleInterval * 1000;
111
112 logAndPrint();
113 }
114
115 // let the RGB led blink green once per second, for 200ms.
116 unsigned int ms = millis() % 1000;
117 if (ms < 200)
118 Esplora.writeGreen(50);
119 else
120 Esplora.writeGreen(0);
121
122 Esplora.writeBlue(0);
123 }
124 else
125 // while not active, keep a reassuring blue color coming
126 // from the Esplora...
127 Esplora.writeBlue(20);
128
129}
130
131/*
132 * Print the table headers.
133 */
134void printHeaders() {
135 Keyboard.print("Time");
136 Keyboard.write(KEY_TAB);
137 activeDelay(300); // Some spreadsheets are slow, e.g. Google
138 // Drive that wants to save every edit.
139 Keyboard.print("Accel X");
140 Keyboard.write(KEY_TAB);
141 activeDelay(300);
142 Keyboard.print("Accel Y");
143 Keyboard.write(KEY_TAB);
144 activeDelay(300);
145 Keyboard.print("Accel Z");
146 Keyboard.println();
147 activeDelay(300);
148}
149
150void logAndPrint() {
151 // do all the samplings at once, because keystrokes have delays
152 unsigned long timeSecs = (millis() - startedAt) / 1000;
153 int xAxis = Esplora.readAccelerometer(X_AXIS);
154 int yAxis = Esplora.readAccelerometer(Y_AXIS);
155 int zAxis = Esplora.readAccelerometer(Z_AXIS);
156
157 Esplora.writeRed(100);
158
159 Keyboard.print(timeSecs);
160 Keyboard.write(KEY_TAB);
161 activeDelay(300);
162 Keyboard.print(xAxis);
163 Keyboard.write(KEY_TAB);
164 activeDelay(300);
165 Keyboard.print(yAxis);
166 Keyboard.write(KEY_TAB);
167 activeDelay(300);
168 Keyboard.print(zAxis);
169 Keyboard.println();
170 activeDelay(300);
171 Keyboard.write(KEY_HOME);
172
173 Esplora.writeRed(0);
174}
175
176/**
177 * Similar to delay(), but allows the program to do something else
178 * in the meanwhile. In particular, it calls checkSwitchPress().
179 * Note 1: it may wait longer than the specified amount, not less;
180 * Note 2: beware of data synchronization issues, e.g. if the
181 * activeDelay() function alters some variables used by the
182 * caller of this function.
183 */
184void activeDelay(unsigned long amount) {
185 unsigned long at = millis() + amount;
186 while (millis() < at) {
187 checkSwitchPress();
188 }
189}
190
191/*
192 * This function reads the status of the switch; if it sees that
193 * it was pressed, toggles the status of the "active" variable.
194 * If it's set to true, also the justActivated variable is set to
195 * true, so the loop() function above can do the right things.
196 * This function should be called as often as possible and do as
197 * little as possible, because it can be called while another
198 * function is running.
199 */
200void checkSwitchPress() {
201 boolean startBtn = Esplora.readButton(SWITCH_DOWN);
202
203 if (startBtn != lastStartBtn) {
204 if (startBtn == HIGH) { // button released
205 active = !active;
206 if (active)
207 justActivated = true;
208 }
209
210 lastStartBtn = startBtn;
211 }
212}
213
Note: See TracBrowser for help on using the repository browser.