Compare commits
6 Commits
bf058182f5
...
b3a228c3ac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3a228c3ac | ||
|
|
10e11858f3 | ||
|
|
662107fb28 | ||
|
|
88e68268f3 | ||
|
|
3c11e1dff8 | ||
|
|
915c573b27 |
BIN
Cvičenie_5.tar
Normal file
BIN
Cvičenie_5.tar
Normal file
Binary file not shown.
46
cvicenie_5/Button.cpp
Normal file
46
cvicenie_5/Button.cpp
Normal file
@@ -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;
|
||||||
|
};
|
||||||
31
cvicenie_5/Button.h
Normal file
31
cvicenie_5/Button.h
Normal file
@@ -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
|
||||||
66
cvicenie_5/Led.cpp
Normal file
66
cvicenie_5/Led.cpp
Normal file
@@ -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<float>(period);
|
||||||
|
ticker.attach(callback(this, &Led::tickerHandler), std::chrono::duration_cast<std::chrono::microseconds>(float_interval));
|
||||||
|
}
|
||||||
33
cvicenie_5/Led.h
Normal file
33
cvicenie_5/Led.h
Normal file
@@ -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
|
||||||
57
cvicenie_5/Uart.cpp
Normal file
57
cvicenie_5/Uart.cpp
Normal file
@@ -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;
|
||||||
|
};
|
||||||
28
cvicenie_5/Uart.h
Normal file
28
cvicenie_5/Uart.h
Normal file
@@ -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
|
||||||
39
cvicenie_5/main.cpp
Normal file
39
cvicenie_5/main.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "mbed.h"
|
||||||
|
#include "Led.h"
|
||||||
|
#include "Button.h"
|
||||||
|
#include "Uart.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,57 +2,31 @@
|
|||||||
|
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
|
|
||||||
#define MAXIMUM_BUFFER_SIZE 32
|
DigitalOut led(LED1);
|
||||||
|
|
||||||
DigitalOut led1(LED1);
|
|
||||||
static BufferedSerial serial_port(USBTX, USBRX);
|
|
||||||
|
|
||||||
class Led {
|
|
||||||
private:
|
|
||||||
Ticker ticker;
|
Ticker ticker;
|
||||||
bool ledState;
|
|
||||||
|
|
||||||
public:
|
void toggle_led() { led = !led; }
|
||||||
Led() : ledState(false) {
|
|
||||||
led1 = ledState;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setBlinkPeriod(float period) {
|
int main() {
|
||||||
ticker.detach();
|
BufferedSerial serial(USBTX, USBRX);
|
||||||
ticker.attach(callback(this, &Led::tickerHandler), period / 2.0f);
|
serial.set_baud(9600);
|
||||||
}
|
|
||||||
|
|
||||||
void tickerHandler() {
|
char buf[32];
|
||||||
ledState = !ledState;
|
char msg[] = "Zadajte cislicu 1-9:\r\n";
|
||||||
led1 = ledState;
|
serial.write(msg, sizeof(msg));
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main()
|
// Use explicit chrono type for full compatibility
|
||||||
{
|
ticker.attach(&toggle_led, std::chrono::seconds(1));
|
||||||
serial_port.set_baud(9600);
|
|
||||||
serial_port.set_format(
|
|
||||||
/* bits */ 8,
|
|
||||||
/* parity */ BufferedSerial::None,
|
|
||||||
/* stop bit */ 1
|
|
||||||
);
|
|
||||||
|
|
||||||
char buf[MAXIMUM_BUFFER_SIZE] = {0};
|
|
||||||
Led ledController;
|
|
||||||
ledController.setBlinkPeriod(1.0f);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
|
if (uint32_t num = serial.read(buf, sizeof(buf))) {
|
||||||
if (buf[0] >= '0' && buf[0] <= '9') {
|
char c = buf[0];
|
||||||
float period = buf[0] - '0';
|
if (c >= '1' && c <= '9') {
|
||||||
if (period > 0) {
|
int period = c - '0';
|
||||||
ledController.setBlinkPeriod(period);
|
ticker.detach();
|
||||||
} else {
|
ticker.attach(&toggle_led, std::chrono::seconds(period));
|
||||||
ledController.setBlinkPeriod(0.1f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serial_port.write(buf, num);
|
|
||||||
}
|
|
||||||
ThisThread::sleep_for(100ms);
|
ThisThread::sleep_for(100ms);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
#define TARGET_TX_PIN USBTX
|
#define TARGET_TX_PIN USBTX
|
||||||
#define TARGET_RX_PIN USBRX
|
#define TARGET_RX_PIN USBRX
|
||||||
#define BAUD_RATE 9600
|
#define BAUD_RATE 1843200
|
||||||
|
// #define BAUD_RATE 460800
|
||||||
|
// #define BAUD_RATE 921600
|
||||||
|
|
||||||
static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, BAUD_RATE);
|
static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, BAUD_RATE);
|
||||||
|
|
||||||
@@ -18,25 +20,97 @@ DigitalOut led(LED1);
|
|||||||
#define BUFFER_SIZE 64
|
#define BUFFER_SIZE 64
|
||||||
static char rx_buffer[BUFFER_SIZE];
|
static char rx_buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
// Draw a moving “sky + ground” background with optional message
|
||||||
|
void draw_mask(uint32_t mask, int shift, const char *text = nullptr)
|
||||||
|
{
|
||||||
|
const int width = 80; // terminal columns
|
||||||
|
const int height = 24; // terminal rows
|
||||||
|
const int horizon = 16; // sky ends here, ground below
|
||||||
|
|
||||||
|
printf("\033[2J\033[H"); // clear + home
|
||||||
|
|
||||||
|
// draw sky layer (moving clouds)
|
||||||
|
for (int y = 0; y < horizon; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
// Parallax: slower movement at top
|
||||||
|
int move = (shift / 2 + y) % width;
|
||||||
|
char c = ((x + move) % 11 == 0) ? '☁' : ' ';
|
||||||
|
printf("%c", c);
|
||||||
|
}
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw horizon line
|
||||||
|
for (int i = 0; i < width; i++) printf("-");
|
||||||
|
printf("\r\n");
|
||||||
|
|
||||||
|
// draw ground (grass / terrain)
|
||||||
|
for (int y = horizon + 1; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
int move = (shift * 2 + y) % width;
|
||||||
|
// simple wave pattern for terrain / hill
|
||||||
|
char c;
|
||||||
|
if (((x + move) % 7) == 0)
|
||||||
|
c = '^';
|
||||||
|
else if (((x + move) % 5) == 0)
|
||||||
|
c = '`';
|
||||||
|
else
|
||||||
|
c = ' ';
|
||||||
|
printf("%c", c);
|
||||||
|
}
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// optional text overlay at bottom
|
||||||
|
if (text && text[0] != '\0') {
|
||||||
|
printf("\r\n[RX] %s\r\n", text);
|
||||||
|
}
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
serial_port.set_format(8, BufferedSerial::None, 1);
|
serial_port.set_format(8, BufferedSerial::None, 1);
|
||||||
|
|
||||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||||
|
|
||||||
|
uint32_t mask = 0xF0F0A55A;
|
||||||
|
int shift = 0;
|
||||||
|
char message[BUFFER_SIZE] = {0};
|
||||||
|
bool message_active = false;
|
||||||
|
Timer msg_timer;
|
||||||
|
Timer anim_timer;
|
||||||
|
msg_timer.start();
|
||||||
|
anim_timer.start();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (serial_port.readable()) {
|
if (serial_port.readable()) {
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
||||||
|
|
||||||
if (num > 0) {
|
if (num > 0) {
|
||||||
led = !led;
|
led = !led;
|
||||||
|
|
||||||
serial_port.write(rx_buffer, num);
|
strncpy(message, rx_buffer, sizeof(message) - 1);
|
||||||
|
message_active = true;
|
||||||
printf("[RX] %s\r\n", rx_buffer);
|
msg_timer.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(10ms);
|
if (message_active && msg_timer.elapsed_time() > 1s) {
|
||||||
|
message_active = false;
|
||||||
|
memset(message, 0, sizeof(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (message_active && msg_timer.elapsed_time() > 100ms) {
|
||||||
|
// shift = (shift + 1) % 8;
|
||||||
|
|
||||||
|
// }
|
||||||
|
if (anim_timer.elapsed_time() >= 200ms) {
|
||||||
|
shift = (shift + 1) % 8;
|
||||||
|
anim_timer.reset();
|
||||||
|
|
||||||
|
draw_mask(mask, shift, message_active ? message : nullptr);
|
||||||
|
}
|
||||||
|
ThisThread::sleep_for(20ms);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user