ΡΠ°ΠΌΡΠΉ ΠΏΡΠΎΡΡΠΎΠΉ ΠΏΡΠΈΠΌΠ΅Ρ
#include "babbler.h"
#include "babbler_simple.h"
#include "babbler_cmd_core.h"
#include "babbler_serial.h"
// Π Π°Π·ΠΌΠ΅ΡΡ Π±ΡΡΠ΅ΡΠΎΠ² Π΄Π»Ρ ΡΡΠ΅Π½ΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄ ΠΈ Π·Π°ΠΏΠΈΡΠΈ ΠΎΡΠ²Π΅ΡΠΎΠ²
// Read and write buffer size for communication modules
#define SERIAL_READ_BUFFER_SIZE 128
#define SERIAL_WRITE_BUFFER_SIZE 512
// ΠΡΡΠ΅ΡΡ Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° Π΄Π°Π½Π½ΡΠΌΠΈ Ρ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠΎΠΌ ΡΠ΅ΡΠ΅Π· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡ.
// +1 Π±Π°ΠΉΡ Π² ΠΊΠΎΠ½ΡΠ΅ Π΄Π»Ρ Π·Π°Π²Π΅ΡΡΠ°ΡΡΠ΅Π³ΠΎ Π½ΡΠ»Ρ
// Data exchange buffers to communicate with computer via serial port.
// +1 extra byte at the end for terminating zero
char serial_read_buffer[SERIAL_READ_BUFFER_SIZE+1];
char serial_write_buffer[SERIAL_WRITE_BUFFER_SIZE];
/** ΠΠ°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/** Registered commands */
extern const babbler_cmd_t BABBLER_COMMANDS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
CMD_HELP,
CMD_PING
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of registered commands*/
extern const int BABBLER_COMMANDS_COUNT = sizeof(BABBLER_COMMANDS)/sizeof(babbler_cmd_t);
/** Π ΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²Π° Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Manuals for registered commands */
extern const babbler_man_t BABBLER_MANUALS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
MAN_HELP,
MAN_PING
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ² Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of manuals for registered commands */
extern const int BABBLER_MANUALS_COUNT = sizeof(BABBLER_MANUALS)/sizeof(babbler_man_t);
void setup() {
Serial.begin(9600);
Serial.println("Starting babbler-powered device, type help for list of commands");
babbler_serial_set_packet_filter(packet_filter_newline);
babbler_serial_set_input_handler(handle_input_simple);
//babbler_serial_setup(
// serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
// serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
// 9600);
babbler_serial_setup(
serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
BABBLER_SERIAL_SKIP_PORT_INIT);
}
void loop() {
// ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎ ΡΠ»Π΅Π΄ΠΈΠΌ Π·Π° ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌ ΠΏΠΎΡΡΠΎΠΌ, ΠΆΠ΄Π΅ΠΌ Π²Ρ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
// monitor serial port for input data
babbler_serial_tasks();
}
ΠΏΡΠΈΠΌΠ΅Ρ ΠΏΠΎΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½Π΅Π΅ - Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠΎΠ±ΡΡΠ²Π΅Π½Π½ΡΡ ΠΊΠΎΠΌΠ°Π½Π΄
#include "babbler.h"
#include "babbler_simple.h"
#include "babbler_cmd_core.h"
#include "babbler_serial.h"
// Π Π°Π·ΠΌΠ΅ΡΡ Π±ΡΡΠ΅ΡΠΎΠ² Π΄Π»Ρ ΡΡΠ΅Π½ΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄ ΠΈ Π·Π°ΠΏΠΈΡΠΈ ΠΎΡΠ²Π΅ΡΠΎΠ²
// Read and write buffer size for communication modules
#define SERIAL_READ_BUFFER_SIZE 128
#define SERIAL_WRITE_BUFFER_SIZE 512
// ΠΡΡΠ΅ΡΡ Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° Π΄Π°Π½Π½ΡΠΌΠΈ Ρ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠΎΠΌ ΡΠ΅ΡΠ΅Π· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡ.
// +1 Π±Π°ΠΉΡ Π² ΠΊΠΎΠ½ΡΠ΅ Π΄Π»Ρ Π·Π°Π²Π΅ΡΡΠ°ΡΡΠ΅Π³ΠΎ Π½ΡΠ»Ρ
// Data exchange buffers to communicate with computer via serial port.
// +1 extra byte at the end for terminating zero
char serial_read_buffer[SERIAL_READ_BUFFER_SIZE+1];
char serial_write_buffer[SERIAL_WRITE_BUFFER_SIZE];
#define LED_PIN 13
bool ledison = false;
/** Π Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ ledon (Π²ΠΊΠ»ΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ) */
/** ledon (turn led ON) command implementation */
int cmd_ledon(char* reply_buffer, int reply_buf_size, int argc=0, char *argv[]=NULL) {
digitalWrite(LED_PIN, HIGH);
ledison = true;
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°
strcpy(reply_buffer, REPLY_OK);
return strlen(reply_buffer);
}
/** Π Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ ledoff (Π²ΠΊΠ»ΡΡΠΈΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΡ) */
/** ledoff (turn led OFF) command implementation */
int cmd_ledoff(char* reply_buffer, int reply_buf_size, int argc=0, char *argv[]=NULL) {
digitalWrite(LED_PIN, LOW);
ledison = false;
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°
strcpy(reply_buffer, REPLY_OK);
return strlen(reply_buffer);
}
/** Π Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ ledstatus (cΡΠ°ΡΡΡ Π»Π°ΠΌΠΏΠΎΡΠΊΠΈ) */
/** ledstatus (get led status) command implementation */
int cmd_ledstatus(char* reply_buffer, int reply_buf_size, int argc=0, char *argv[]=NULL) {
if(ledison) {
strcpy(reply_buffer, "on");
} else {
strcpy(reply_buffer, "off");
}
return strlen(reply_buffer);
}
babbler_cmd_t CMD_LEDON = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledon",
/* ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠ΅ΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* pointer to function with command implementation*/
&cmd_ledon
};
babbler_man_t MAN_LEDON = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledon",
/* ΠΊΡΠ°ΡΠΊΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* short description */
"turn led ON",
/* ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²ΠΎ */
/* manual */
"SYNOPSIS\n"
" ledon\n"
"DESCRIPTION\n"
"Turn led ON."
};
babbler_cmd_t CMD_LEDOFF = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledoff",
/* ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠ΅ΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* pointer to function with command implementation*/
&cmd_ledoff
};
babbler_man_t MAN_LEDOFF = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledoff",
/* ΠΊΡΠ°ΡΠΊΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* short description */
"turn led OFF",
/* ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²ΠΎ */
/* manual */
"SYNOPSIS\n"
" ledoff\n"
"DESCRIPTION\n"
"Turn led OFF."
};
babbler_cmd_t CMD_LEDSTATUS = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledstatus",
/* ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠ΅ΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* pointer to function with command implementation*/
&cmd_ledstatus
};
babbler_man_t MAN_LEDSTATUS = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"ledstatus",
/* ΠΊΡΠ°ΡΠΊΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* short description */
"get led status: on/off",
/* ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²ΠΎ */
/* manual */
"SYNOPSIS\n"
" ledstatus\n"
"DESCRIPTION\n"
"Get led status: on/off."
};
/** ΠΠ°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/** Registered commands */
extern const babbler_cmd_t BABBLER_COMMANDS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
CMD_HELP,
CMD_PING,
// ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// custom commands
CMD_LEDON,
CMD_LEDOFF,
CMD_LEDSTATUS
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of registered commands*/
extern const int BABBLER_COMMANDS_COUNT = sizeof(BABBLER_COMMANDS)/sizeof(babbler_cmd_t);
/** Π ΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²Π° Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Manuals for registered commands */
extern const babbler_man_t BABBLER_MANUALS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
MAN_HELP,
MAN_PING,
// ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// custom commands
MAN_LEDON,
MAN_LEDOFF,
MAN_LEDSTATUS
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ² Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of manuals for registered commands */
extern const int BABBLER_MANUALS_COUNT = sizeof(BABBLER_MANUALS)/sizeof(babbler_man_t);
void setup() {
Serial.begin(9600);
Serial.println("Starting babbler-powered device, type help for list of commands");
babbler_serial_set_packet_filter(packet_filter_newline);
babbler_serial_set_input_handler(handle_input_simple);
//babbler_serial_setup(
// serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
// serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
// 9600);
babbler_serial_setup(
serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
BABBLER_SERIAL_SKIP_PORT_INIT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎ ΡΠ»Π΅Π΄ΠΈΠΌ Π·Π° ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌ ΠΏΠΎΡΡΠΎΠΌ, ΠΆΠ΄Π΅ΠΌ Π²Ρ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
// monitor serial port for input data
babbler_serial_tasks();
}
ΠΠΎΠΌΠ°Π½Π΄Ρ Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ
#include "babbler.h"
#include "babbler_simple.h"
#include "babbler_cmd_core.h"
#include "babbler_serial.h"
// Π Π°Π·ΠΌΠ΅ΡΡ Π±ΡΡΠ΅ΡΠΎΠ² Π΄Π»Ρ ΡΡΠ΅Π½ΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄ ΠΈ Π·Π°ΠΏΠΈΡΠΈ ΠΎΡΠ²Π΅ΡΠΎΠ²
// Read and write buffer size for communication modules
#define SERIAL_READ_BUFFER_SIZE 128
#define SERIAL_WRITE_BUFFER_SIZE 512
// ΠΡΡΠ΅ΡΡ Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° Π΄Π°Π½Π½ΡΠΌΠΈ Ρ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠΎΠΌ ΡΠ΅ΡΠ΅Π· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡ.
// +1 Π±Π°ΠΉΡ Π² ΠΊΠΎΠ½ΡΠ΅ Π΄Π»Ρ Π·Π°Π²Π΅ΡΡΠ°ΡΡΠ΅Π³ΠΎ Π½ΡΠ»Ρ
// Data exchange buffers to communicate with computer via serial port.
// +1 extra byte at the end for terminating zero
char serial_read_buffer[SERIAL_READ_BUFFER_SIZE+1];
char serial_write_buffer[SERIAL_WRITE_BUFFER_SIZE];
/** Π Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ pin_mode (ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΠ΅ΠΆΠΈΠΌ ΠΏΠΈΠ½Π°: INPUT/OUTPUT) */
/** pin_mode (set pin mode: INPUT/OUTPUT) command implementation */
int cmd_pin_mode(char* reply_buffer, int reply_buf_size, int argc, char *argv[]) {
bool paramsOk = true;
bool success = false;
// ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌΡΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ
// argv[0] - ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ "pin_mode" (ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ Π²ΡΠ΅Π³Π΄Π° ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ)
// argv[1] - Π½ΠΎΠΌΠ΅Ρ ΠΏΠΈΠ½Π°
// argv[2] - ΡΠ΅ΠΆΠΈΠΌ ΠΏΠΈΠ½Π°: OUTPUT/INPUT
// expected params
// argv[0] - cmd name: "pin_mode" (1st param is always command name)
// argv[1] - pin number
// argv[2] - pin mode: INPUT/OUTPUT
if(argc == 3) {
// ΡΠ°ΡΠΏΠΎΠ·Π½Π°ΡΡ Π½ΠΎΠΌΠ΅Ρ ΠΏΠΈΠ½Π° ΠΈΠ· 2Π³ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°
// parse pin number from 2nd param
char* pinStr = argv[1];
int pin = atoi(pinStr);
if(pin > 0 || (pin == 0 && strcmp("0", pinStr) == 0)) {
// Ρ Π½ΠΎΠΌΠ΅ΡΠΎΠΌ ΠΏΠΈΠ½Π° Π²ΡΠ΅ ΠΎΠΊ,
// ΡΠ°ΡΠΏΠΎΠ·Π½Π°ΡΡ ΡΠ΅ΠΆΠΈΠΌ INPUT/OUTPUT ΠΈΠ· 3Π³ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°
// pin number is ok,
// parse mode INPUT/OUTPUT from the 3rd param
char* modeStr = argv[2];
if(strcmp("OUTPUT", modeStr) == 0) {
// Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// execute command
pinMode(pin, OUTPUT);
success = true;
} else if(strcmp("INPUT", modeStr) == 0) {
// Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// execute command
pinMode(pin, INPUT);
success = true;
} else {
paramsOk = false;
}
} else {
paramsOk = false;
}
} else {
paramsOk = false;
}
// Π·Π°ΠΏΠΈΡΠ΅ΠΌ ΠΎΡΠ²Π΅Ρ
// write reply
if(success) {
// Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ Π²ΡΠ΅ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅, ΠΊΠΎΠΌΠ°Π½Π΄Π° Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°
// params ok, command executed
strcpy(reply_buffer, REPLY_OK);
} else if(!paramsOk) {
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°, ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ
// command not executed with bad params error
strcpy(reply_buffer, REPLY_BAD_PARAMS);
} else {
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°, ΠΊΠ°ΠΊΠ°Ρ-ΡΠΎ Π΄ΡΡΠ³Π°Ρ ΠΎΡΠΈΠ±ΠΊΠ°
// command not executed with some other error
strcpy(reply_buffer, REPLY_ERROR);
}
return strlen(reply_buffer);
}
/** Π Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ digital_write (Π·Π°ΠΏΠΈΡΠ°ΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ HIGH/LOW Π² ΠΏΠΎΡΡ) */
/** digital_write (write value HIGH/LOW to port) command implementation */
int cmd_digital_write(char* reply_buffer, int reply_buf_size, int argc, char *argv[]) {
bool paramsOk = true;
bool success = false;
// ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌΡΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ
// argv[0] - ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ "digital_write" (ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ Π²ΡΠ΅Π³Π΄Π° ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ)
// argv[1] - Π½ΠΎΠΌΠ΅Ρ ΠΏΠΈΠ½Π°
// argv[2] - Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅: HIGH/1/LOW/0
// expected params
// argv[0] - cmd name: "digital_write" (1st param is always command name)
// argv[1] - pin number
// argv[2] - value: HIGH/1/LOW/0
if(argc == 3) {
// ΡΠ°ΡΠΏΠΎΠ·Π½Π°ΡΡ Π½ΠΎΠΌΠ΅Ρ ΠΏΠΈΠ½Π° ΠΈΠ· 2Π³ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°
// parse pin number from 2nd param
char* pinStr = argv[1];
int pin = atoi(pinStr);
if(pin > 0 || (pin == 0 && strcmp("0", pinStr) == 0)) {
// Ρ Π½ΠΎΠΌΠ΅ΡΠΎΠΌ ΠΏΠΈΠ½Π° Π²ΡΠ΅ ΠΎΠΊ,
// ΡΠ°ΡΠΏΠΎΠ·Π½Π°ΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ HIGH/1/LOW/0 ΠΈΠ· 3Π³ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°
// pin number is ok,
// parse value HIGH/1/LOW/0 from the 3rd param
char* valStr = argv[2];
if(strcmp("HIGH", valStr) == 0 || strcmp("1", valStr) == 0) {
// Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// execute command
digitalWrite(pin, HIGH);
success = true;
} else if(strcmp("LOW", valStr) == 0 || strcmp("0", valStr) == 0) {
// Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// execute command
digitalWrite(pin, LOW);
success = true;
} else {
paramsOk = false;
}
} else {
paramsOk = false;
}
} else {
paramsOk = false;
}
// Π·Π°ΠΏΠΈΡΠ΅ΠΌ ΠΎΡΠ²Π΅Ρ
// write reply
if(success) {
// Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ Π²ΡΠ΅ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅, ΠΊΠΎΠΌΠ°Π½Π΄Π° Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°
// params ok, command executed
strcpy(reply_buffer, REPLY_OK);
} else if(!paramsOk) {
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°, ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ
// command not executed with bad params error
strcpy(reply_buffer, REPLY_BAD_PARAMS);
} else {
// ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π°, ΠΊΠ°ΠΊΠ°Ρ-ΡΠΎ Π΄ΡΡΠ³Π°Ρ ΠΎΡΠΈΠ±ΠΊΠ°
// command not executed with some other error
strcpy(reply_buffer, REPLY_ERROR);
}
return strlen(reply_buffer);
}
babbler_cmd_t CMD_PIN_MODE = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"pin_mode",
/* ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠ΅ΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* pointer to function with command implementation*/
&cmd_pin_mode
};
babbler_man_t MAN_PIN_MODE = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"pin_mode",
/* ΠΊΡΠ°ΡΠΊΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* short description */
"set pin mode: INPUT/OUTPUT",
/* ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²ΠΎ */
/* manual */
"SYNOPSIS\n"
" pin_mode pin mode\n"
"DESCRIPTION\n"
"Set i/o pin mode to INPUT/OUTPUT.\n"
"OPTIONS\n"
" pin - i/o pin number\n"
" mode - pin mode: INPUT or OUTPUT"
};
babbler_cmd_t CMD_DIGITAL_WRITE = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"digital_write",
/* ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΠ΅ΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* pointer to function with command implementation*/
&cmd_digital_write
};
babbler_man_t MAN_DIGITAL_WRITE = {
/* ΠΈΠΌΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/* command name */
"digital_write",
/* ΠΊΡΠ°ΡΠΊΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* short description */
"write digital value HIGH/LOW (1/0) to output port",
/* ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΠΎΠ΅ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ */
/* detailed description */
"SYNOPSIS\n"
" digital_write pin val\n"
"DESCRIPTION\n"
"Write digital value HIGH/LOW (1/0) to output port.\n"
"OPTIONS\n"
" pin - pin number\n"
" val - value to write: 0/LOW for LOW or 1/HIGH for HIGH"
};
/** ΠΠ°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ */
/** Registered commands */
extern const babbler_cmd_t BABBLER_COMMANDS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
CMD_HELP,
CMD_PING,
// ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// custom commands
CMD_PIN_MODE,
CMD_DIGITAL_WRITE
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of registered commands*/
extern const int BABBLER_COMMANDS_COUNT = sizeof(BABBLER_COMMANDS)/sizeof(babbler_cmd_t);
/** Π ΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²Π° Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Manuals for registered commands */
extern const babbler_man_t BABBLER_MANUALS[] = {
// ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΠΈΠ· babbler_cmd_core.h
// commands from babbler_cmd.core.h
MAN_HELP,
MAN_PING,
// ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ
// custom commands
MAN_PIN_MODE,
MAN_DIGITAL_WRITE
};
/** ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ² Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΊΠΎΠΌΠ°Π½Π΄ */
/** Number of manuals for registered commands */
extern const int BABBLER_MANUALS_COUNT = sizeof(BABBLER_MANUALS)/sizeof(babbler_man_t);
void setup() {
Serial.begin(9600);
Serial.println("Starting babbler-powered device, type help for list of commands");
babbler_serial_set_packet_filter(packet_filter_newline);
babbler_serial_set_input_handler(handle_input_simple);
//babbler_serial_setup(
// serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
// serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
// 9600);
babbler_serial_setup(
serial_read_buffer, SERIAL_READ_BUFFER_SIZE,
serial_write_buffer, SERIAL_WRITE_BUFFER_SIZE,
BABBLER_SERIAL_SKIP_PORT_INIT);
}
void loop() {
// ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎ ΡΠ»Π΅Π΄ΠΈΠΌ Π·Π° ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌ ΠΏΠΎΡΡΠΎΠΌ, ΠΆΠ΄Π΅ΠΌ Π²Ρ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
// monitor serial port for input data
babbler_serial_tasks();
}