From e62be3b12d3c94d709a77d89f52c31f7c4ac475d Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 22:22:47 -0800 Subject: Initial commit. --- desktop/main.cc | 215 +++++++++++++++++++++++++++++++++++++++++++++++++ desktop/rpmcount.pro | 15 ++++ desktop/serial_utils.h | 33 ++++++++ desktop/term.cc | 28 +++++++ desktop/term.h | 3 + desktop/types.h | 27 +++++++ 6 files changed, 321 insertions(+) create mode 100755 desktop/main.cc create mode 100755 desktop/rpmcount.pro create mode 100755 desktop/serial_utils.h create mode 100755 desktop/term.cc create mode 100755 desktop/term.h create mode 100755 desktop/types.h (limited to 'desktop') diff --git a/desktop/main.cc b/desktop/main.cc new file mode 100755 index 0000000..ba77e4b --- /dev/null +++ b/desktop/main.cc @@ -0,0 +1,215 @@ +#include "serial_utils.h" +#include "term.h" +#include "types.h" + +#include +#include +#include +#include + +using namespace std; +using namespace LibSerial; + +enum ErrorCode +{ + Ok, + Error +}; + +enum MessageHeader +{ + ReadConfig, + UpdateStartDelay, + UpdateRPMCount, + UpdateSignalsPerRPM +}; + +struct Config +{ + U32 startDelay; // Start delay in milliseconds + U32 rpmCount; // Number of RPM ticks to count + U32 signalsPerRPM; // Number of signal ticks per RPM tick +}; + +template +bool writeSafe (SerialStream& serial, MessageHeader header, const T& val) +{ + write(serial, (U8)header); + write(serial, val); + + U8 errorCode = read(serial); + T x = read(serial); + + fprintf(stderr, "error code: %d\n", errorCode); + fprintf(stderr, "val: %d\n", x); + + return errorCode == Ok && x == val; +} + +bool configureStartDelay (SerialStream& serial, Config& config) +{ + printf("Tiempo de retardo (milisegundos)\n"); + printf("--------------------------------\n"); + printf("\n"); + printf("Valor actual : %d\n", config.startDelay); + printf("\n"); + printf("Nuevo valor : "); + + U32 x; + cin >> x; + + bool result = writeSafe(serial, UpdateStartDelay, x); + if (result) + { + config.startDelay = x; + } + return result; +} + +bool configureRPMCount (SerialStream& serial, Config& config) +{ + printf("RPM maximo\n"); + printf("----------\n"); + printf("\n"); + printf("Valor actual : %d\n", config.rpmCount); + printf("\n"); + printf("Nuevo valor : "); + + U32 x; + cin >> x; + + bool result = writeSafe(serial, UpdateRPMCount, x); + if (result) + { + config.rpmCount = x; + } + return result; +} + +bool configureSignalsPerRPM (SerialStream& serial, Config& config) +{ + printf("Impulsos/RPM\n"); + printf("------------\n"); + printf("\n"); + printf("Valor actual : %d\n", config.signalsPerRPM); + printf("\n"); + printf("Nuevo valor : "); + + U32 x; + cin >> x; + + bool result = writeSafe(serial, UpdateSignalsPerRPM, x); + if (result) + { + config.signalsPerRPM = x; + } + return result; +} + +void loop (SerialStream& serial, Config& config) +{ + int option = -1; + + while (option != 0) + { + system("clear"); + printf("Configuracion\n"); + printf("-------------\n"); + printf("Tiempo de retardo : %d\n", config.startDelay); + printf("RPM maximo : %d\n", config.rpmCount); + printf("Impulsos/rpm : %d\n", config.signalsPerRPM); + printf("\n"); + printf("Menu\n"); + printf("----\n"); + printf("[0] Salir\n"); + printf("[1] Configurar tiempo de retardo\n"); + printf("[2] Configurar RPM maximo\n"); + printf("[3] Configurar impulsos/RPM\n"); + printf("\n"); + printf("> "); + + option = getch() - '0'; + system("clear"); + + bool error = false; + + switch (option) + { + case 1: error = !configureStartDelay(serial, config); break; + case 2: error = !configureRPMCount(serial, config); break; + case 3: error = !configureSignalsPerRPM(serial, config); break; + default: break; + } + + if (error) + { + fprintf(stderr, "Error al escribir valor\n"); + return; + } + } +} + +void readConfig (SerialStream& serial, Config& config) +{ + write(serial, (U8)ReadConfig); + + U8 errorCode = read(serial); + if (errorCode == Ok) + { + config.startDelay = read(serial); + config.rpmCount = read(serial); + config.signalsPerRPM = read(serial); + } +} + +void openArduino (const char* serialDevice, SerialStream& serial) +{ + if (serial.IsOpen()) serial.Close(); + serial.Open(serialDevice); + serial.SetBaudRate(SerialStreamBuf::BAUD_9600); + serial.SetCharSize(SerialStreamBuf::CHAR_SIZE_8); + serial.SetNumOfStopBits(1); + serial.SetParity(SerialStreamBuf::PARITY_DEFAULT); + serial.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_DEFAULT); +} + +void usage (const char* argv0) +{ + fprintf(stderr, "Uso:\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "%s \n", argv0); + fprintf(stderr, "\n"); + fprintf(stderr, "Args:\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "dispositivo - Nombre de dispositivo serie\n"); +} + +int main (int argc, char** argv) +{ + if (argc != 2) + { + usage(argv[0]); + exit(0); + } + + const char* serialDevice = argv[1]; + + SerialStream serial; + printf("Conectando con %s...\n", serialDevice); + openArduino(serialDevice, serial); + if (serial.IsOpen()) + { + sleep(2); // Wait for arduino to wake up + printf("Leyendo configuracion...\n"); + Config config; + readConfig(serial, config); + loop(serial, config); + } + else + { + printf("Error al conectar con %s\n", serialDevice); + cin.get(); + } + + return 0; +} diff --git a/desktop/rpmcount.pro b/desktop/rpmcount.pro new file mode 100755 index 0000000..794ae42 --- /dev/null +++ b/desktop/rpmcount.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +CONFIG += console +CONFIG -= app_bundle +CONFIG -= qt + +LIBS += -lserial + +SOURCES += \ + main.cc \ + term.cc + +HEADERS += \ + types.h \ + serial_utils.h \ + term.h diff --git a/desktop/serial_utils.h b/desktop/serial_utils.h new file mode 100755 index 0000000..8c351e4 --- /dev/null +++ b/desktop/serial_utils.h @@ -0,0 +1,33 @@ +#pragma once + +#include "types.h" + +#include + +template +T read (LibSerial::SerialStream& serial) +{ + T x; + serial.read((char*) &x, sizeof(T)); + return x; +} + +template <> +U32 read (LibSerial::SerialStream& serial) +{ + U32 x = 0; + for (int i = 0; i < 4; ++i) + { + U8 b; + serial.read((char*)&b, 1); + x = x >> 8; + x |= (b << 24); + } + return x; +} + +template +void write (LibSerial::SerialStream& serial, const T& val) +{ + serial.write((const char*) &val, sizeof(T)); +} diff --git a/desktop/term.cc b/desktop/term.cc new file mode 100755 index 0000000..a72f1e9 --- /dev/null +++ b/desktop/term.cc @@ -0,0 +1,28 @@ +#include "term.h" + +#include +#include //_getch*/ +#include //_getch*/ + +char getch () +{ + char buf=0; + struct termios old={0}; + fflush(stdout); + if(tcgetattr(0, &old)<0) + perror("tcsetattr()"); + old.c_lflag&=~ICANON; + old.c_lflag&=~ECHO; + old.c_cc[VMIN]=1; + old.c_cc[VTIME]=0; + if(tcsetattr(0, TCSANOW, &old)<0) + perror("tcsetattr ICANON"); + if(read(0,&buf,1)<0) + perror("read()"); + old.c_lflag|=ICANON; + old.c_lflag|=ECHO; + if(tcsetattr(0, TCSADRAIN, &old)<0) + perror ("tcsetattr ~ICANON"); + printf("%c\n",buf); + return buf; + } diff --git a/desktop/term.h b/desktop/term.h new file mode 100755 index 0000000..356992f --- /dev/null +++ b/desktop/term.h @@ -0,0 +1,3 @@ +#pragma once + +char getch (); diff --git a/desktop/types.h b/desktop/types.h new file mode 100755 index 0000000..da462d4 --- /dev/null +++ b/desktop/types.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +/* +File: Types +*/ + +#if defined(__GNUC__) || defined(__GNUG__) //Compiling with GNU C/C++ compiler + typedef __INT8_TYPE__ I8; + typedef __INT16_TYPE__ I16; + typedef __INT32_TYPE__ I32; + typedef __INT64_TYPE__ I64; + typedef __UINT8_TYPE__ U8; + typedef __UINT16_TYPE__ U16; + typedef __UINT32_TYPE__ U32; + typedef __UINT64_TYPE__ U64; +#else //Visual Studio + typedef __int8 I8; + typedef __int16 I16; + typedef __int32 I32; + typedef __int64 I64; + typedef unsigned __int8 U8; + typedef unsigned __int16 U16; + typedef unsigned __int32 U32; + typedef unsigned __int64 U64; +#endif -- cgit v1.2.3