From 3c11e1dff845e61e0d8906daabda0bdc45064959 Mon Sep 17 00:00:00 2001 From: Filipriec Date: Thu, 13 Nov 2025 14:42:47 +0100 Subject: [PATCH] WIP: keep local changes before merging remote --- Cvičenie_5.tar | Bin 0 -> 11264 bytes cvicenie_5/Button.cpp | 46 +++++++++++++++++++++++++++++ cvicenie_5/Button.h | 31 +++++++++++++++++++ cvicenie_5/Led.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ cvicenie_5/Led.h | 33 +++++++++++++++++++++ cvicenie_5/Uart.cpp | 57 +++++++++++++++++++++++++++++++++++ cvicenie_5/Uart.h | 28 ++++++++++++++++++ cvicenie_5/main.cpp | 39 ++++++++++++++++++++++++ hod5a/main.cpp | 67 +++++++++++++----------------------------- 9 files changed, 320 insertions(+), 47 deletions(-) create mode 100644 Cvičenie_5.tar create mode 100644 cvicenie_5/Button.cpp create mode 100644 cvicenie_5/Button.h create mode 100644 cvicenie_5/Led.cpp create mode 100644 cvicenie_5/Led.h create mode 100644 cvicenie_5/Uart.cpp create mode 100644 cvicenie_5/Uart.h create mode 100644 cvicenie_5/main.cpp diff --git a/Cvičenie_5.tar b/Cvičenie_5.tar new file mode 100644 index 0000000000000000000000000000000000000000..15c693b717be80e88f81f21ae200eb22be8b052c GIT binary patch literal 11264 zcmeHN-*4MS67Dnqifw|ksl<^;*_KnrGEmt@8^E;%#m3!30f8b{5fO^i@rRwDaQ^oD zW@ncoC8su0>!DZi!6Ij8XJ%)2XTI6>+>erWuvq-C!N+l&-ob(R0l&_H+x!Ko>pF+R zJvela4)+h;UROA-(>po@-v(`}M3;HtXE3cIeCG=d&T)8f^!qn?UPeg}m!T9d<`WsV zXD_TJ1wE_3T;D5p^(1sKM^jLy$51wX^n@e$rS9dU8yE?XP?gNvJU%`S$6C4X1(Q)`2L z1RKmV{AJuV;`bFhS}X3G59g!t;O6@Jip_yB?!Gc+;0jS|wX7&9#2h=!Zdrd=7KuNL zK1PKfUzLT(WH^pxXw%>zzx9sVF{H~Y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)39ZISTkY31n2;m9< zr_-rmlW1M*hgOyXp*1g6{xdBrLSS~a@_6{X@ZdZa%v#!9ovalz7v#u>Z*Xp>he!rP zMktX60%T3C$S-lyqkJ(|$rA|;+XzRgo)^rrG)X-#EHh-0G&xpmC)!13RNrCIgVM)= zpBHq7yHq@nf-KD?j;O-?gjZ>0B+4}FexosKEByI9fQ7kP@DEcT$^Xt#_rT!)UUy&R zf4AG+;{WHH(qyaT6DD z7(550qwejR2Z@F{Ufy=$`b_Ndf+vvlfp)3PBixZRoqLH zfqCbCVJlAJ?z|F{sK~{uj(EFQr!IWl_x}MSJ_t}35K+*PB(POPgg~G{6<|0;WRn^E&fO}kR&4V7*m$#-3 zbHK;8>-8Bc|EeaRrj9sNsO5&$J6L`A46trwuVQsWZ_YbSh^{FfIzZ+aU1O@9AS)-Z zHHf|=y{t~vRj)QjOE#*Pi>p?{bZR>j;Ld?TtdzDuwDWgQ60V_)$3Qz3A`bn|j;?VUSc<<>eW6Qbqz}5r* H?;iLcKp}J( literal 0 HcmV?d00001 diff --git a/cvicenie_5/Button.cpp b/cvicenie_5/Button.cpp new file mode 100644 index 0000000..56dd7b0 --- /dev/null +++ b/cvicenie_5/Button.cpp @@ -0,0 +1,46 @@ +#include "Button.h" + +// Constructor +Button::Button(PinName pin) + : button(pin) +{ + counter = 0; + pressed = false; + button.rise(callback(this, &Button::interrupt)); // attach increment function of this counter instance + // set pin mode : PullUp / PullDown / PullNone / OpenDrain +}; + + +void Button::interrupt() +{ + pressed = true; + counter++; + if(counter >= 8) + counter=0; +}; + +// Destructor +Button::~Button() +{ +}; + +bool Button::read(void) +{ + return button.read(); +}; + +bool Button::wasPressed(void) +{ + if(pressed) + { + pressed = false; + return true; + } + else + return false; +}; + +uint16_t Button::getCount(void) +{ + return counter; +}; \ No newline at end of file diff --git a/cvicenie_5/Button.h b/cvicenie_5/Button.h new file mode 100644 index 0000000..f89a20f --- /dev/null +++ b/cvicenie_5/Button.h @@ -0,0 +1,31 @@ +#ifndef __BUTTON_H +#define __BUTTON_H + +#include "mbed.h" + +// class Led +class Button +{ +private: + void interrupt(); +public: + //! Constructor + Button(PinName pin); + + //! Destructor + ~Button(); + + // get value + bool read(void); + + bool wasPressed(void); + // get number of button pressed + uint16_t getCount(void); + +private: + InterruptIn button; + volatile bool pressed; + volatile uint16_t counter; +}; + +#endif diff --git a/cvicenie_5/Led.cpp b/cvicenie_5/Led.cpp new file mode 100644 index 0000000..7a1147e --- /dev/null +++ b/cvicenie_5/Led.cpp @@ -0,0 +1,66 @@ +#include "Led.h" + +// Constructor +Led::Led(PinName pin) + : led(pin) +{ + led.period_ms(10); + led = 0; +}; + +// Destructor +Led::~Led() +{ +}; + +void Led::set(bool s) +{ + if(s) + led = 1; + else + led = 0; +}; + +void Led::setDuty(float duty) +{ + led.write(duty); +}; + +float Led::getDuty() +{ + return led.read(); // Return the current output duty-cycle setting +} + +void Led::graduallyOn() +{ + for(float i=0 ; i<=1; i=i+0.01) + { + this->setDuty(i); + ThisThread::sleep_for(20ms); + } + led = 0; +}; + +PwmOut* Led::getPwmOut() +{ + return &led; +}; + +void Led::tickerHandler(void) +{ + led = !led; +}; + +void Led::setBlinkPeriod(float period) +{ + if(period<=0) + { + ticker.detach(); // turn off ticker +// set(false); + this->set(false); // turn off led + return; + } + // ticker.attach(callback(this, &Led::tickerHandler), period); // deprecated + auto float_interval = std::chrono::duration(period); + ticker.attach(callback(this, &Led::tickerHandler), std::chrono::duration_cast(float_interval)); +} \ No newline at end of file diff --git a/cvicenie_5/Led.h b/cvicenie_5/Led.h new file mode 100644 index 0000000..f843a29 --- /dev/null +++ b/cvicenie_5/Led.h @@ -0,0 +1,33 @@ +#ifndef __LED_H +#define __LED_H + +#include "mbed.h" + +// class Led +class Led +{ +private: + +public: + //! Constructor + Led(PinName pin); + //! Destructor + ~Led(); + // set value + void set(bool s); + // set duty + void setDuty(float duty); + // get duty + float getDuty(); + void graduallyOn(); + PwmOut* getPwmOut(); + void setBlinkPeriod(float period); + void tickerHandler(); + + +private: + PwmOut led; + Ticker ticker; + +}; +#endif diff --git a/cvicenie_5/Uart.cpp b/cvicenie_5/Uart.cpp new file mode 100644 index 0000000..4017bfd --- /dev/null +++ b/cvicenie_5/Uart.cpp @@ -0,0 +1,57 @@ +#include "Uart.h" + +// Constructor +Uart::Uart(PinName tx, PinName rx, int baud = 9600) + : serial(tx, rx, baud) +{ + received = false; + received_character = ' '; + // Set desired properties + serial.baud(baud); + serial.format( + /* bits */ 8, + /* parity */ SerialBase::None, + /* stop bit */ 1 + ); + serial.attach(callback(this, &Uart::rxInterrupt), SerialBase::RxIrq); +}; + + +void Uart::rxInterrupt() +{ + char c; + serial.read(&c, 1); + received_character = c; + received = true; +}; + +// Destructor +Uart::~Uart() +{ +}; + +bool Uart::isCharReceived() +{ + if(received) + { + received = false; + return true; + } + else + return false; +}; + +char Uart::getReceivedChar() +{ + return received_character; +}; + +void Uart::writeChar(char c) +{ + serial.write(&c, 1); +}; + +UnbufferedSerial* Uart::getSerial() +{ + return &serial; +}; \ No newline at end of file diff --git a/cvicenie_5/Uart.h b/cvicenie_5/Uart.h new file mode 100644 index 0000000..4070784 --- /dev/null +++ b/cvicenie_5/Uart.h @@ -0,0 +1,28 @@ +#ifndef __UART_H +#define __UART_H + +#include "mbed.h" + +// class Led +class Uart +{ +private: + void rxInterrupt(); +public: + //! Constructor + Uart(PinName rx, PinName tx, int baud); + + //! Destructor + ~Uart(); + + bool isCharReceived(); + char getReceivedChar(); + void writeChar(char c); + UnbufferedSerial* getSerial(); +private: + UnbufferedSerial serial; + volatile char received_character; + volatile bool received; +}; + +#endif diff --git a/cvicenie_5/main.cpp b/cvicenie_5/main.cpp new file mode 100644 index 0000000..c6827ef --- /dev/null +++ b/cvicenie_5/main.cpp @@ -0,0 +1,39 @@ +#include "mbed.h" +#include "Led.h" +#include "Button.h" +#include "Uart.h" +#include +#include +#include + +//LED and button PIN names +#define LED_GREEN PC_7 // LD1 LED_GREEN +#define LED_BLUE PB_7 // LD2 LED_BLUE +#define LED_RED PG_2 // LD3 LED_RED +#define USER_BUTTON PC_13 // B1 USER_BUTTON + +int main() +{ + + DigitalOut red_led(LED_RED); // NO PWM on this pin + Led green_led(LED_GREEN), blue_led(LED_BLUE); + Button btn(USER_BUTTON); + Uart pc(USBTX, USBRX, 115200); // Baud Rate = 115200 + char c; + uint8_t num; + printf("---------START-------\r\n"); + printf("Press number : \r\n"); + while (true) { + if (pc.isCharReceived()) { + c = pc.getReceivedChar(); + printf("\r\n"); + //pc.writeChar(pc.getReceivedChar()); + if((c >='0') && (c <= '9')) + { + num = c - '0'; // conversion of character to number + printf("Received number : %d \r\n", num); + green_led.setBlinkPeriod(num); + } + } + } +} diff --git a/hod5a/main.cpp b/hod5a/main.cpp index 229f177..90c6c0a 100644 --- a/hod5a/main.cpp +++ b/hod5a/main.cpp @@ -2,58 +2,31 @@ #include "mbed.h" -#define MAXIMUM_BUFFER_SIZE 32 +DigitalOut led(LED1); +Ticker ticker; -class Led { -private: - DigitalOut led; - Ticker ticker; - float period; - -public: - Led(PinName pin) : led(pin), period(1.0) { - led = 0; - } - - void setBlinkPeriod(float period) { - this->period = period; - ticker.detach(); - if (period > 0) { - auto us = std::chrono::microseconds(static_cast(period * 1000000)); - ticker.attach(callback(this, &Led::tickerHandler), us); - } - } - - void tickerHandler() { - led = !led; - } -}; +void toggle_led() { led = !led; } int main() { - Led myLed(LED1); - static BufferedSerial serial_port(USBTX, USBRX); - - serial_port.set_baud(9600); - serial_port.set_format(8, BufferedSerial::None, 1); - - char buf[MAXIMUM_BUFFER_SIZE] = {0}; - char msg[] = "Zadaj cislicu 0-9:\r\n"; - serial_port.write(msg, sizeof(msg)); - - while(1) { - if (uint32_t num = serial_port.read(buf, sizeof(buf))) { + BufferedSerial serial(USBTX, USBRX); + serial.set_baud(9600); + + char buf[32]; + char msg[] = "Zadajte cislicu 1-9:\r\n"; + serial.write(msg, sizeof(msg)); + + // Use explicit chrono type for full compatibility + ticker.attach(&toggle_led, std::chrono::seconds(1)); + + while (1) { + if (uint32_t num = serial.read(buf, sizeof(buf))) { char c = buf[0]; - - if (c >= '0' && c <= '9') { - int digit = c - '0'; - float newPeriod = (float)digit; - - myLed.setBlinkPeriod(newPeriod); - - serial_port.write(buf, num); + if (c >= '1' && c <= '9') { + int period = c - '0'; + ticker.detach(); + ticker.attach(&toggle_led, std::chrono::seconds(period)); } } - - ThisThread::sleep_for(150ms); + ThisThread::sleep_for(100ms); } }