50 lines
1 KiB
C++
50 lines
1 KiB
C++
|
/*
|
||
|
SerialUtil.cpp
|
||
|
(C)2013 kou029w - MIT License
|
||
|
*/
|
||
|
|
||
|
#include <Arduino.h>
|
||
|
#include "SerialUtil.h"
|
||
|
|
||
|
template<>
|
||
|
String inputLine(String str, HardwareSerial& serial);
|
||
|
template<>
|
||
|
String getStr(size_t size, HardwareSerial& serial);
|
||
|
#ifdef __AVR_ATmega32U4__
|
||
|
template<>
|
||
|
String inputLine(String str, Serial_& serial);
|
||
|
template<>
|
||
|
String getStr(size_t size, Serial_& serial);
|
||
|
#endif
|
||
|
|
||
|
// serialから文字列を一行('\r'まで)入力 (str:入力がないとき返す文字列)
|
||
|
template<typename T>
|
||
|
String inputLine(String str, T& serial){
|
||
|
if(!serial) return str;
|
||
|
String result = "";
|
||
|
for(;;){
|
||
|
if(serial.available()){
|
||
|
char c = serial.read();
|
||
|
if(c == '\r') break;
|
||
|
result += String(c);
|
||
|
}
|
||
|
}
|
||
|
if(result != "") return result;
|
||
|
return str;
|
||
|
}
|
||
|
|
||
|
// serialから文字列を入力
|
||
|
template<typename T>
|
||
|
String getStr(size_t size, T& serial){
|
||
|
if(!serial) return String("");
|
||
|
String str = "";
|
||
|
for(size_t i = 0; i<size;){
|
||
|
if(serial.available()){
|
||
|
char c = serial.read();
|
||
|
str.concat(String(c));
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
return str;
|
||
|
}
|