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

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

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

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