aboutsummaryrefslogtreecommitdiff
path: root/desktop
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2021-12-04 22:22:47 -0800
committer3gg <3gg@shellblade.net>2021-12-04 22:22:47 -0800
commite62be3b12d3c94d709a77d89f52c31f7c4ac475d (patch)
treebb351f85960443086730f0354f829958b2d03190 /desktop
parent48481ba9032689e1bceba05c98bc69972e1a2d3b (diff)
Initial commit.
Diffstat (limited to 'desktop')
-rwxr-xr-xdesktop/main.cc215
-rwxr-xr-xdesktop/rpmcount.pro15
-rwxr-xr-xdesktop/serial_utils.h33
-rwxr-xr-xdesktop/term.cc28
-rwxr-xr-xdesktop/term.h3
-rwxr-xr-xdesktop/types.h27
6 files changed, 321 insertions, 0 deletions
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 @@
1#include "serial_utils.h"
2#include "term.h"
3#include "types.h"
4
5#include <SerialStream.h>
6#include <iostream>
7#include <cstdio>
8#include <cstdlib>
9
10using namespace std;
11using namespace LibSerial;
12
13enum ErrorCode
14{
15 Ok,
16 Error
17};
18
19enum MessageHeader
20{
21 ReadConfig,
22 UpdateStartDelay,
23 UpdateRPMCount,
24 UpdateSignalsPerRPM
25};
26
27struct Config
28{
29 U32 startDelay; // Start delay in milliseconds
30 U32 rpmCount; // Number of RPM ticks to count
31 U32 signalsPerRPM; // Number of signal ticks per RPM tick
32};
33
34template <typename T>
35bool writeSafe (SerialStream& serial, MessageHeader header, const T& val)
36{
37 write<U8>(serial, (U8)header);
38 write<T>(serial, val);
39
40 U8 errorCode = read<U8>(serial);
41 T x = read<U32>(serial);
42
43 fprintf(stderr, "error code: %d\n", errorCode);
44 fprintf(stderr, "val: %d\n", x);
45
46 return errorCode == Ok && x == val;
47}
48
49bool configureStartDelay (SerialStream& serial, Config& config)
50{
51 printf("Tiempo de retardo (milisegundos)\n");
52 printf("--------------------------------\n");
53 printf("\n");
54 printf("Valor actual : %d\n", config.startDelay);
55 printf("\n");
56 printf("Nuevo valor : ");
57
58 U32 x;
59 cin >> x;
60
61 bool result = writeSafe<U32>(serial, UpdateStartDelay, x);
62 if (result)
63 {
64 config.startDelay = x;
65 }
66 return result;
67}
68
69bool configureRPMCount (SerialStream& serial, Config& config)
70{
71 printf("RPM maximo\n");
72 printf("----------\n");
73 printf("\n");
74 printf("Valor actual : %d\n", config.rpmCount);
75 printf("\n");
76 printf("Nuevo valor : ");
77
78 U32 x;
79 cin >> x;
80
81 bool result = writeSafe<U32>(serial, UpdateRPMCount, x);
82 if (result)
83 {
84 config.rpmCount = x;
85 }
86 return result;
87}
88
89bool configureSignalsPerRPM (SerialStream& serial, Config& config)
90{
91 printf("Impulsos/RPM\n");
92 printf("------------\n");
93 printf("\n");
94 printf("Valor actual : %d\n", config.signalsPerRPM);
95 printf("\n");
96 printf("Nuevo valor : ");
97
98 U32 x;
99 cin >> x;
100
101 bool result = writeSafe<U32>(serial, UpdateSignalsPerRPM, x);
102 if (result)
103 {
104 config.signalsPerRPM = x;
105 }
106 return result;
107}
108
109void loop (SerialStream& serial, Config& config)
110{
111 int option = -1;
112
113 while (option != 0)
114 {
115 system("clear");
116 printf("Configuracion\n");
117 printf("-------------\n");
118 printf("Tiempo de retardo : %d\n", config.startDelay);
119 printf("RPM maximo : %d\n", config.rpmCount);
120 printf("Impulsos/rpm : %d\n", config.signalsPerRPM);
121 printf("\n");
122 printf("Menu\n");
123 printf("----\n");
124 printf("[0] Salir\n");
125 printf("[1] Configurar tiempo de retardo\n");
126 printf("[2] Configurar RPM maximo\n");
127 printf("[3] Configurar impulsos/RPM\n");
128 printf("\n");
129 printf("> ");
130
131 option = getch() - '0';
132 system("clear");
133
134 bool error = false;
135
136 switch (option)
137 {
138 case 1: error = !configureStartDelay(serial, config); break;
139 case 2: error = !configureRPMCount(serial, config); break;
140 case 3: error = !configureSignalsPerRPM(serial, config); break;
141 default: break;
142 }
143
144 if (error)
145 {
146 fprintf(stderr, "Error al escribir valor\n");
147 return;
148 }
149 }
150}
151
152void readConfig (SerialStream& serial, Config& config)
153{
154 write<U8>(serial, (U8)ReadConfig);
155
156 U8 errorCode = read<U8>(serial);
157 if (errorCode == Ok)
158 {
159 config.startDelay = read<U32>(serial);
160 config.rpmCount = read<U32>(serial);
161 config.signalsPerRPM = read<U32>(serial);
162 }
163}
164
165void openArduino (const char* serialDevice, SerialStream& serial)
166{
167 if (serial.IsOpen()) serial.Close();
168 serial.Open(serialDevice);
169 serial.SetBaudRate(SerialStreamBuf::BAUD_9600);
170 serial.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
171 serial.SetNumOfStopBits(1);
172 serial.SetParity(SerialStreamBuf::PARITY_DEFAULT);
173 serial.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_DEFAULT);
174}
175
176void usage (const char* argv0)
177{
178 fprintf(stderr, "Uso:\n");
179 fprintf(stderr, "\n");
180 fprintf(stderr, "%s <args>\n", argv0);
181 fprintf(stderr, "\n");
182 fprintf(stderr, "Args:\n");
183 fprintf(stderr, "\n");
184 fprintf(stderr, "dispositivo - Nombre de dispositivo serie\n");
185}
186
187int main (int argc, char** argv)
188{
189 if (argc != 2)
190 {
191 usage(argv[0]);
192 exit(0);
193 }
194
195 const char* serialDevice = argv[1];
196
197 SerialStream serial;
198 printf("Conectando con %s...\n", serialDevice);
199 openArduino(serialDevice, serial);
200 if (serial.IsOpen())
201 {
202 sleep(2); // Wait for arduino to wake up
203 printf("Leyendo configuracion...\n");
204 Config config;
205 readConfig(serial, config);
206 loop(serial, config);
207 }
208 else
209 {
210 printf("Error al conectar con %s\n", serialDevice);
211 cin.get();
212 }
213
214 return 0;
215}
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 @@
1TEMPLATE = app
2CONFIG += console
3CONFIG -= app_bundle
4CONFIG -= qt
5
6LIBS += -lserial
7
8SOURCES += \
9 main.cc \
10 term.cc
11
12HEADERS += \
13 types.h \
14 serial_utils.h \
15 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 @@
1#pragma once
2
3#include "types.h"
4
5#include <SerialStream.h>
6
7template <typename T>
8T read (LibSerial::SerialStream& serial)
9{
10 T x;
11 serial.read((char*) &x, sizeof(T));
12 return x;
13}
14
15template <>
16U32 read<U32> (LibSerial::SerialStream& serial)
17{
18 U32 x = 0;
19 for (int i = 0; i < 4; ++i)
20 {
21 U8 b;
22 serial.read((char*)&b, 1);
23 x = x >> 8;
24 x |= (b << 24);
25 }
26 return x;
27}
28
29template <typename T>
30void write (LibSerial::SerialStream& serial, const T& val)
31{
32 serial.write((const char*) &val, sizeof(T));
33}
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 @@
1#include "term.h"
2
3#include <cstdio>
4#include <unistd.h> //_getch*/
5#include <termios.h> //_getch*/
6
7char getch ()
8{
9 char buf=0;
10 struct termios old={0};
11 fflush(stdout);
12 if(tcgetattr(0, &old)<0)
13 perror("tcsetattr()");
14 old.c_lflag&=~ICANON;
15 old.c_lflag&=~ECHO;
16 old.c_cc[VMIN]=1;
17 old.c_cc[VTIME]=0;
18 if(tcsetattr(0, TCSANOW, &old)<0)
19 perror("tcsetattr ICANON");
20 if(read(0,&buf,1)<0)
21 perror("read()");
22 old.c_lflag|=ICANON;
23 old.c_lflag|=ECHO;
24 if(tcsetattr(0, TCSADRAIN, &old)<0)
25 perror ("tcsetattr ~ICANON");
26 printf("%c\n",buf);
27 return buf;
28 }
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 @@
1#pragma once
2
3char 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 @@
1#pragma once
2
3#include <stdint.h>
4
5/*
6File: Types
7*/
8
9#if defined(__GNUC__) || defined(__GNUG__) //Compiling with GNU C/C++ compiler
10 typedef __INT8_TYPE__ I8;
11 typedef __INT16_TYPE__ I16;
12 typedef __INT32_TYPE__ I32;
13 typedef __INT64_TYPE__ I64;
14 typedef __UINT8_TYPE__ U8;
15 typedef __UINT16_TYPE__ U16;
16 typedef __UINT32_TYPE__ U32;
17 typedef __UINT64_TYPE__ U64;
18#else //Visual Studio
19 typedef __int8 I8;
20 typedef __int16 I16;
21 typedef __int32 I32;
22 typedef __int64 I64;
23 typedef unsigned __int8 U8;
24 typedef unsigned __int16 U16;
25 typedef unsigned __int32 U32;
26 typedef unsigned __int64 U64;
27#endif