Skip to content

Instantly share code, notes, and snippets.

@walkure
Last active February 19, 2022 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkure/7c2769d1f81f34d333c3f1391c90bfab to your computer and use it in GitHub Desktop.
Save walkure/7c2769d1f81f34d333c3f1391c90bfab to your computer and use it in GitHub Desktop.
Arduino便利関数
// Streamから一行読み込む関数
String readLine(Stream* s){
String ret;
bool start = false;
while(true){
int ch = s->read();
if(ch < 0){
return ret;
}
if(ch == '\r' || ch == '\n'){
if(start){
return ret;
}else{
continue;
}
}
start = true;
ret += (char)ch;
}
}
//適当なdelimiterで区切られた文字列のうち指定されたところだけ読み込む関数
String splitString(String line,char delimiter,uint8_t index){
uint32_t start = 0;
uint32_t end = line.length();
for(uint32_t i = 0 ; i < line.length() ; i++){
unsigned int c = line[i];
if(c == 0){
// unexpeced termination of line
break;
}else if(c == delimiter){
if(index == 0){
end = i;
break;
}else if(index == 1){
start = i+1;
}
index --;
}
}
if(index == 0){
return line.substring(start,end);
}else{
return String();
}
}
// bytes列をhex stringに変換するヘルパ
inline char itoc(const uint8_t i) {
return i < 10 ? i+0x30 : i+0x57; // 0x57 = 0x61('a') - 0x0a
//return i < 10 ? i+0x30 : i+0x37; // 0x37 = 0x41('A') - 0x0a
}
// bytes列をhex stringに変換
// strbufのサイズは len*2+1
const char* BytesToHexStr(const uint8_t * const data, char* strbuf, size_t len){
for(size_t i = 0 ; i < len ; i ++){
strbuf[i*2+1] = itoc(data[i] & 0xf);
strbuf[i*2] = itoc(data[i] >> 4);
}
strbuf[len*2] = '\0';
return strbuf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment