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

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

1.7.10のファイルに更新

File size: 4.9 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
27#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_ARCH_SAMD)
28
29#include "Ciao.h"
30
31CiaoClass::CiaoClass(Stream &_stream) :
32 stream(_stream), started(false) {
33 // Empty
34}
35
36void CiaoClass::begin() {
37
38 String start;
39 String stop;
40
41 // Wait for U-boot to finish startup
42 do {
43 dropAll();
44 delay(1000);
45 }while (stream.available() > 0);
46
47 stream.println("ciao;r;status"); //check the bridge python status
48 String status = stream.readStringUntil(END_TX_CHAR);
49 if(status == "1;running"){
50 do{ //kill a istance of the bridge.py
51 stream.println("ciao;w;quit");
52 stop = stream.readStringUntil(END_TX_CHAR);
53 delay(3000);
54 }while(stop != "1;done");
55 }
56 do{
57 stream.print(F("run-ciao\n")); //start bridge python
58 stream.readStringUntil(END_TX_CHAR);
59 delay(3000);
60 stream.println("ciao;r;status"); //check if bridge python is running
61 start = stream.readStringUntil(END_TX_CHAR);
62 }while (start != "1;running");
63}
64
65CiaoData CiaoClass::read( String connector, String param1, String param2 , String param3 ) {
66
67 CiaoData data;
68 stream.print(connector + ";r"); // send read request
69 if (param1 != ""){
70 stream.print(";"+param1);
71 }
72 if (param2 != ""){
73 stream.print(DATA_SPLIT_CHAR);
74 stream.print(param2);
75 }
76 if (param3 != ""){
77 stream.print(DATA_SPLIT_CHAR);
78 stream.print(param3);
79 }
80 stream.println();
81 String message = stream.readStringUntil(END_TX_CHAR);
82 data = parse(message, ";");
83 return data;
84}
85
86CiaoData CiaoClass::write( String connector, String param1, String param2 , String param3 ) {
87
88 CiaoData data;
89 if (param1 != ""){
90 stream.print(connector+";w;");
91 stream.print(param1);
92 }
93 if (param2 != ""){
94 stream.print(DATA_SPLIT_CHAR);
95 stream.print(param2);
96 }
97 if (param3 != ""){
98 stream.print(DATA_SPLIT_CHAR);
99 stream.print(param3);
100 }
101 stream.println();
102 String mess_receive = stream.readStringUntil(END_TX_CHAR);
103 data = parse(mess_receive, ";");
104 return data;
105
106}
107
108CiaoData CiaoClass::writeResponse( String connector, String id, String param1, String param2 , String param3) {
109
110 CiaoData data;
111 if(id.length()> ID_SIZE_TX){
112 if (param1 != ""){
113 stream.print(connector+";wr;");
114 stream.print(id+";");
115 stream.print(param1);
116 }
117 if (param2 != ""){
118 stream.print(DATA_SPLIT_CHAR);
119 stream.print(param2);
120 }
121 if (param3 != ""){
122 stream.print(DATA_SPLIT_CHAR);
123 stream.print(param3);
124 }
125 stream.println();
126 String mess_receive = stream.readStringUntil(END_TX_CHAR);
127 data = parse(mess_receive, ";");
128 }
129 return data;
130}
131
132CiaoData CiaoClass::parse(String message, String split){
133 String status;
134 CiaoData data;
135 int statusIndex = message.indexOf(split);
136 int messIndex = message.indexOf(split, statusIndex+1);
137 status = message.substring(0, statusIndex); //message status (0: no message; 1:data ready; -1:error)
138
139 if(status == ID_READY && message.substring(statusIndex+1, messIndex) != "done"){
140 data.msg_split[0]=message.substring(statusIndex+1, messIndex);
141 data.parseMessage(message.substring(messIndex+1)); //parsing data received
142 }
143 else{
144 data.msg_split[0] = status;
145 data.msg_split[2] = message.substring(statusIndex+1, messIndex);
146 }
147 return data;
148}
149
150void CiaoClass::dropAll() {
151 while (stream.available() > 0) {
152 stream.read();
153 }
154}
155
156//fuction to split command (ex: digital/13/1)
157void splitString(String command, String split, String msg[], int size){
158
159 for(int a=0;a< size ;a++) //initialize array element to -1
160 msg[a]= ID_ERROR;
161 char buf[command.length()+1];
162 command.toCharArray(buf, command.length()+1);
163 char* array;
164 const char splt[1]= {split.charAt(0)};
165 int i=0;
166 array = strtok (buf,splt);
167 while (array != NULL){
168 msg[i]=String(array);
169 array = strtok (NULL, splt);
170 i++;
171 }
172}
173
174// Ciao instance
175#ifdef __AVR_ATmega32U4__
176// Yun variants (where HardwareSerial is Serial1)
177SerialCiaoClass Ciao(Serial1);
178#elif defined ARDUINO_ARCH_SAMD
179// Tian variants (where Serial_ is SerialUSB)
180SerialCiaoClass Ciao(SerialUSB);
181#endif
182
183#endif
Note: See TracBrowser for help on using the repository browser.