]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Add arduino-alike GPIO pin control helpers
authorWez Furlong <wez@fb.com>
Mon, 28 Nov 2016 06:43:11 +0000 (22:43 -0800)
committerWez Furlong <wez@fb.com>
Mon, 28 Nov 2016 06:58:34 +0000 (22:58 -0800)
Unlike the arduino functions, these don't take abstract pin numbers,
they take pin labels like `B0`.  Also, rather than taking very
generic parameter names, these take slightly more descriptive
enum values.

These improve the clarity of code that would otherwise be inscrutable
bit manipulation in tersely named port register names.

quantum/pincontrol.h [new file with mode: 0644]

diff --git a/quantum/pincontrol.h b/quantum/pincontrol.h
new file mode 100644 (file)
index 0000000..36ce29e
--- /dev/null
@@ -0,0 +1,37 @@
+#pragma once
+// Some helpers for controlling gpio pins
+#include <avr/io.h>
+
+enum {
+  PinDirectionInput = 0,
+  PinDirectionOutput = 1,
+  PinLevelHigh = 1,
+  PinLevelLow = 0,
+};
+
+// ex: pinMode(B0, PinDirectionOutput);
+static inline void pinMode(uint8_t pin, int mode) {
+  uint8_t bv = _BV(pin & 0xf);
+  if (mode == PinDirectionOutput) {
+    _SFR_IO8((pin >> 4) + 1) |= bv;
+  } else {
+    _SFR_IO8((pin >> 4) + 1) &= ~bv;
+    _SFR_IO8((pin >> 4) + 2) &= ~bv;
+  }
+}
+
+// ex: digitalWrite(B0, PinLevelHigh);
+static inline void digitalWrite(uint8_t pin, int mode) {
+  uint8_t bv = _BV(pin & 0xf);
+  if (mode == PinLevelHigh) {
+    _SFR_IO8((pin >> 4) + 2) |= bv;
+  } else {
+    _SFR_IO8((pin >> 4) + 2) &= ~bv;
+  }
+}
+
+// Return true if the pin is HIGH
+// digitalRead(B0)
+static inline bool digitalRead(uint8_t pin) {
+  return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
+}