#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);
}
}
}