source: rtos_arduino/trunk/examples/CompositeExample/r2ca_app.cpp@ 260

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

マクロ名を更新.
実行モデルを変更.

File size: 25.5 KB
Line 
1#include "r2ca.h"
2#include "i2c_lcd.h"
3
4#define LED1_PORT 2
5#define LED2_PORT 3
6
7#define SSID "XXXXX"
8#define PASSWORD "YYYYY"
9
10#define AP_SSID "M0_AP"
11#define AP_PASSWORD "none"
12
13//#define Serial SerialUSB
14
15/////////////////////////////////////////////////////////////////////
16// Inter Task Variable
17/////////////////////////////////////////////////////////////////////
18#define LED_MODE_ONOFF 0
19#define LED_MODE_BLINK 1
20#define LED_BLINK_CYC_INIT 500
21
22bool gled1_state = false;
23bool gled2_state = false;
24uint8_t led_mode = LED_MODE_ONOFF;
25uint32_t led_blink_cyc = LED_BLINK_CYC_INIT;
26
27/////////////////////////////////////////////////////////////////////
28// Command Task
29/////////////////////////////////////////////////////////////////////
30const char *usage = " \
311 : Select Sensor Task.\n\
322 : Select Web Task. \n\
333 : Select LED Task. \n\
344 : Select TFT Task. \n\
355 : Select Processing Task. \n\
36";
37
38extern void task1_setup();
39extern void task2_setup();
40extern void task3_setup();
41extern void task4_setup();
42extern void task5_setup();
43
44void setup(){
45 Serial.begin(115200);
46 while(!Serial){
47 delay(1);
48 }
49 Serial.println("Main Task : setup start!");
50 Serial.println(usage);
51
52 task1_setup();
53 task2_setup();
54 task3_setup();
55 task4_setup();
56 task5_setup();
57}
58
59
60/*
61 * ƒT[ƒrƒXƒR[ƒ‹‚̃Gƒ‰[‚̃ƒOo—Í
62 */
63Inline void
64svc_perror(const char *file, int_t line, const char *expr, ER ercd)
65{
66 if (ercd < 0) {
67 t_perror(LOG_ERROR, file, line, expr, ercd);
68 }
69}
70
71#define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
72
73ID tskid = R2CA_TASK1;
74int_t tskno = 1;
75
76#define HIGH_PRIORITY 4
77#define MID_PRIORITY 5
78#define LOW_PRIORITY 6
79
80void loop(){
81 char c;
82 ER_UINT ercd;
83 PRI tskpri;
84 SYSUTM utime1, utime2;
85
86 if (Serial.available()){
87 c = Serial.read();
88 switch (c) {
89 case '1':
90 tskno = 1;
91 tskid = R2CA_TASK1;
92 syslog(LOG_INFO, "Select Sensor Task.");
93 break;
94 case '2':
95 tskno = 2;
96 tskid = R2CA_TASK2;
97 syslog(LOG_INFO, "Select Web Task.");
98 break;
99 case '3':
100 tskno = 3;
101 tskid = R2CA_TASK3;
102 syslog(LOG_INFO, "Select LED Task.");
103 break;
104 case '4':
105 tskno = 4;
106 tskid = R2CA_TASK4;
107 syslog(LOG_INFO, "Select TFT Task.");
108 break;
109 case '5':
110 tskno = 5;
111 tskid = R2CA_TASK5;
112 syslog(LOG_INFO, "Select Processing Task.");
113 break;
114 case 'a':
115 syslog(LOG_INFO, "#act_tsk(%d)", tskno);
116 SVC_PERROR(act_tsk(tskid));
117 break;
118 case 'A':
119 syslog(LOG_INFO, "#can_act(%d)", tskno);
120 SVC_PERROR(ercd = can_act(tskid));
121 if (ercd >= 0) {
122 syslog(LOG_NOTICE, "can_act(%d) returns %d", tskno, ercd);
123 }
124 break;
125 case 't':
126 syslog(LOG_INFO, "#ter_tsk(%d)", tskno);
127 SVC_PERROR(ter_tsk(tskid));
128 break;
129 case '>':
130 syslog(LOG_INFO, "#chg_pri(%d, HIGH_PRIORITY)", tskno);
131 SVC_PERROR(chg_pri(tskid, HIGH_PRIORITY));
132 break;
133 case '=':
134 syslog(LOG_INFO, "#chg_pri(%d, MID_PRIORITY)", tskno);
135 SVC_PERROR(chg_pri(tskid, MID_PRIORITY));
136 break;
137 case '<':
138 syslog(LOG_INFO, "#chg_pri(%d, LOW_PRIORITY)", tskno);
139 SVC_PERROR(chg_pri(tskid, LOW_PRIORITY));
140 break;
141 case 'G':
142 syslog(LOG_INFO, "#get_pri(%d, &tskpri)", tskno);
143 SVC_PERROR(ercd = get_pri(tskid, &tskpri));
144 if (ercd >= 0) {
145 syslog(LOG_NOTICE, "priority of task %d is %d", tskno, tskpri);
146 }
147 break;
148 case 'w':
149 syslog(LOG_INFO, "#wup_tsk(%d)", tskno);
150 SVC_PERROR(wup_tsk(tskid));
151 break;
152 case 'W':
153 syslog(LOG_INFO, "#can_wup(%d)", tskno);
154 SVC_PERROR(ercd = can_wup(tskid));
155 if (ercd >= 0) {
156 syslog(LOG_NOTICE, "can_wup(%d) returns %d", tskno, ercd);
157 }
158 break;
159 case 'l':
160 syslog(LOG_INFO, "#rel_wai(%d)", tskno);
161 SVC_PERROR(rel_wai(tskid));
162 break;
163 case 'u':
164 syslog(LOG_INFO, "#sus_tsk(%d)", tskno);
165 SVC_PERROR(sus_tsk(tskid));
166 break;
167 case 'm':
168 syslog(LOG_INFO, "#rsm_tsk(%d)", tskno);
169 SVC_PERROR(rsm_tsk(tskid));
170 break;
171 case 'x':
172 syslog(LOG_INFO, "#ras_tex(%d, 0x0001U)", tskno);
173 SVC_PERROR(ras_tex(tskid, 0x0001U));
174 break;
175 case 'X':
176 syslog(LOG_INFO, "#ras_tex(%d, 0x0002U)", tskno);
177 SVC_PERROR(ras_tex(tskid, 0x0002U));
178 break;
179 case 'r':
180 syslog(LOG_INFO, "#rot_rdq(three priorities)");
181 SVC_PERROR(rot_rdq(HIGH_PRIORITY));
182 SVC_PERROR(rot_rdq(MID_PRIORITY));
183 SVC_PERROR(rot_rdq(LOW_PRIORITY));
184 break;
185 case 'V':
186 SVC_PERROR(get_utm(&utime1));
187 SVC_PERROR(get_utm(&utime2));
188 syslog(LOG_NOTICE, "utime1 = %ld, utime2 = %ld",
189 (ulong_t) utime1, (ulong_t) utime2);
190 break;
191 }
192 }
193 delay(10);
194}
195
196/////////////////////////////////////////////////////////////////////
197//
198// Sensor Task
199//
200/////////////////////////////////////////////////////////////////////
201//#define SENSOR_DEBUG_PRINT
202
203float tmp007_objt;
204float tmp007_diet;
205float tsl2591_light;
206uint16_t vcnl4000_proximity;
207
208#include <Wire.h>
209#include <Adafruit_Sensor.h>
210
211#define USE_TMP007
212#define USE_TSL2591
213#define USE_VCNL4000
214#define USE_I2CLCD
215
216#ifdef USE_TMP007
217#include "Adafruit_TMP007.h"
218Adafruit_TMP007 tmp007;
219#endif /* USE_TMP007 */
220
221#ifdef USE_TSL2591
222#include "Adafruit_TSL2591.h"
223Adafruit_TSL2591 tsl2591(2591);
224#endif /* USE_TSL2591 */
225
226#ifdef USE_VCNL4000
227#include "Adafruit_VCNL4000.h"
228Adafruit_VCNL4000 vcnl4000;
229#endif /* USE_VCNL4000 */
230
231#define SENSOR_DELAY_MS 10
232
233#define TMP007_CYCLE_MS 1000
234uint32_t tmp007_cycle;
235
236#define TSL2591_CYCLE_MS 100
237uint32_t tsl2591_cycle;
238
239#define VCNL4000_CYCLE_MS 100
240uint32_t vcnl4000_cycle;
241
242#define I2CLCD_CYCLE_MS 100
243uint32_t i2clcd_cycle;
244
245uint32_t led_cycle;
246
247#ifdef USE_TSL2591
248/**************************************************************************/
249/*
250 Displays some basic information on this sensor from the unified
251 sensor API sensor_t type (see Adafruit_Sensor for more information)
252*/
253/**************************************************************************/
254void tsl2591_displaySensorDetails(void)
255{
256 sensor_t sensor;
257 tsl2591.getSensor(&sensor);
258 Serial.println("------------------------------------");
259 Serial.print ("Sensor: "); Serial.println(sensor.name);
260 Serial.print ("Driver Ver: "); Serial.println(sensor.version);
261 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
262 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
263 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
264 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
265 Serial.println("------------------------------------");
266 Serial.println("");
267 delay(500);
268}
269
270/**************************************************************************/
271/*
272 Configures the gain and integration time for the TSL2591
273*/
274/**************************************************************************/
275void tsl2591_configureSensor(void)
276{
277 // You can change the gain on the fly, to adapt to brighter/dimmer light situations
278 //tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
279 tsl2591.setGain(TSL2591_GAIN_MED); // 25x gain
280 //tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
281
282 // Changing the integration time gives you a longer time over which to sense light
283 // longer timelines are slower, but are good in very low light situtations!
284 tsl2591.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
285 //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
286 //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
287 //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
288 //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
289 //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
290
291 /* Display the gain and integration time for reference sake */
292 Serial.println("------------------------------------");
293 Serial.print ("Gain: ");
294 tsl2591Gain_t gain = tsl2591.getGain();
295 switch(gain)
296 {
297 case TSL2591_GAIN_LOW:
298 Serial.println("1x (Low)");
299 break;
300 case TSL2591_GAIN_MED:
301 Serial.println("25x (Medium)");
302 break;
303 case TSL2591_GAIN_HIGH:
304 Serial.println("428x (High)");
305 break;
306 case TSL2591_GAIN_MAX:
307 Serial.println("9876x (Max)");
308 break;
309 }
310 Serial.print ("Timing: ");
311 Serial.print((tsl2591.getTiming() + 1) * 100, DEC);
312 Serial.println(" ms");
313 Serial.println("------------------------------------");
314 Serial.println("");
315}
316#endif /* USE_TSL2591 */
317
318void task1_setup() {
319 Serial.println("Sensor Task : setup start!");
320
321#ifdef USE_TMP007
322 if (tmp007.begin()) {
323 Serial.println("Found a TMP007 sensor.");
324 } else {
325 Serial.println("No TMP007 found!");
326 while (1);
327 }
328 tmp007_cycle = 0;
329#endif /* USE_TMP007 */
330
331#ifdef USE_TSL2591
332 if (tsl2591.begin()) {
333 Serial.println("Found a TSL2591 sensor");
334 } else {
335 Serial.println("No TSL2591 found.");
336 while (1);
337 }
338
339 /* Display some basic information on this sensor */
340 tsl2591_displaySensorDetails();
341
342 /* Configure the sensor */
343 tsl2591_configureSensor();
344#endif /* USE_TSL2591 */
345
346#ifdef USE_VCNL4000
347 if (vcnl4000.begin()) {
348 Serial.println("Found a VCNL4000 sensor");
349 } else {
350 Serial.println("No VCNL4000 found.");
351 while (1);
352 }
353#endif /* USE_VCNL4000 */
354
355#ifdef USE_I2CLCD
356 i2clcd_begin();
357#endif /* USE_I2CLCD */
358}
359
360void loop1() {
361 static uint32_t sensor_cyc_tim = 0;
362
363 if ((millis() - sensor_cyc_tim) < SENSOR_DELAY_MS){
364 delay(1);
365 return;
366 }else{
367 sensor_cyc_tim = millis();
368 }
369
370#ifdef USE_TMP007
371 /* TMP007 */
372 if((++tmp007_cycle) == (TMP007_CYCLE_MS/SENSOR_DELAY_MS)) {
373 tmp007_cycle = 0;
374 tmp007_objt = tmp007.readObjTempC();
375 tmp007_diet = tmp007.readDieTempC();
376#ifdef SENSOR_DEBUG_PRINT
377 Serial.print("Object Temperature: "); Serial.print(tmp007_objt); Serial.println("*C");
378 Serial.print("Die Temperature: "); Serial.print(tmp007_diet); Serial.println("*C");
379#endif /* SENSOR_DEBUG_PRINT */
380 }
381#endif /* USE_TMP007 */
382
383#ifdef USE_TSL2591
384 /* TSL2591 */
385 if((++tsl2591_cycle) == (TSL2591_CYCLE_MS/SENSOR_DELAY_MS)) {
386 tsl2591_cycle = 0;
387 /* Get a new sensor event */
388 sensors_event_t event;
389 tsl2591.getEvent(&event);
390 tsl2591_light = event.light;
391#ifdef SENSOR_DEBUG_PRINT
392 /* Display the results (light is measured in lux) */
393 if ((event.light == 0) |
394 (event.light > 4294966000.0) |
395 (event.light <-4294966000.0)) {
396 /* If event.light = 0 lux the sensor is probably saturated */
397 /* and no reliable data could be generated! */
398 /* if event.light is +/- 4294967040 there was a float over/underflow */
399 Serial.println("Invalid data (adjust gain or timing)");
400 } else {
401 Serial.print(event.light); Serial.println(" lux");
402 }
403#endif /* SENSOR_DEBUG_PRINT */
404 }
405#endif /* USE_TSL2591 */
406
407#ifdef USE_VCNL4000
408 /* VCNL4000 */
409 if((++vcnl4000_cycle) == (VCNL4000_CYCLE_MS/SENSOR_DELAY_MS)) {
410 vcnl4000_cycle = 0;
411 vcnl4000_proximity = vcnl4000.readProximity();
412#ifdef SENSOR_DEBUG_PRINT
413 Serial.print("Ambient: "); Serial.println(vcnl4000.readAmbient());
414 Serial.print("Proimity: "); Serial.println(vcnl4000_proximity);
415#endif /* SENSOR_DEBUG_PRINT */
416 }
417#endif /* USE_VCNL4000 */
418
419#ifdef USE_I2CLCD
420 if((++i2clcd_cycle) == (I2CLCD_CYCLE_MS/SENSOR_DELAY_MS)) {
421 i2clcd_cycle = 0;
422 i2clcd_clear();
423 i2clcd_setCursor(0, 0);
424 i2clcd_printfloat(tmp007_objt);
425 i2clcd_printStr("*C");
426 i2clcd_setCursor(0, 1);
427 i2clcd_printInt(tsl2591_light);
428 i2clcd_printStr("lux");
429 }
430#endif /* USE_I2CLCD */
431}
432
433/////////////////////////////////////////////////////////////////////
434//
435// Web Server Task
436//
437/////////////////////////////////////////////////////////////////////
438#include "ESP8266.h"
439
440ESP8266 wifi;
441
442void task2_setup()
443{
444 Serial.println("Web Server Task : Start!");
445
446 wifi.begin(Serial5, 115200);
447
448 Serial.print("FW Version:");
449 Serial.println(wifi.getVersion().c_str());
450
451#if 0
452 if (wifi.setOprToStation()) {
453 Serial.print("to station ok\r\n");
454 } else {
455 Serial.print("to station err\r\n");
456 }
457
458 if (wifi.joinAP(SSID, PASSWORD)) {
459 Serial.print("Join AP success\r\n");
460 Serial.print("IP: ");
461 Serial.println(wifi.getLocalIP().c_str());
462 } else {
463 Serial.print("Join AP failure\r\n");
464 }
465#else
466 if (wifi.setOprToSoftAP()) {
467 Serial.print("to softap ok\r\n");
468 } else {
469 Serial.print("to softap err\r\n");
470 }
471
472 if(wifi.setSoftAPParam(AP_SSID, AP_PASSWORD, 7, 0)){
473 Serial.print("Set SoftAP success\r\n");
474 Serial.print("IP: ");
475 Serial.println(wifi.getLocalIP().c_str());
476 }
477 else {
478 Serial.print("Set SoftAP failure\r\n");
479 }
480#endif
481 if (wifi.enableMUX()) {
482 Serial.print("multiple ok\r\n");
483 } else {
484 Serial.print("multiple err\r\n");
485 }
486
487 if (wifi.startTCPServer(80)) {
488 Serial.print("start tcp server ok\r\n");
489 } else {
490 Serial.print("start tcp server err\r\n");
491 }
492
493 if (wifi.setTCPServerTimeout(10)) {
494 Serial.print("set tcp server timout 10 seconds\r\n");
495 } else {
496 Serial.print("set tcp server timout err\r\n");
497 }
498
499 Serial.print("setup end\r\n");
500}
501
502
503const char *web_led_control = " \
504HTTP/1.0 200 OK \r\n\
505Content-Type: text/html \r\n\
506\r\n\
507<html> \r\n\
508<head> \r\n\
509<script language=javascript> \r\n\
510function connecttext( textid, ischecked ) { \r\n\
511 document.getElementById(textid).disabled = !ischecked; \r\n\
512} \r\n\
513</script> \r\n\
514</head> \r\n\
515<body><font size=\"7\"> \r\n\
516<form method=get> \r\n\
517 \r\n\
518<p> \r\n\
519<h3>LED</h3> \r\n\
520<input type=\"checkbox\" name=\"LED2_ONOFF\" value=\"on\" onChange=\"this.form.submit()\" LED2_ISCHECKED /> LED2 \r\n\
521<input type=\"checkbox\" name=\"LED1_ONOFF\" value=\"on\" onChange=\"this.form.submit()\" LED1_ISCHECKED /> LED1 \r\n\
522</p> \r\n\
523 \r\n\
524<p> \r\n\
525<h3>Mode</h3> \r\n\
526OnOff<INPUT type=\"radio\" name=\"led_mode\" value=\"onoff\" onclick=\"connecttext('textforscb3', false);\" onChange=\"this.form.submit()\" LED_MODE_ONOFF > \r\n\
527Blink<INPUT type=\"radio\" name=\"led_mode\" value=\"blink\" onclick=\"connecttext('textforscb3', true);\" onChange=\"this.form.submit()\" LED_MODE_BLINK > \r\n\
528</p> \r\n\
529 \r\n\
530<p> \r\n\
531<INPUT style=\"font-size:24px;\" type=\"text\" name=\"led_cycle\" id=\"textforscb3\" size=\"10\" value=\"LED_BLINK_CYC\" LED_BLINK_CYC_DISABLE> ms \r\n\
532<input style=\"font-size:24px;\" type=\"submit\" value=\"Update\"> \r\n\
533</p> \r\n\
534</form> \r\n\
535</body> \r\n\
536</html> \r\n\
537";
538
539
540const char *web_sensormonitor = " \
541HTTP/1.1 200 OK\r\n\
542Content-Type: text/html\r\n\
543Connection: close\r\n\
544Refresh: 2\r\n\
545\r\n\
546<!DOCTYPE HTML>\r\n\
547<html>\r\n \
548<body><font size=\"7\"> \r\n\
549<p>Die Temperature : DIE_TEMP_I.DIE_TEMP_D *C</p>\r\n\
550<p>Object Temperature : OBJ_TEMP_I.OBJ_TEMP_D *C</p>\r\n\
551<p>Illuminance : TSL2591_LIGHT_I.TSL2591_LIGHT_D Lux</p>\r\n\
552<p>Proximity : VCNL4000_PROXIMITY</p>\r\n\
553</body></html> ";
554
555const char *web_top = " \
556HTTP/1.0 200 OK \r\n\
557Content-Type: text/html \r\n\
558\r\n\
559<html> \r\n\
560<body><font size=\"7\"> \r\n\
561<ul> \r\n\
562<li><a href=\"./ledcontrol\">LED Control</a><br></li> \r\n\
563<li><a href=\"./sensormonitor\">Sensor Monitor</a><br></li> \r\n\
564</ul> \r\n\
565</body></html> ";
566
567
568void loop2()
569{
570 uint8_t buffer[128] = {0};
571 uint8_t mux_id;
572 uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer));
573 String cmd;
574
575 if(len == 0) {
576 return;
577 }
578
579 Serial.println("Web Server Task : Recive Request");
580 String buf_string((char*)buffer);
581// Serial.println(buf_string);
582
583 if((buf_string.indexOf("/ledcontrol") != -1)) {
584 gled1_state = (buf_string.indexOf("LED1_ONOFF=on") != -1);
585 gled2_state = (buf_string.indexOf("LED2_ONOFF=on") != -1);
586 led_mode = (buf_string.indexOf("led_mode=blink") != -1)? LED_MODE_BLINK : LED_MODE_ONOFF;
587 int index = buf_string.indexOf("led_cycle=");
588 if(index != -1){
589 index += sizeof("led_cycle=") - 1;
590 int last_index = buf_string.indexOf(" ", index);
591 String sub_string = buf_string.substring(index, last_index);
592 led_blink_cyc = 0;
593 for(int i=0; i < sub_string.length(); i++){
594 led_blink_cyc = led_blink_cyc * 10 + (sub_string.charAt(i) - '0');
595 }
596 }
597 cmd = web_led_control;
598 cmd.replace("LED1_ISCHECKED", (gled1_state)?"checked":"");
599 cmd.replace("LED2_ISCHECKED", (gled2_state)?"checked":"");
600 cmd.replace("LED_MODE_ONOFF", (led_mode == LED_MODE_ONOFF)?"checked":"");
601 cmd.replace("LED_MODE_BLINK", (led_mode == LED_MODE_BLINK)?"checked":"");
602 String led_blink_cyc_str(led_blink_cyc);
603 cmd.replace("LED_BLINK_CYC_DISABLE", (led_mode != LED_MODE_BLINK)?"disabled":"");
604 cmd.replace("LED_BLINK_CYC", led_blink_cyc_str);
605 }
606 else if((buf_string.indexOf("/sensormonitor") != -1)) {
607 cmd = web_sensormonitor;
608 cmd.replace("OBJ_TEMP_I", String((int)tmp007_objt));
609 cmd.replace("OBJ_TEMP_D", String((int)((tmp007_objt - (int)tmp007_objt) * 100)));
610 cmd.replace("DIE_TEMP_I", String((int)tmp007_diet));
611 cmd.replace("DIE_TEMP_D", String((int)((tmp007_diet - (int)tmp007_diet) * 100)));
612 cmd.replace("TSL2591_LIGHT_I", String((int)tsl2591_light));
613 cmd.replace("TSL2591_LIGHT_D", String((int)((tsl2591_light - (int)tsl2591_light) * 100)));
614 cmd.replace("VCNL4000_PROXIMITY", String(vcnl4000_proximity));
615 }else {
616 cmd = web_top;
617 }
618
619 wifi.send(mux_id, cmd);
620 wifi.releaseTCP(mux_id);
621}
622
623/////////////////////////////////////////////////////////////////////
624//
625// LED Task
626//
627/////////////////////////////////////////////////////////////////////
628void task3_setup() {
629 Serial.println("LED Task : setup start!");
630 pinMode(LED1_PORT, OUTPUT);
631 digitalWrite(LED1_PORT, LOW);
632 pinMode(LED2_PORT, OUTPUT);
633 digitalWrite(LED2_PORT, LOW);
634}
635
636void loop3() {
637 static bool gled1_blink_state = false;
638 static bool gled2_blink_state = false;
639 static uint32_t analog_cnt = 0;
640 static bool analog_cnt_mode = true;
641
642 if(led_mode == LED_MODE_ONOFF) {
643 digitalWrite(LED1_PORT, (gled1_state)?HIGH:LOW);
644 digitalWrite(LED2_PORT, (gled2_state)?HIGH:LOW);
645 }
646 else {
647 if((++led_cycle) > (led_blink_cyc/2)) {
648 led_cycle = 0;
649 if(gled1_state){
650 if(gled1_blink_state){
651 gled1_blink_state = false;
652 digitalWrite(LED1_PORT,LOW);
653 }else{
654 gled1_blink_state = true;
655 digitalWrite(LED1_PORT,HIGH);
656 }
657 }
658 else {
659 digitalWrite(LED1_PORT, LOW);
660 }
661 if(gled2_state){
662 if(gled2_blink_state){
663 gled2_blink_state = false;
664 digitalWrite(LED2_PORT,LOW);
665 }else{
666 gled2_blink_state = true;
667 digitalWrite(LED2_PORT,HIGH);
668 }
669 }
670 else {
671 digitalWrite(LED2_PORT,LOW);
672 }
673 }
674 }
675
676 if(analog_cnt_mode){
677 analog_cnt++;
678 if(analog_cnt > 255*5){
679 analog_cnt_mode = false;
680 }
681 }else{
682 analog_cnt--;
683 if(analog_cnt == 0){
684 analog_cnt_mode = true;
685 }
686 }
687
688 analogWrite(13,analog_cnt/5);
689
690 delay(1);
691}
692
693/////////////////////////////////////////////////////////////////////
694//
695// TFT Task
696//
697/////////////////////////////////////////////////////////////////////
698/*
699
700 Arduino TFT Bitmap Logo example
701
702 This example reads an image file from a micro-SD card
703 and draws it on the screen, at random locations.
704
705 In this sketch, the Arduino logo is read from a micro-SD card.
706 There is a .bmp file included with this sketch.
707 - open the sketch folder (Ctrl-K or Cmd-K)
708 - copy the "arduino.bmp" file to a micro-SD
709 - put the SD into the SD slot of the Arduino TFT module.
710
711 This example code is in the public domain.
712
713 Created 19 April 2013 by Enrico Gueli
714
715 http://arduino.cc/en/Tutorial/TFTBitmapLogo
716
717 */
718
719// include the necessary libraries
720#include <SPI.h>
721#include <SD.h>
722#include <TFT.h> // Arduino LCD library
723
724// pin definition for the Uno
725#define sd_cs 7
726#define lcd_cs 10
727#define dc 9
728#define rst 8
729
730// pin definition for the Leonardo
731//#define sd_cs 8
732//#define lcd_cs 7
733//#define dc 0
734//#define rst 1
735
736TFT TFTscreen = TFT(lcd_cs, dc, rst);
737
738// this variable represents the image to be drawn on screen
739PImage logo;
740
741void task4_setup() {
742 Serial.println(F("TFT Task : start!"));
743
744 // initialize the GLCD and show a message
745 // asking the user to open the serial line
746 TFTscreen.begin();
747 TFTscreen.setRotation(0);
748 TFTscreen.background(255, 255, 255);
749
750 TFTscreen.stroke(0, 0, 255);
751 TFTscreen.println();
752 TFTscreen.println(F("Arduino TFT Bitmap Example"));
753 TFTscreen.stroke(0, 0, 0);
754 TFTscreen.println(F("Open serial monitor"));
755 TFTscreen.println(F("to run the sketch"));
756
757 delay(1000);
758
759 // clear the GLCD screen before starting
760 TFTscreen.background(255, 255, 255);
761
762 // try to access the SD card. If that fails (e.g.
763 // no card present), the setup process will stop.
764 Serial.print(F("Initializing SD card..."));
765 if (!SD.begin(sd_cs)) {
766 Serial.println(F("failed!"));
767 return;
768 }
769 Serial.println(F("OK!"));
770
771 // initialize and clear the GLCD screen
772 TFTscreen.begin();
773 TFTscreen.setRotation(0);
774 TFTscreen.background(255, 255, 255);
775
776 // now that the SD card can be access, try to load the
777 // image file.
778 logo = TFTscreen.loadImage("topame.bmp");
779 if (!logo.isValid()) {
780 Serial.println(F("error while loading arduino.bmp"));
781 }
782}
783
784int loop_cnt = 0;;
785
786void loop4() {
787 // don't do anything if the image wasn't loaded correctly.
788 if (logo.isValid() == false) {
789 return;
790 }
791
792 TFTscreen.setCursor(0, 30);
793 TFTscreen.print(F("loop() is running "));
794 TFTscreen.print(loop_cnt++);
795 TFTscreen.print(F("."));
796 delay(1000);
797 TFTscreen.background(255, 255, 255);
798
799 // get a random location where to draw the image.
800 // To avoid the image to be draw outside the screen,
801 // take into account the image size.
802 int x = random(TFTscreen.width() - logo.width());
803 int y = random(TFTscreen.height() - logo.height());
804
805 // draw the image to the screen
806 TFTscreen.image(logo, x, y);
807
808 // wait a little bit before drawing again
809 delay(2000);
810 TFTscreen.background(255, 255, 255);
811}
812
813/////////////////////////////////////////////////////////////////////
814//
815// Processing Task
816//
817/////////////////////////////////////////////////////////////////////
818//#undef Serial
819#define PSERIAL Serial3
820#define OSERIAL Serial
821
822bool processing_connected = false;
823
824void establishContact() {
825 while (!PSERIAL.available()) {
826 PSERIAL.print('A'); // send a capital A
827 delay(300);
828 }
829 OSERIAL.println("Processing Task : Connect!");
830 processing_connected = true;
831}
832
833void task5_setup()
834{
835 PSERIAL.begin(115200);
836 while(!PSERIAL){
837 delay(1);
838 }
839 OSERIAL.println("Processing Task : Start!");
840}
841
842int inByte = 0;
843int last_connect = 0;
844
845#define TIMEOUT_MS 3000
846
847void loop5()
848{
849 if(!processing_connected){
850 establishContact();
851 }else{
852 if((millis() - last_connect) > TIMEOUT_MS){
853 processing_connected = false;
854 OSERIAL.println("Processing Task : Disconnect!");
855 }
856 }
857
858 uint16_t load;
859 uint16_t isr_cnt;
860 uint16_t dispatch_cnt;
861
862 load = 100 - map(r2ca_idle_result, 0, IDLE_TASK_IDLE_LOOP_10MS/10, 0, 100);
863 isr_cnt = (r2ca_isr_result > 0xffff)? 0xffff : r2ca_isr_result;
864 dispatch_cnt = (r2ca_dispatch_result > 0xffff)? 0xffff : r2ca_dispatch_result;
865
866 if (PSERIAL.available()){
867 inByte = PSERIAL.read();
868 last_connect = millis();
869#if 0
870 uint8_t buf[10];
871 buf[0] = (uint8_t)(load >> 8);
872 buf[1] = (uint8_t)load;
873 buf[2] = (uint8_t)(isr_cnt >> 8);
874 buf[3] = (uint8_t)isr_cnt;
875 buf[4] = (uint8_t)(dispatch_cnt >> 8);
876 buf[5] = dispatch_cnt;
877 PSERIAL.write(buf, 6);
878#endif
879 PSERIAL.write((uint8_t)(load >> 8));
880 PSERIAL.write((uint8_t)load);
881 PSERIAL.write((uint8_t)(isr_cnt >> 8));
882 PSERIAL.write((uint8_t)isr_cnt);
883 PSERIAL.write((uint8_t)(dispatch_cnt >> 8));
884 PSERIAL.write((uint8_t)dispatch_cnt);
885
886 }
887 delay(1);
888}
Note: See TracBrowser for help on using the repository browser.