From 6ee0dd161c300227a1f3e5e26848562ba1bc980c Mon Sep 17 00:00:00 2001
From: arielmw87 <arielmw87@gmail.com>
Date: Mon, 1 Jul 2024 19:10:00 +0000
Subject: [PATCH 1/5] update lib

---
 .gitignore  | 6 ++++++
 mbed-os.lib | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..269fd5d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+BUILD
+.mbed
+compile_commands.json
+.clangd
+.cache
+mbed-os
diff --git a/mbed-os.lib b/mbed-os.lib
index b566ab0..2dd8afb 100644
--- a/mbed-os.lib
+++ b/mbed-os.lib
@@ -1 +1 @@
-https://github.com/ARMmbed/mbed-os/#9a8c9e2c297f64c805698d72cd541ff3cd7fe538
\ No newline at end of file
+https://github.com/ARMmbed/mbed-os/#17dc3dc2e6e2817a8bd3df62f38583319f0e4fed
\ No newline at end of file

From 379cced3a2813d7b5fbaab0f81e94b12047b18e3 Mon Sep 17 00:00:00 2001
From: arielmw87 <arielmw87@gmail.com>
Date: Fri, 5 Jul 2024 19:35:48 +0000
Subject: [PATCH 2/5] actualizo codigo para TP1

---
 main.cpp | 188 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 169 insertions(+), 19 deletions(-)

diff --git a/main.cpp b/main.cpp
index 59930a8..4afaa28 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,32 +1,54 @@
 /*
- * Copyright (c) 2020 Arm Limited and affiliates.
- * SPDX-License-Identifier: Apache-2.0
- */
+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>
 
-// Create a DigitalOutput object to toggle an LED whenever data is received.
-static DigitalOut led(LED1);
 
-// Create a UnbufferedSerial object with a default baud rate.
-static UnbufferedSerial serial_port(USBTX, USBRX);
+//instancio objetos de botones
+DigitalIn start_button(PC_13);
+DigitalIn time_button(PB_5);
 
-void on_rx_interrupt()
-{
-    char c;
+//instancio obj. entrada analogica
+AnalogIn speed_input(PA_0);
 
-    // Toggle the LED.
-    led = !led;
+//instancio objetos de leds
+DigitalOut led_time_0(PA_3);
+DigitalOut led_time_1(PA_2);
+DigitalOut led_time_2(PA_10);
 
-    // 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);
-    }
-}
+DigitalOut motor(PB_3);
+
+//instancio uart driver
+UnbufferedSerial serial_port(USBTX, USBRX);
 
 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(
@@ -35,6 +57,134 @@ int main(void)
         /* 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);
+    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.
+        
+        //atiendo comandos seriales:
+        if(serial_port.readable( ){
+
+            
+        }
+
+        //leo la velocidad seteada
+        speed = speed_input * 255; // 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 < 255 ? pwm_cnt+1 : 0; //esto podria ser simplemente "pwm_cnt++" pero para asegurarse de que pegue la vuelta...
+
+            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;
+        }
+
+
+
+
+    }
 }

From 70d68273a049b4bc64d93adf95f866cd086f35b3 Mon Sep 17 00:00:00 2001
From: arielmw87 <arielmw87@gmail.com>
Date: Sat, 6 Jul 2024 00:29:20 +0000
Subject: [PATCH 3/5] agrego json de config para el %d

---
 mbed_app.json | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 mbed_app.json

diff --git a/mbed_app.json b/mbed_app.json
new file mode 100644
index 0000000..9cde318
--- /dev/null
+++ b/mbed_app.json
@@ -0,0 +1,7 @@
+{
+    "target_overrides": {
+        "*": {
+            "target.printf_lib": "std"
+        }
+    }
+}
\ No newline at end of file

From 6789775d1757bf286ff31a0bec5b363fc8677e58 Mon Sep 17 00:00:00 2001
From: arielmw87 <arielmw87@gmail.com>
Date: Sat, 6 Jul 2024 00:29:33 +0000
Subject: [PATCH 4/5] agrego manejo serial

---
 main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/main.cpp b/main.cpp
index 4afaa28..524679c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -40,7 +40,46 @@ DigitalOut led_time_2(PA_10);
 DigitalOut motor(PB_3);
 
 //instancio uart driver
-UnbufferedSerial serial_port(USBTX, USBRX);
+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)
 {
@@ -82,14 +121,16 @@ int main(void)
 
         thread_sleep_for(1); //uso de base de tiempo falopa, tambien me cambia frecuencia de pwm, polemiquisimo.
         
-        //atiendo comandos seriales:
-        if(serial_port.readable( ){
 
-            
+        if( manage_serial(speed, set_time) == true ){
+            working=true;
         }
 
+
+
+
         //leo la velocidad seteada
-        speed = speed_input * 255; // escalo a 0 a 255 para el pwm casero
+        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 ){
@@ -115,7 +156,7 @@ int main(void)
 
             //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 < 255 ? pwm_cnt+1 : 0; //esto podria ser simplemente "pwm_cnt++" pero para asegurarse de que pegue la vuelta...
+            pwm_cnt = pwm_cnt < 99 ? pwm_cnt+1 : 0;
 
             motor = pwm_cnt < speed ? 0 : 1 ; //pwm :s
         }else{

From 9ca699b8a70417c60ff8298539f7db769c98340e Mon Sep 17 00:00:00 2001
From: arielmw87 <arielmw87@gmail.com>
Date: Fri, 5 Jul 2024 21:36:18 -0300
Subject: [PATCH 5/5] Update README.md

---
 README.md | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 232bf50..ca46345 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,28 @@
-#UnbufferedSerial
+#Agitador magnetico - magnetic stirrer
 
-MIRRORED FROM MASTER EXAMPLE SNIPPETS REPOSITORY: mbed-os-examples-docs_only.
-ANY CHANGES MADE DIRECTLY TO THIS REPOSITORY WILL BE AUTOMATICALLY OVERWRITTEN.
+## descripcion:
+El sistema consta de un motor DC acoplado a imanes que hace girar un buzo magnético dentro de
+un erlenmeyer agitando su contenido, simulado por un led.
+El usuario debe poder controlar encendido y setear un tiempo de agitacion entre valores
+predefinidos y la velocidad mediante un potenciómetro. Como opcion para el usuario se podra
+setear y activar desde una terminal serie, además de monitorizar tiempo restante, velocidad seteada
+y temperatura.
+
+## 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)
+
+    
+Nota: partio de un fork de ejemplo serial de mbed, por lo que esta lleno de porquerias, hacer que quede bien y no perder el arbol de commits implica mucho bardo que no justifica, probablemente para siguientes TP simplemente copie la ultima version a un repo nuevo y que la historia se desvanezca.