forked from OpenSprinkler/OpenSprinkler-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.h
148 lines (122 loc) · 3.73 KB
/
gpio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* OpenSprinkler Unified (AVR/RPI/BBB/LINUX) Firmware
* Copyright (C) 2015 by Ray Wang ([email protected])
*
* GPIO header file
* Feb 2015 @ OpenSprinkler.com
*
* This file is part of the OpenSprinkler library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef GPIO_H
#define GPIO_H
#if defined(ARDUINO)
#if defined(ESP8266)
#include "Arduino.h"
// PCA9555 register defines
#define NXP_INPUT_REG 0
#define NXP_OUTPUT_REG 2
#define NXP_INVERT_REG 4
#define NXP_CONFIG_REG 6
#define IOEXP_TYPE_8574 0
#define IOEXP_TYPE_8575 1
#define IOEXP_TYPE_9555 2
#define IOEXP_TYPE_UNKNOWN 254
#define IOEXP_TYPE_NONEXIST 255
class IOEXP {
public:
IOEXP(uint8_t addr=255) { address = addr; type = IOEXP_TYPE_NONEXIST; }
virtual void pinMode(uint8_t pin, uint8_t IOMode) { }
virtual uint16_t i2c_read(uint8_t reg) { return 0xFFFF; }
virtual void i2c_write(uint8_t reg, uint16_t v) { }
// software implementation of shift register out
virtual void shift_out(uint8_t plat, uint8_t pclk, uint8_t pdat, uint8_t v) { }
void digitalWrite(uint16_t v) {
i2c_write(NXP_OUTPUT_REG, v);
}
uint16_t digitalRead() {
return i2c_read(NXP_INPUT_REG);
}
uint8_t digitalRead(uint8_t pin) {
return (digitalRead() & (1<<pin)) ? HIGH : LOW;
}
void digitalWrite(uint8_t pin, uint8_t v) {
uint16_t values = i2c_read(NXP_OUTPUT_REG);
if(v > 0) values |= (1<<pin);
else values &= ~(1 << pin);
i2c_write(NXP_OUTPUT_REG, values);
}
static byte detectType(uint8_t address);
uint8_t address;
uint8_t type;
};
class PCA9555 : public IOEXP {
public:
PCA9555(uint8_t addr) { address = addr; type = IOEXP_TYPE_9555; }
void pinMode(uint8_t pin, uint8_t IOMode);
uint16_t i2c_read(uint8_t reg);
void i2c_write(uint8_t reg, uint16_t v);
void shift_out(uint8_t plat, uint8_t pclk, uint8_t pdat, uint8_t v);
};
class PCF8575 : public IOEXP {
public:
PCF8575(uint8_t addr) { address = addr; type = IOEXP_TYPE_8575; }
void pinMode(uint8_t pin, uint8_t IOMode) {
if(IOMode!=OUTPUT) inputmask |= (1<<pin);
}
uint16_t i2c_read(uint8_t reg);
void i2c_write(uint8_t reg, uint16_t v);
private:
uint16_t inputmask = 0;
};
class PCF8574 : public IOEXP {
public:
PCF8574(uint8_t addr) { address = addr; type = IOEXP_TYPE_8574; }
void pinMode(uint8_t pin, uint8_t IOMode) {
if(IOMode!=OUTPUT) inputmask |= (1<<pin);
}
uint16_t i2c_read(uint8_t reg);
void i2c_write(uint8_t reg, uint16_t v);
private:
uint8_t inputmask = 0; // mask bits for input pins
};
void pinModeExt(byte pin, byte mode);
void digitalWriteExt(byte pin, byte value);
byte digitalReadExt(byte pin);
#endif // ESP8266
#else
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include "defines.h"
#define OUTPUT 0
#define INPUT 1
#if defined(OSPI)
#define INPUT_PULLUP 2
#else
#define INPUT_PULLUP INPUT
#endif
#define HIGH 1
#define LOW 0
void pinMode(int pin, byte mode);
void digitalWrite(int pin, byte value);
int gpio_fd_open(int pin, int mode = O_WRONLY);
void gpio_fd_close(int fd);
void gpio_write(int fd, byte value);
byte digitalRead(int pin);
// mode can be any of 'rising', 'falling', 'both'
void attachInterrupt(int pin, const char* mode, void (*isr)(void));
#endif
#endif // GPIO_H