source: rtos_arduino/trunk/arduino_lib/libraries/Ciao/src/lib/Ciao.cpp@ 175

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

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 5.0 KB
Line 
1/*
2****************************************************************************
3* Copyright (c) 2015 Arduino srl. All right reserved.
4*
5* File : Ciao.cpp
6* Date : 2016/02/16
7* Revision : 0.0.2 $
8* Author: andrea[at]arduino[dot]org
9*
10****************************************************************************
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#include "Ciao.h"
27
28#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_ARCH_SAMD)
29
30#if defined(__AVR_ATmega32U4__)
31CiaoClass::CiaoClass(Stream &_stream) :
32 stream(_stream), started(false) {
33 // Empty
34}
35#elif defined(ARDUINO_ARCH_SAMD)
36CiaoClass::CiaoClass(Serial_ stream){
37 // Empty
38}
39#endif
40
41void CiaoClass::begin() {
42
43 String start;
44 String stop;
45
46 // Wait for U-boot to finish startup
47 do {
48 dropAll();
49 delay(1000);
50 }while (stream.available() > 0);
51
52 stream.println("ciao;r;status"); //check the bridge python status
53 String status = stream.readStringUntil(END_TX_CHAR);
54 if(status == "1;running"){
55 do{ //kill a istance of the bridge.py
56 stream.println("ciao;w;quit");
57 stop = stream.readStringUntil(END_TX_CHAR);
58 delay(3000);
59 }while(stop != "1;done");
60 }
61 do{
62 #if defined(__AVR_ATmega32U4__)
63 stream.print(F("run-ciao\n")); //start bridge python
64 stream.readStringUntil(END_TX_CHAR);
65 #endif
66 delay(3000);
67 stream.println("ciao;r;status"); //check if bridge python is running
68 start = stream.readStringUntil(END_TX_CHAR);
69 }while (start != "1;running");
70}
71
72CiaoData CiaoClass::read( String connector, String param1, String param2 , String param3 ) {
73
74 CiaoData data;
75 stream.print(connector + ";r"); // send read request
76 if (param1 != ""){
77 stream.print(";"+param1);
78 }
79 if (param2 != ""){
80 stream.print(DATA_SPLIT_CHAR);
81 stream.print(param2);
82 }
83 if (param3 != ""){
84 stream.print(DATA_SPLIT_CHAR);
85 stream.print(param3);
86 }
87 stream.println();
88 String message = stream.readStringUntil(END_TX_CHAR);
89 data = parse(message, ";");
90 return data;
91}
92
93CiaoData CiaoClass::write( String connector, String param1, String param2 , String param3 ) {
94
95 CiaoData data;
96 if (param1 != ""){
97 stream.print(connector+";w;");
98 stream.print(param1);
99 }
100 if (param2 != ""){
101 stream.print(DATA_SPLIT_CHAR);
102 stream.print(param2);
103 }
104 if (param3 != ""){
105 stream.print(DATA_SPLIT_CHAR);
106 stream.print(param3);
107 }
108 stream.println();
109 String mess_receive = stream.readStringUntil(END_TX_CHAR);
110 data = parse(mess_receive, ";");
111 return data;
112
113}
114
115CiaoData CiaoClass::writeResponse( String connector, String id, String param1, String param2 , String param3) {
116
117 CiaoData data;
118 if(id.length()> ID_SIZE_TX){
119 if (param1 != ""){
120 stream.print(connector+";wr;");
121 stream.print(id+";");
122 stream.print(param1);
123 }
124 if (param2 != ""){
125 stream.print(DATA_SPLIT_CHAR);
126 stream.print(param2);
127 }
128 if (param3 != ""){
129 stream.print(DATA_SPLIT_CHAR);
130 stream.print(param3);
131 }
132 stream.println();
133 String mess_receive = stream.readStringUntil(END_TX_CHAR);
134 data = parse(mess_receive, ";");
135 }
136 return data;
137}
138
139CiaoData CiaoClass::parse(String message, String split){
140 String status;
141 CiaoData data;
142 int statusIndex = message.indexOf(split);
143 int messIndex = message.indexOf(split, statusIndex+1);
144 status = message.substring(0, statusIndex); //message status (0: no message; 1:data ready; -1:error)
145
146 if(status == ID_READY && message.substring(statusIndex+1, messIndex) != "done"){
147 data.msg_split[0]=message.substring(statusIndex+1, messIndex);
148 data.parseMessage(message.substring(messIndex+1)); //parsing data received
149 }
150 else{
151 data.msg_split[0] = status;
152 data.msg_split[2] = message.substring(statusIndex+1, messIndex);
153 }
154 return data;
155}
156
157void CiaoClass::dropAll() {
158 while (stream.available() > 0) {
159 stream.read();
160 }
161}
162
163//fuction to split command (ex: digital/13/1)
164void splitString(String command, String split, String msg[], int size){
165
166 for(int a=0;a< size ;a++) //initialize array element to -1
167 msg[a]= ID_ERROR;
168 char buf[command.length()+1];
169 command.toCharArray(buf, command.length()+1);
170 char* array;
171 const char splt[1]= {split.charAt(0)};
172 int i=0;
173 array = strtok (buf,splt);
174 while (array != NULL){
175 msg[i]=String(array);
176 array = strtok (NULL, splt);
177 i++;
178 }
179}
180
181// Ciao instance
182#ifdef __AVR_ATmega32U4__
183// Yun variants (where HardwareSerial is Serial1)
184SerialCiaoClass Ciao(Serial1);
185#elif defined ARDUINO_ARCH_SAMD
186// Tian variants (where Serial_ is SerialUSB)
187SerialCiaoClass Ciao(SerialUSB);
188#endif
189
190#endif
Note: See TracBrowser for help on using the repository browser.