aboutsummaryrefslogtreecommitdiff
path: root/desktop/main.cc
blob: ba77e4bc7ab84c302fc46f9375118d1e3ac89ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "serial_utils.h"
#include "term.h"
#include "types.h"

#include <SerialStream.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>

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 <typename T>
bool writeSafe (SerialStream& serial, MessageHeader header, const T& val)
{
    write<U8>(serial, (U8)header);
    write<T>(serial, val);

    U8 errorCode = read<U8>(serial);
    T x = read<U32>(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<U32>(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<U32>(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<U32>(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<U8>(serial, (U8)ReadConfig);

    U8 errorCode = read<U8>(serial);
    if (errorCode == Ok)
    {
        config.startDelay    = read<U32>(serial);
        config.rpmCount      = read<U32>(serial);
        config.signalsPerRPM = read<U32>(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 <args>\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;
}