![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
df
Fork of APP1 by
Utility.cpp@13:bb9669053eb3, 2017-01-16 (annotated)
- Committer:
- GaiSensei
- Date:
- Mon Jan 16 00:06:45 2017 +0000
- Revision:
- 13:bb9669053eb3
- Parent:
- 12:1c341b119b23
- Child:
- 15:b38d9d210e32
Create homemade_mbed functions; ; - Read bits; - Write bits; ; Sorry messy commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 6:3facf0329142 | 1 | #include "Utility.hpp" |
dupm2216 | 6:3facf0329142 | 2 | |
dupm2216 | 6:3facf0329142 | 3 | #include <cmath> |
dupm2216 | 6:3facf0329142 | 4 | |
dupm2216 | 6:3facf0329142 | 5 | namespace utility |
dupm2216 | 6:3facf0329142 | 6 | { |
dupm2216 | 6:3facf0329142 | 7 | bool is_almost_equal(double a, double b, double tolerance) |
dupm2216 | 6:3facf0329142 | 8 | { |
dupm2216 | 6:3facf0329142 | 9 | double difference = std::abs(a-b); |
dupm2216 | 6:3facf0329142 | 10 | return (difference <= tolerance); |
dupm2216 | 6:3facf0329142 | 11 | } |
dupm2216 | 6:3facf0329142 | 12 | |
dupm2216 | 6:3facf0329142 | 13 | //Return angle between 0 and 360 degree |
dupm2216 | 6:3facf0329142 | 14 | double wrap_angle(double angle) |
dupm2216 | 6:3facf0329142 | 15 | { |
dupm2216 | 6:3facf0329142 | 16 | return angle - 360 * std::floor( angle / 360 ); |
dupm2216 | 6:3facf0329142 | 17 | } |
dupm2216 | 6:3facf0329142 | 18 | |
dupm2216 | 6:3facf0329142 | 19 | double degree_from_radian(const double angle_radian) |
dupm2216 | 6:3facf0329142 | 20 | { |
dupm2216 | 6:3facf0329142 | 21 | const double angle_degree = angle_radian * 180.0 / PI; |
dupm2216 | 6:3facf0329142 | 22 | return wrap_angle(angle_degree); |
dupm2216 | 6:3facf0329142 | 23 | } |
GaiSensei | 12:1c341b119b23 | 24 | |
GaiSensei | 13:bb9669053eb3 | 25 | unsigned int update_bit(const unsigned int previous_4_bytes, const int position, const bool new_bit_value) |
GaiSensei | 13:bb9669053eb3 | 26 | { |
GaiSensei | 13:bb9669053eb3 | 27 | return update_bits(previous_4_bytes, position, position, new_bit_value); |
GaiSensei | 13:bb9669053eb3 | 28 | } |
GaiSensei | 13:bb9669053eb3 | 29 | |
GaiSensei | 13:bb9669053eb3 | 30 | unsigned int update_bits(const unsigned int previous_4_bytes, const int start_bit, const int stop_bit, const unsigned int new_bits) |
GaiSensei | 12:1c341b119b23 | 31 | { |
GaiSensei | 13:bb9669053eb3 | 32 | const int all_ones_but_n_zeros_right_shifted = (0xFFFFFFFF << (stop_bit - start_bit + 1)); |
GaiSensei | 13:bb9669053eb3 | 33 | const int all_zeros_but_n_ones_right_shifted = ~all_ones_but_n_zeros_right_shifted; |
GaiSensei | 13:bb9669053eb3 | 34 | |
GaiSensei | 13:bb9669053eb3 | 35 | const int all_zeros_but_ones_at_position = all_zeros_but_n_ones_right_shifted << start_bit; |
GaiSensei | 13:bb9669053eb3 | 36 | const int all_ones_but_zeros_at_position = ~all_zeros_but_ones_at_position; |
GaiSensei | 13:bb9669053eb3 | 37 | |
GaiSensei | 13:bb9669053eb3 | 38 | const int all_zeros_but_new_bits_at_position = new_bits << start_bit; |
GaiSensei | 13:bb9669053eb3 | 39 | const int all_unchanged_but_zeros_at_position = previous_4_bytes & all_ones_but_zeros_at_position; |
GaiSensei | 13:bb9669053eb3 | 40 | const int all_unchanged_but_new_bit_value_at_position = all_unchanged_but_zeros_at_position | all_zeros_but_new_bits_at_position; |
GaiSensei | 12:1c341b119b23 | 41 | return all_unchanged_but_new_bit_value_at_position; |
GaiSensei | 12:1c341b119b23 | 42 | } |
dupm2216 | 9:12519f9dd3cd | 43 | } |