/
k3dr
/
stationProgrammer
Обзор
Документация
Войти
/
k3dr
/
stationProgrammer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ArduinoStationBase/ArduinoStationBase.ino
212 строк
4 KB
k3dr
upload files
11 янв 2026, 10:23
11 янв 2026, 10:23
ccd2d23
Код
Авторство
О чём код?
#include "stationLib_Lite.h" #include <EEPROM.h> //Modes of station work #define STATION 1 #define DEBUG 0 //Quantity of arrows on station #define ARR_COUNT 1 //Первична инициализация памяти #define KEY 340 #define INIT_KEY 350 #define ST_MSG "DEBUG" #define WS1_SR_PIN 9 #define SW_PIN 5 int work_mode = STATION; struct arrowInf { uint8_t pin; // 1 byte uint8_t strPos; // 1 byte uint8_t trnPos; // 1 byte bool curPos; // 1 byte }; const int sizeArrow = sizeof(arrowInf); arrowInf arrs[ARR_COUNT] { {9, 50, 150, 1}, }; arrow ws1 ( arrs[0].pin, 1, 2, 3 ); int assocArray[40]; void send(const arrowInf* sheet); bool receive(arrowInf* sheet); void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); pinMode(WS1_SR_PIN, OUTPUT); pinMode(SW_PIN, INPUT_PULLUP); if (KEY == INIT_KEY) { configureEEPROM(); } else { int eepromAddress = 0; for (int i = 0; i < ARR_COUNT; i++) { dumpEEPROM(&arrs[i], &eepromAddress); } } makeAssocArray(); } void loop() { if (work_mode == STATION) { if (Serial.available()) { connectionToProgrammer(); } else { processArrows(); } } //work_mode == DEBUG else { if (Serial.available()) { String cmd = Serial.readStringUntil('\n'); processCommand(cmd); } } } void processArrows() { bool sw_signal = digitalRead(SW_PIN); bool ws1_state; EEPROM.get(3, ws1_state); if (!sw_signal && ws1_state ) { Serial.println("STR"); ws1.switchTurning(); } else if (sw_signal && !ws1_state) { ws1.switchStraight(); } } void send(const arrowInf* sheet) { Serial.write((const char*) sheet, sizeArrow); }; bool receive(arrowInf* sheet) { return (Serial.readBytes((char*) sheet, sizeArrow) == sizeArrow); }; /* Receive "DEBUG"-connection message from programmer and send information about arrows from arrs as bytes of arrowInf struct */ void connectionToProgrammer() { String cmd = Serial.readStringUntil("\n"); if (cmd.indexOf(ST_MSG) != -1) { work_mode = DEBUG; digitalWrite(LED_BUILTIN, HIGH); Serial.println(ARR_COUNT); delay(300); for (int i=0; i<ARR_COUNT; i++){ send(&arrs[i]); delay(300); } } } void configureEEPROM() { int eepromAddress = 0; for (int i = 0; i < ARR_COUNT; i++) { EEPROM.put(eepromAddress, arrs[i]); eepromAddress += sizeArrow; } //blink digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); } void dumpEEPROM(arrowInf* oneArrInf, int* eepromAddress) { EEPROM.get(*eepromAddress, *oneArrInf); *eepromAddress += sizeArrow; } void makeAssocArray() { for (int i = 0; i < ARR_COUNT; i++) { int pin = arrs[i].pin; assocArray[pin] = i; } } void processCommand(String cmd) { //Exp. // ARR:1;SET_STR:259. String device; //Device type int pin; //Connection pin String action; //Command int value; // Value to be set int indexOfColon = cmd.indexOf(':'); device = cmd.substring(0, indexOfColon-1); int indexOfSemiColon = cmd.indexOf(';'); pin = cmd.substring(indexOfColon+1, indexOfSemiColon).toInt(); indexOfColon = cmd.indexOf(':', indexOfSemiColon); action = cmd.substring(indexOfSemiColon+1, indexOfColon); int indexOfPoint = cmd.indexOf('.', indexOfSemiColon); value = cmd.substring(indexOfColon+1,indexOfPoint).toInt(); Serial.println(pin); Serial.println(action); Serial.println(value); int arr_inf_assoc = assocArray[pin]; int new_eeprom_address; if (action == "SET_STR") { new_eeprom_address = 1 + arr_inf_assoc*4; EEPROM.update(new_eeprom_address, value); } else if (action == "SET_TRN") { new_eeprom_address = 2 + arr_inf_assoc*4; EEPROM.update(new_eeprom_address, value); } else if (action == "MANUAL") { ; } }