Skip to content

sadr0b0t/babbler_h

Repository files navigation

babbler_h

babbler screencast

самый простой ΠΏΡ€ΠΈΠΌΠ΅Ρ€

#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();
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages