![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Template project for University of York ELE00032C Lab 4
main.cpp@2:00b981e6c241, 2021-12-17 (annotated)
- Committer:
- ajp109
- Date:
- Fri Dec 17 15:18:35 2021 +0000
- Revision:
- 2:00b981e6c241
- Parent:
- 1:09ea183d3f39
Pull updates for 2022
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajp109 | 0:77209603a6fe | 1 | #include "mbed.h" |
ajp109 | 0:77209603a6fe | 2 | |
ajp109 | 1:09ea183d3f39 | 3 | class BoundedInt { |
ajp109 | 1:09ea183d3f39 | 4 | |
ajp109 | 1:09ea183d3f39 | 5 | int min_; |
ajp109 | 1:09ea183d3f39 | 6 | int max_; |
ajp109 | 1:09ea183d3f39 | 7 | int value_; |
ajp109 | 1:09ea183d3f39 | 8 | |
ajp109 | 1:09ea183d3f39 | 9 | public: |
ajp109 | 1:09ea183d3f39 | 10 | |
ajp109 | 1:09ea183d3f39 | 11 | /* Constructor: takes a minimum and maximum value, and an initial value. |
ajp109 | 1:09ea183d3f39 | 12 | Enforces min <= max and calls setValue() to enforce min <= value <= max. */ |
ajp109 | 1:09ea183d3f39 | 13 | BoundedInt(int min, int max, int value) { |
ajp109 | 1:09ea183d3f39 | 14 | if (max < min) { |
ajp109 | 1:09ea183d3f39 | 15 | max = min; |
ajp109 | 1:09ea183d3f39 | 16 | } |
ajp109 | 1:09ea183d3f39 | 17 | min_ = min; |
ajp109 | 1:09ea183d3f39 | 18 | max_ = max; |
ajp109 | 1:09ea183d3f39 | 19 | setValue(value); |
ajp109 | 1:09ea183d3f39 | 20 | } |
ajp109 | 1:09ea183d3f39 | 21 | |
ajp109 | 1:09ea183d3f39 | 22 | /* Sets a new value for this BoundedInt. If the supplied value is outside |
ajp109 | 1:09ea183d3f39 | 23 | the range [min, max] it will be set to the closest valid value instead. */ |
ajp109 | 1:09ea183d3f39 | 24 | void setValue(int value) { |
ajp109 | 1:09ea183d3f39 | 25 | if (value < min_) { |
ajp109 | 1:09ea183d3f39 | 26 | value = min_; |
ajp109 | 1:09ea183d3f39 | 27 | } |
ajp109 | 1:09ea183d3f39 | 28 | if (value > max_) { |
ajp109 | 1:09ea183d3f39 | 29 | value = max_; |
ajp109 | 1:09ea183d3f39 | 30 | } |
ajp109 | 1:09ea183d3f39 | 31 | value_ = value; |
ajp109 | 1:09ea183d3f39 | 32 | } |
ajp109 | 1:09ea183d3f39 | 33 | |
ajp109 | 1:09ea183d3f39 | 34 | /* Returns the value of this BoundedInt as an int */ |
ajp109 | 1:09ea183d3f39 | 35 | int getValue() { |
ajp109 | 1:09ea183d3f39 | 36 | return value_; |
ajp109 | 1:09ea183d3f39 | 37 | } |
ajp109 | 1:09ea183d3f39 | 38 | |
ajp109 | 1:09ea183d3f39 | 39 | /* Shorthand for .setValue() */ |
ajp109 | 1:09ea183d3f39 | 40 | BoundedInt & operator=(int value) { |
ajp109 | 1:09ea183d3f39 | 41 | setValue(value); |
ajp109 | 1:09ea183d3f39 | 42 | return *this; |
ajp109 | 1:09ea183d3f39 | 43 | } |
ajp109 | 1:09ea183d3f39 | 44 | |
ajp109 | 1:09ea183d3f39 | 45 | }; |
ajp109 | 1:09ea183d3f39 | 46 | |
ajp109 | 1:09ea183d3f39 | 47 | int main() { |
ajp109 | 1:09ea183d3f39 | 48 | printf("ok\r\n"); |
ajp109 | 1:09ea183d3f39 | 49 | |
ajp109 | 1:09ea183d3f39 | 50 | BoundedInt test(0, 10, 0); // Minimum, maximum, initial value |
ajp109 | 1:09ea183d3f39 | 51 | |
ajp109 | 1:09ea183d3f39 | 52 | printf("Value is %d\r\n", test.getValue()); |
ajp109 | 1:09ea183d3f39 | 53 | test = 4; |
ajp109 | 1:09ea183d3f39 | 54 | printf("Value is %d\r\n", test.getValue()); |
ajp109 | 1:09ea183d3f39 | 55 | test = 25; |
ajp109 | 1:09ea183d3f39 | 56 | printf("Value is %d\r\n", test.getValue()); |
ajp109 | 1:09ea183d3f39 | 57 | |
ajp109 | 1:09ea183d3f39 | 58 | // Do nothing, forever... |
ajp109 | 1:09ea183d3f39 | 59 | while (true); |
ajp109 | 0:77209603a6fe | 60 | } |