BLE switch interface with GROVE joystic for micro:bit http://mahoro-ba.net/e2073.html

Dependencies:   microbit

Revision:
0:28fb3e9ef81a
Child:
3:b6e9850d3e76
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KeyValueInt.cpp	Mon Sep 17 02:48:51 2018 +0000
@@ -0,0 +1,44 @@
+//=================================
+//  Class KeyValueInt
+//=================================
+//    The MIT License (MIT)   Copyright (c) 2018 Masatomo Kojima
+//
+// Keys and values are stored in pairs, maximum and minimum values are set, 
+// and values can be easily increased or decreased.
+// キーと値をペアで記憶し、値の最大値、最小値を設定し、値の増減を容易にします。
+
+#include "KeyValueInt.h"
+
+KeyValueInt::KeyValueInt(const char *key, char disp, int data, int min, int max, bool rotation) {
+    this->key = key;
+    this->disp = (disp==0 ? key[0] : disp);
+    this->value = data;
+    this->min = min;
+    this->max = max;
+    this->rotation = rotation;
+    set(data);
+}
+
+int KeyValueInt::range(int data){
+    if (this->rotation) {
+        if (data < this->min) data = this->max;
+        if (data > this->max) data = this->min;
+    } else {
+        if (data < this->min) data = this->min;
+        if (data > this->max) data = this->max;
+    }
+    return data;
+}
+
+int KeyValueInt::set(int data){
+    this->value = range(data);
+    return this->value;
+}
+
+void KeyValueInt::inc(int delta){
+    set(range(value + delta));
+}
+
+void KeyValueInt::dec(int delta){
+   set(range(value - delta));
+}