Skip to content

Commit 6789775

Browse files
committed
agrego manejo serial
1 parent 70d6827 commit 6789775

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

main.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,46 @@ DigitalOut led_time_2(PA_10);
4040
DigitalOut motor(PB_3);
4141

4242
//instancio uart driver
43-
UnbufferedSerial serial_port(USBTX, USBRX);
43+
UnbufferedSerial serial_port(USBTX, USBRX); //seria ideal limitar el scope a una biblioteca de manejo serial
44+
45+
46+
// to do: pasar un puntero a una estructura que contenga todo el estado del aparato para poder monitorear y modificar mas libremente
47+
bool manage_serial(uint8_t vel, uint8_t tim){
48+
bool ret = false;
49+
//atiendo comandos seriales:
50+
char received = 0; // '\0'
51+
if( serial_port.readable() ){
52+
serial_port.read( &received, 1);
53+
switch(received){
54+
//---------------------------------------------------------------
55+
case 'A':
56+
serial_port.write( "Starting stirer\r\n", 17);// estaria bueno una funcion que le pases el const char* y cuente solo los caracteres.
57+
ret=true;
58+
break;
59+
//---------------------------------------------------------------
60+
case 'S':
61+
char str[30];
62+
sprintf(str,"La velocidad seteada es: %d\n", vel);
63+
serial_port.write( str, strlen(str) );
64+
65+
sprintf(str,"el tiempo seteado es: %d\n", tim);
66+
serial_port.write( str, strlen(str) );
67+
68+
//queda por implementar medicion de temperatura, pero no deja de ser solo otro A/D
69+
//sprintf(str,"La temperatura actual es %d\n", speed);
70+
//serial.write( str, strlen(str) );
71+
break;
72+
//---------------------------------------------------------------
73+
default:
74+
//availableCommands();
75+
break;
76+
//---------------------------------------------------------------
77+
}
78+
79+
}
80+
return ret;
81+
}
82+
4483

4584
int main(void)
4685
{
@@ -82,14 +121,16 @@ int main(void)
82121

83122
thread_sleep_for(1); //uso de base de tiempo falopa, tambien me cambia frecuencia de pwm, polemiquisimo.
84123

85-
//atiendo comandos seriales:
86-
if(serial_port.readable( ){
87124

88-
125+
if( manage_serial(speed, set_time) == true ){
126+
working=true;
89127
}
90128

129+
130+
131+
91132
//leo la velocidad seteada
92-
speed = speed_input * 255; // escalo a 0 a 255 para el pwm casero
133+
speed = speed_input * 99; // escalo a 0 a 255 para el pwm casero
93134

94135
//espero a que se aprete boton de start para arrancar
95136
if(start_button == 0 ){
@@ -115,7 +156,7 @@ int main(void)
115156

116157
//hago control de velocidad, bastante precario, sin control de frecuencia, fase, ni nada de nada, de casualidad cambia el ancho de pulso
117158
static uint8_t pwm_cnt=0;
118-
pwm_cnt = pwm_cnt < 255 ? pwm_cnt+1 : 0; //esto podria ser simplemente "pwm_cnt++" pero para asegurarse de que pegue la vuelta...
159+
pwm_cnt = pwm_cnt < 99 ? pwm_cnt+1 : 0;
119160

120161
motor = pwm_cnt < speed ? 0 : 1 ; //pwm :s
121162
}else{

0 commit comments

Comments
 (0)