Initial release
KeyValueInt.cpp@8:be1ae533ab8c, 2018-12-14 (annotated)
- Committer:
- masakjm
- Date:
- Fri Dec 14 18:40:09 2018 +0000
- Revision:
- 8:be1ae533ab8c
- Parent:
- 3:b6e9850d3e76
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masakjm | 0:28fb3e9ef81a | 1 | //================================= |
masakjm | 0:28fb3e9ef81a | 2 | // Class KeyValueInt |
masakjm | 0:28fb3e9ef81a | 3 | //================================= |
masakjm | 0:28fb3e9ef81a | 4 | // The MIT License (MIT) Copyright (c) 2018 Masatomo Kojima |
masakjm | 0:28fb3e9ef81a | 5 | // |
masakjm | 0:28fb3e9ef81a | 6 | // Keys and values are stored in pairs, maximum and minimum values are set, |
masakjm | 0:28fb3e9ef81a | 7 | // and values can be easily increased or decreased. |
masakjm | 3:b6e9850d3e76 | 8 | // キーと値をペアで記憶する。値の最大値、最小値を設定し、値の増減を容易にする。 |
masakjm | 0:28fb3e9ef81a | 9 | |
masakjm | 0:28fb3e9ef81a | 10 | #include "KeyValueInt.h" |
masakjm | 0:28fb3e9ef81a | 11 | |
masakjm | 3:b6e9850d3e76 | 12 | /** ---------- |
masakjm | 3:b6e9850d3e76 | 13 | * @brief キーと値をペアで記憶する。値の最大値、最小値を設定し、値の増減を容易にする。 |
masakjm | 3:b6e9850d3e76 | 14 | * @param *key キー |
masakjm | 3:b6e9850d3e76 | 15 | * @param disp LED表示の文字 |
masakjm | 3:b6e9850d3e76 | 16 | * @param data データ |
masakjm | 3:b6e9850d3e76 | 17 | * @param min 最小値 |
masakjm | 3:b6e9850d3e76 | 18 | * @param max 最大値 |
masakjm | 3:b6e9850d3e76 | 19 | * @param rotation true:循環する false:循環しない |
masakjm | 3:b6e9850d3e76 | 20 | */ |
masakjm | 0:28fb3e9ef81a | 21 | KeyValueInt::KeyValueInt(const char *key, char disp, int data, int min, int max, bool rotation) { |
masakjm | 0:28fb3e9ef81a | 22 | this->key = key; |
masakjm | 0:28fb3e9ef81a | 23 | this->disp = (disp==0 ? key[0] : disp); |
masakjm | 0:28fb3e9ef81a | 24 | this->value = data; |
masakjm | 0:28fb3e9ef81a | 25 | this->min = min; |
masakjm | 0:28fb3e9ef81a | 26 | this->max = max; |
masakjm | 0:28fb3e9ef81a | 27 | this->rotation = rotation; |
masakjm | 0:28fb3e9ef81a | 28 | set(data); |
masakjm | 0:28fb3e9ef81a | 29 | } |
masakjm | 0:28fb3e9ef81a | 30 | |
masakjm | 3:b6e9850d3e76 | 31 | /** ---------- |
masakjm | 3:b6e9850d3e76 | 32 | * @brief 最大値と最小値を考慮して、正しい範囲の値を返す |
masakjm | 3:b6e9850d3e76 | 33 | * @param data データ |
masakjm | 3:b6e9850d3e76 | 34 | */ |
masakjm | 3:b6e9850d3e76 | 35 | int KeyValueInt::range(int data){ |
masakjm | 0:28fb3e9ef81a | 36 | if (this->rotation) { |
masakjm | 0:28fb3e9ef81a | 37 | if (data < this->min) data = this->max; |
masakjm | 0:28fb3e9ef81a | 38 | if (data > this->max) data = this->min; |
masakjm | 0:28fb3e9ef81a | 39 | } else { |
masakjm | 0:28fb3e9ef81a | 40 | if (data < this->min) data = this->min; |
masakjm | 0:28fb3e9ef81a | 41 | if (data > this->max) data = this->max; |
masakjm | 0:28fb3e9ef81a | 42 | } |
masakjm | 0:28fb3e9ef81a | 43 | return data; |
masakjm | 0:28fb3e9ef81a | 44 | } |
masakjm | 0:28fb3e9ef81a | 45 | |
masakjm | 3:b6e9850d3e76 | 46 | /** ---------- |
masakjm | 3:b6e9850d3e76 | 47 | * @brief 値を更新する |
masakjm | 3:b6e9850d3e76 | 48 | * @param data データ |
masakjm | 3:b6e9850d3e76 | 49 | */ |
masakjm | 0:28fb3e9ef81a | 50 | int KeyValueInt::set(int data){ |
masakjm | 0:28fb3e9ef81a | 51 | this->value = range(data); |
masakjm | 0:28fb3e9ef81a | 52 | return this->value; |
masakjm | 0:28fb3e9ef81a | 53 | } |
masakjm | 0:28fb3e9ef81a | 54 | |
masakjm | 3:b6e9850d3e76 | 55 | /** ---------- |
masakjm | 3:b6e9850d3e76 | 56 | * @brief 値を増やす |
masakjm | 3:b6e9850d3e76 | 57 | * @param delta 変化量(1) |
masakjm | 3:b6e9850d3e76 | 58 | */ |
masakjm | 0:28fb3e9ef81a | 59 | void KeyValueInt::inc(int delta){ |
masakjm | 0:28fb3e9ef81a | 60 | set(range(value + delta)); |
masakjm | 0:28fb3e9ef81a | 61 | } |
masakjm | 0:28fb3e9ef81a | 62 | |
masakjm | 3:b6e9850d3e76 | 63 | /** ---------- |
masakjm | 3:b6e9850d3e76 | 64 | * @brief 値を減らす |
masakjm | 3:b6e9850d3e76 | 65 | * @param delta 変化量(1) |
masakjm | 3:b6e9850d3e76 | 66 | */ |
masakjm | 0:28fb3e9ef81a | 67 | void KeyValueInt::dec(int delta){ |
masakjm | 0:28fb3e9ef81a | 68 | set(range(value - delta)); |
masakjm | 0:28fb3e9ef81a | 69 | } |