Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
Diff: Utility.cpp
- Revision:
- 15:b38d9d210e32
- Parent:
- 13:bb9669053eb3
- Child:
- 18:a21199781d20
--- a/Utility.cpp Mon Jan 16 02:28:40 2017 +0000 +++ b/Utility.cpp Tue Jan 17 00:06:38 2017 +0000 @@ -1,4 +1,5 @@ #include "Utility.hpp" +#include "mbed.h" #include <cmath> @@ -24,20 +25,35 @@ unsigned int update_bit(const unsigned int previous_4_bytes, const int position, const bool new_bit_value) { - return update_bits(previous_4_bytes, position, position, new_bit_value); + return update_bits(previous_4_bytes, position, position, 0xFFFFFFFF, new_bit_value); } - unsigned int update_bits(const unsigned int previous_4_bytes, const int start_bit, const int stop_bit, const unsigned int new_bits) + unsigned int update_bits(const unsigned int previous_4_bytes, const int start_bit, const int stop_bit, const unsigned int reserved_bits_mask, const unsigned int new_bits) { - const int all_ones_but_n_zeros_right_shifted = (0xFFFFFFFF << (stop_bit - start_bit + 1)); - const int all_zeros_but_n_ones_right_shifted = ~all_ones_but_n_zeros_right_shifted; + const unsigned int all_ones_but_n_zeros_right_shifted = (0xFFFFFFFF << (stop_bit - start_bit + 1)); + const unsigned int all_zeros_but_n_ones_right_shifted = ~all_ones_but_n_zeros_right_shifted; + + const unsigned int all_zeros_but_ones_at_position = all_zeros_but_n_ones_right_shifted << start_bit; + const unsigned int all_ones_but_zeros_at_position = ~all_zeros_but_ones_at_position; + + const unsigned int all_zeros_but_new_bits_at_position = new_bits << start_bit; + const unsigned int all_unchanged_but_zeros_at_position = previous_4_bytes & all_ones_but_zeros_at_position; + const unsigned int all_unchanged_but_new_bit_value_at_position = all_unchanged_but_zeros_at_position | all_zeros_but_new_bits_at_position; + + const unsigned int updated_bits_with_reserved_bits_at_zero = all_unchanged_but_new_bit_value_at_position & reserved_bits_mask; - const int all_zeros_but_ones_at_position = all_zeros_but_n_ones_right_shifted << start_bit; - const int all_ones_but_zeros_at_position = ~all_zeros_but_ones_at_position; - - const int all_zeros_but_new_bits_at_position = new_bits << start_bit; - const int all_unchanged_but_zeros_at_position = previous_4_bytes & all_ones_but_zeros_at_position; - const int all_unchanged_but_new_bit_value_at_position = all_unchanged_but_zeros_at_position | all_zeros_but_new_bits_at_position; - return all_unchanged_but_new_bit_value_at_position; + return updated_bits_with_reserved_bits_at_zero; + } + + void blink() + { + DigitalOut led(LED1); + while(1) + { + led = 1; + wait(0.2); + led = 0; + wait(0.2); + } } } \ No newline at end of file