前文提到我要建一個程式庫以字元陣列的方式,取代 C++ 字串變換,目的確保系統運作穩定,程式碼如下:
// (C) 2015-2016, Bridan Wang, CC BY-NC-SA 3.0 TW
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)
#include <SoftwareSerial.h>
byte echo_index = 0;
char echo[64] = "";
char gIrCommandString[16] = "Connect to ";
byte gIrCommandIndex = 11;
SoftwareSerial BT(13, 3); // pin 13 connect to BT's TX
// pin 3 connect to BT's RX
void BT_BEGIN() {
BT.begin(57600);
// BT 使用 Baud Rate 57600
}
void REPLY(void) {
if (echo_index != 0) {
echo[echo_index] = 0;
BT.println(echo);
echo[0] = echo_index = 0;
}
}
void ECHO_STRING(char *st){
byte i;
for (i=0 ; i < 16 ; i++) {
if (*st == 0)
break;
else
echo[echo_index++] = *(st++);
}
}
void ECHO_CHARS(char *st){
do {
echo[echo_index++] = *(st++);
} while (*st != 0);
}
void ECHO_INT(int jj) {
byte kk;
byte ii = 1;
if (jj >= 10000) {
ii = 5;
} else if (jj >= 1000) {
ii = 4;
} else if (jj >= 100) {
ii = 3;
} else if (jj >= 10) {
ii = 2;
}
echo_index += ii;
kk = echo_index;
do {
echo[--kk] = '0' + (jj % 10);
jj /= 10;
} while (--ii != 0);
}
void setup() {
char i = 8;
BT_BEGIN(); // BT open
delay(3000); // wait for BT to connect.
ECHO_STRING(gIrCommandString);
ECHO_CHARS((char*)"ROSA,V");
ECHO_INT(i);
REPLY();
}
沒有留言:
張貼留言