Search
(Pro vyučující.)
NUCLEO-F446RE
prg-nucleo
main.cpp
#include "mbed.h" DigitalOut myled (LED1); // "Handle" to the LED. int main() { while (1) { myled = !myled; // Flip the state of the LED. ThisThread::sleep_for(1s); // Efficient MCU sleep. } }
Build project
struct
mybutton
fall()
mybutton.fall(&on_button_fall)
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton(BUTTON1); int main() { while (1) { if (mybutton == 0) { myled = 1; } else { myled = 0; } } }
myled
DigitalOut
int
write
Příklad řešení
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton(BUTTON1); int main() { bool mybutton_last = 0; while (1) { bool mybutton_current = mybutton; if (mybutton_last == 0 && mybutton_current == 1) { myled = !myled; } mybutton_last = mybutton_current; } }
Možné řešení
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton(BUTTON1); int main() { bool mybutton_last = 0; while (1) { bool mybutton_current = mybutton; if (mybutton_last == 0 && mybutton_current == 1) { ThisThread::sleep_for(100ms); // Efficient MCU sleep. if (mybutton) { myled = !myled; } } mybutton_last = mybutton_current; } }
#include "mbed.h" DigitalOut myled(LED1); InterruptIn mybutton(BUTTON1); void on_button_fall(){ myled = !myled; } int main() { mybutton.fall(&on_button_fall); ThisThread::sleep_for(1s); }
Timeout
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton_read(BUTTON1); InterruptIn mybutton_irq(BUTTON1); Timeout timeout; void on_timeout(){ if(mybutton_read == 0){ myled = !myled; } } void on_button_fall(){ timeout.attach(&on_timeout, 1s); } int main() { mybutton_irq.fall(&on_button_fall); while(1){ ThisThread::sleep_for(1s); } }
on_button_fall
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton(BUTTON1); // This will define the memory footprint and the total time window captured by the program. constexpr uint32_t BUFFER_SIZE = 500; constexpr Kernel::Clock::duration_u32 TIME_UNIT = 10ms; constexpr uint32_t RECORDING_SIGNAL_BLINK_COUNT = 0; // Here we will store what the user "wrote" using the button. bool buffer [BUFFER_SIZE]; // A generic blinking function to show the user mode changes. Note that this BLOCKS the program. void blink(uint32_t i, Kernel::Clock::duration_u32 t = 50ms){ for(; i > 0; i--){ myled = true; ThisThread::sleep_for(t); myled = false; ThisThread::sleep_for(t); } } void record(){ for(uint32_t i = 0; i < BUFFER_SIZE; i++){ // Take one sample from the middle of the sampling period. ThisThread::sleep_for(TIME_UNIT/2); buffer[i] = !mybutton; ThisThread::sleep_for(TIME_UNIT/2); // Tell the user that the sample was taken. Note, that this blocks! blink(RECORDING_SIGNAL_BLINK_COUNT); } } void replay(){ for(uint32_t i = 0; i < BUFFER_SIZE; i++){ myled = buffer[i]; ThisThread::sleep_for(TIME_UNIT); } myled=false; // Turn of the light after the show. } int main() { blink(6); record(); blink(6); replay(); blink(6); // The program ends. Use the black button to restart the MCU. }
Možná zlepšení:
uint32_t
constexpr uint32_t NUMBER_OF_SAMPLES = 500; constexpr uint32_t BITS_PER_INDEX = 8*sizeof(uint32_t); constexpr uint32_t NUMBER_OF_INDICES = NUMBER_OF_SAMPLES / BITS_PER_INDEX + (NUMBER_OF_SAMPLES % BITS_PER_INDEX ? 1 : 0); uint32_t memory [NUMBER_OF_INDICES];
x
bit = (number >> x) & 1;
bit = (number) & (x << 1);
1
number |= 1 << x;
0
number &= ~(1 << x);
number ^= 1 << x;
v
number = (number & ~(1U << x)) | (v << x);
Možná kostra řešení
#include "mbed.h" DigitalOut myled(LED1); DigitalIn mybutton(BUTTON1); constexpr Kernel::Clock::duration_u32 TIME_UNIT = 500ms; constexpr uint32_t NUMBER_OF_SAMPLES = 40; constexpr uint32_t BITS_PER_INDEX = 8*sizeof(uint32_t); constexpr uint32_t NUMBER_OF_INDICES = NUMBER_OF_SAMPLES / BITS_PER_INDEX + (NUMBER_OF_SAMPLES % BITS_PER_INDEX ? 1 : 0); uint32_t memory [NUMBER_OF_INDICES]; void set_bit_better(uint32_t sample, bool value){ uint32_t memory_index = sample / BITS_PER_INDEX; if(memory_index >= NUMBER_OF_INDICES){ // Velky Spatny. } uint8_t x = sample % BITS_PER_INDEX; memory[memory_index] = (memory[memory_index] & ~(1U << x)) | (value << x); } void set_bit(uint32_t sample, bool value){ uint32_t memory_index = sample / BITS_PER_INDEX; if(memory_index >= NUMBER_OF_INDICES){ // Velky Spatny. } uint8_t x = sample % BITS_PER_INDEX; if(value){ memory[memory_index] |= 1 << x; }else{ memory[memory_index] &= ~(1 << x); } } bool get_bit(uint32_t sample){ uint32_t memory_index = sample / BITS_PER_INDEX; if(memory_index >= NUMBER_OF_INDICES){ // Velky Spatny. } uint8_t x = sample % BITS_PER_INDEX; return (memory[memory_index] >> x) & 1; } void prepare_sos(){ set_bit(1, 1); set_bit(2, 0); set_bit(3, 1); set_bit(4, 0); set_bit(5, 1); set_bit(6, 0); set_bit(7, 0); set_bit(8, 0); set_bit_better(9, 1); set_bit_better(10, 1); set_bit_better(11, 1); set_bit_better(12, 0); set_bit_better(13, 1); set_bit_better(14, 1); set_bit_better(15, 1); set_bit_better(16, 0); set_bit_better(17, 1); set_bit_better(18, 1); set_bit_better(19, 1); set_bit_better(20, 0); set_bit_better(21, 0); set_bit_better(22, 0); set_bit(23, 1); set_bit(24, 0); set_bit(25, 1); set_bit(25, 0); set_bit(26, 1); } int main() { memory[0] = 88569621; // A little bit more succinct memory setup. //prepare_sos(); // Both lines of code result in equivalent data in the memory. while(1){ for(int i = 0; i < NUMBER_OF_SAMPLES; i++){ myled = get_bit(i); ThisThread::sleep_for(TIME_UNIT); } } }