-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.cpp
231 lines (183 loc) · 6.49 KB
/
main.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
descripcion: Control basico de agitador magnetico con control de set_time de agitado y velocidad del motor
Perifericos:
botones:
start: BUTTON1 (PC13)
cambiar set_time: D4 (PB5)
LEDs:
leds set_time: D0 (PA3)
D1 (PA2)
D2 (PA10)
Motor D3 (PB3)
Analogico:
Pote: A0 (PA0)
programmer: Ariel Wels
*/
#include "mbed.h"
#include <cstdint>
//instancio objetos de botones
DigitalIn start_button(PC_13);
DigitalIn time_button(PB_5);
//instancio obj. entrada analogica
AnalogIn speed_input(PA_0);
//instancio objetos de leds
DigitalOut led_time_0(PA_3);
DigitalOut led_time_1(PA_2);
DigitalOut led_time_2(PA_10);
DigitalOut motor(PB_3);
//instancio uart driver
UnbufferedSerial serial_port(USBTX, USBRX); //seria ideal limitar el scope a una biblioteca de manejo serial
// to do: pasar un puntero a una estructura que contenga todo el estado del aparato para poder monitorear y modificar mas libremente
bool manage_serial(uint8_t vel, uint8_t tim){
bool ret = false;
//atiendo comandos seriales:
char received = 0; // '\0'
if( serial_port.readable() ){
serial_port.read( &received, 1);
switch(received){
//---------------------------------------------------------------
case 'A':
serial_port.write( "Starting stirer\r\n", 17);// estaria bueno una funcion que le pases el const char* y cuente solo los caracteres.
ret=true;
break;
//---------------------------------------------------------------
case 'S':
char str[30];
sprintf(str,"La velocidad seteada es: %d\n", vel);
serial_port.write( str, strlen(str) );
sprintf(str,"el tiempo seteado es: %d\n", tim);
serial_port.write( str, strlen(str) );
//queda por implementar medicion de temperatura, pero no deja de ser solo otro A/D
//sprintf(str,"La temperatura actual es %d\n", speed);
//serial.write( str, strlen(str) );
break;
//---------------------------------------------------------------
default:
//availableCommands();
break;
//---------------------------------------------------------------
}
}
return ret;
}
int main(void)
{
//habilito pullups
start_button.mode(PullUp); // este ya esta externo en la placa pero no molesta.
time_button.mode(PullUp);
//config de serial port
// Set desired properties (9600-8-N-1).
serial_port.baud(9600);
serial_port.format(
/* bits */ 8,
/* parity */ SerialBase::None,
/* stop bit */ 1
);
/* comento porque creo que la idea de este tp no es usar irq, callbacks ni nada por el estilo.
// Register a callback to process a Rx (receive) interrupt.
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
*/
bool working=false;
uint8_t set_time = 0, speed=0;
uint32_t elapsed_time=0;
//infinite loop
while(true){
/*
// Read the data to clear the receive interrupt.
if (serial_port.read(&c, 1)) {
// Echo the input back to the terminal.
serial_port.write(&c, 1);
}
*/
thread_sleep_for(1); //uso de base de tiempo falopa, tambien me cambia frecuencia de pwm, polemiquisimo.
if( manage_serial(speed, set_time) == true ){
working=true;
}
//leo la velocidad seteada
speed = speed_input * 99; // escalo a 0 a 255 para el pwm casero
//espero a que se aprete boton de start para arrancar
if(start_button == 0 ){
//habilito a contar set_time
working=true;
}
//boton de cambio de tiempo seteado
if(time_button == 0){
//cambio set_time seteado
set_time++;
if(set_time >= 8 )
set_time = 0;
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
// si se disparo el funcionamiento, empiezo a contar tiempo y pongo el motor a la velocidad que va
if(working){
elapsed_time++;
//hago control de velocidad, bastante precario, sin control de frecuencia, fase, ni nada de nada, de casualidad cambia el ancho de pulso
static uint8_t pwm_cnt=0;
pwm_cnt = pwm_cnt < 99 ? pwm_cnt+1 : 0;
motor = pwm_cnt < speed ? 0 : 1 ; //pwm :s
}else{
motor=0;
}
//Si se acabo el tiempo apago (cuento en base a minutos, 60000 millisegundos)
if(elapsed_time >= ((uint32_t)set_time*60000)){
working = false;
elapsed_time=0;
}
//actualizo los leds con el set_time seleccionado
switch (set_time) {
//---------------------
case 0:
led_time_0 = 0;
led_time_1 = 0;
led_time_2 = 0;
break;
//---------------------
case 1:
led_time_0 = 1;
led_time_1 = 0;
led_time_2 = 0;
break;
//---------------------
case 2:
led_time_0 = 0;
led_time_1 = 1;
led_time_2 = 0;
break;
//---------------------
case 3:
led_time_0 = 1;
led_time_1 = 1;
led_time_2 = 0;
break;
//---------------------
case 4:
led_time_0 = 0;
led_time_1 = 0;
led_time_2 = 1;
break;
//---------------------
case 5:
led_time_0 = 1;
led_time_1 = 0;
led_time_2 = 1;
break;
//---------------------
case 6:
led_time_0 = 0;
led_time_1 = 1;
led_time_2 = 1;
break;
//---------------------
case 7:
led_time_0 = 1;
led_time_1 = 1;
led_time_2 = 1;
break;
//---------------------
default:
set_time = 0;
}
}
}