source: rtos_arduino/trunk/examples/CompositeExample/rca_app.cpp@ 137

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

サンプルの追加.

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