CmdMessenger - cchamchi/cansat GitHub Wiki
์๋์ด๋ ธ์ PC๊ฐ์ command๋ฅผ ์ฃผ๊ณ ๋ฐ์์ ์๋ protocol
์๋์ด๋ ธ ํํ์ด์ง์์ ์ฐพ์ ์ ์๋ค. ํ์ฌ 4.0๊น์ง ๋์์์
https://github.com/thijse/Arduino-CmdMessenger
๊ธฐ๋ณธ ์ ์ธ ๊ตฌ์กฐ๋ ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ๋ค. ์๋ฆฌ์ผ ํฌํธ๋ฅผ ํตํด CmdID,para1,para2...paraN; (๋์ ์ธ๋ฏธ์ฝ๋ก ) ์ ํํ๋ก command๋ฅผ ์ฃผ๊ณ ๋ฐ๋๋ค

์๋์ด๋ ธ ์ฝ๋๋
Step1 ์๋ฆฌ์ผ ํฌํธ์ CmdMessenger object ์ฐ๊ฒฐ
// Attach a new CmdMessenger object to the default Serial port
CmdMessenger cmdMessenger = CmdMessenger(Serial);
Step2 command List ์ ์ธ
enum
{
kAcknowledge,
kError,
kSetLed, // Command to request led to be set in specific state
kSetLedFrequency,
};
Step3 command ID ์ ์คํ๋ ํจ์ (callback function)์ฐ๊ฒฐ
// Callbacks define on which received commands we take action
void attachCommandCallbacks()
{
// Attach callback methods
cmdMessenger.attach(OnUnknownCommand);
cmdMessenger.attach(kSetLed, OnSetLed);
cmdMessenger.attach(kSetLedFrequency, OnSetLedFrequency);
}
Step5 callback function์์ ์คํ๋ ๋์ ์ ์
// Callback function that sets led on or off
void OnSetLed()
{
// Read led state argument, interpret string as boolean
ledState = cmdMessenger.readBoolArg();
cmdMessenger.sendCmd(kAcknowledge,ledState);
}
// Callback function that sets leds blinking frequency
void OnSetLedFrequency()
{
// Read led state argument, interpret string as boolean
ledFrequency = cmdMessenger.readFloatArg();
// Make sure the frequency is not zero (to prevent divide by zero)
if (ledFrequency < 0.001) { ledFrequency = 0.001; }
// translate frequency in on and off times in miliseconds
intervalOn = (500.0/ledFrequency);
intervalOff = (1000.0/ledFrequency);
cmdMessenger.sendCmd(kAcknowledge,ledFrequency);
}
Step6 setup
void setup()
{
// Listen on serial connection for messages from the PC
Serial.begin(115200);
// Adds newline to every command
cmdMessenger.printLfCr();
// Attach my application's user-defined callback methods
attachCommandCallbacks();
// Send the status to the PC that says the Arduino has booted
// Note that this is a good debug function: it will let you also know
// if your program had a bug and the arduino restarted
cmdMessenger.sendCmd(kAcknowledge,"Arduino has started!");
}
Step7 Loop์์ ๋ช ๋ น ๊ธฐ๋ค๋ฆฌ๊ธฐ
void loop()
{
// Process incoming serial data, and perform callbacks
cmdMessenger.feedinSerialData();
delay(10);
}