WIP: keep local changes before merging remote
This commit is contained in:
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,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<long long>(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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user