summaryrefslogtreecommitdiff
path: root/include/arduino-nano/driver
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2017-12-18 11:16:24 +0100
committerDaniel Friesel <derf@finalrewind.org>2017-12-18 11:16:24 +0100
commitef3ef99299fe3c4ea4cc9ab1c6526da2eb89e1a5 (patch)
treeb221a89dc892674c131459eb4d6cf957a0466c9e /include/arduino-nano/driver
parent771b62b056cfebecf8fb5ed4e1938ffc12d167f8 (diff)
arduino-nano: Add GPIO input/output/read/write support
Diffstat (limited to 'include/arduino-nano/driver')
-rw-r--r--include/arduino-nano/driver/gpio.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/include/arduino-nano/driver/gpio.h b/include/arduino-nano/driver/gpio.h
index 177e7c5..17bdfc8 100644
--- a/include/arduino-nano/driver/gpio.h
+++ b/include/arduino-nano/driver/gpio.h
@@ -1,16 +1,101 @@
#ifndef GPIO_H
#define GPIO_H
+#include <avr/io.h>
+
class GPIO {
private:
GPIO(const GPIO &copy);
public:
GPIO () {}
+
+ enum Pin : unsigned char {
+ pb0 = 8,
+ pb1 = 9,
+ pb2 = 10,
+ pb3 = 11,
+ pb4 = 12,
+ pb5 = 13,
+ pb6 = 14,
+ pb7 = 15,
+ pc0 = 16,
+ pc1 = 17,
+ pc2 = 18,
+ pc3 = 19,
+ pc4 = 20,
+ pc5 = 21,
+ pc6 = 22,
+ pd0 = 24,
+ pd1 = 25,
+ pd2 = 26,
+ pd3 = 27,
+ pd4 = 28,
+ pd5 = 29,
+ pd6 = 30,
+ pd7 = 31
+ };
+
void setup();
void led_on(unsigned char id);
void led_off(unsigned char id);
void led_toggle(unsigned char id);
+ inline void input(unsigned char const pin) {
+ if (pin < 8) {
+ } else if (pin < 16) {
+ DDRB &= ~_BV(pin - 8);
+ } else if (pin < 24) {
+ DDRC &= ~_BV(pin - 16);
+ } else if (pin < 32) {
+ DDRD &= ~_BV(pin - 24);
+ }
+ }
+ inline void output(unsigned char const pin) {
+ if (pin < 8) {
+ } else if (pin < 16) {
+ DDRB |= _BV(pin - 8);
+ } else if (pin < 24) {
+ DDRC |= _BV(pin - 16);
+ } else if (pin < 32) {
+ DDRD |= _BV(pin - 24);
+ }
+ }
+ inline unsigned char read(unsigned char const pin) {
+ if (pin < 8) {
+ }
+ if (pin < 16) {
+ return (PINB & _BV(pin - 8));
+ }
+ if (pin < 24) {
+ return (PINC & _BV(pin - 16));
+ }
+ if (pin < 32) {
+ return (PIND & _BV(pin - 24));
+ }
+ return 0;
+ }
+ inline void write(unsigned char const pin, unsigned char value) {
+ if (pin < 8) {
+ } else if (pin < 16) {
+ if (value) {
+ PORTB |= _BV(pin - 8);
+ } else {
+ PORTB &= ~_BV(pin - 8);
+ }
+ } else if (pin < 24) {
+ if (value) {
+ PORTB |= _BV(pin - 16);
+ } else {
+ PORTB &= ~_BV(pin - 16);
+ }
+ } else if (pin < 32) {
+ if (value) {
+ PORTB |= _BV(pin - 24);
+ } else {
+ PORTB &= ~_BV(pin - 24);
+ }
+ }
+ }
};
extern GPIO gpio;