-
Notifications
You must be signed in to change notification settings - Fork 12
/
PCF8591.h
135 lines (109 loc) · 3.87 KB
/
PCF8591.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
/** \mainpage PCF8591 library
* PCF8591 Analog Port Expand
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef PCF8591_h
#define PCF8591_h
#include "Wire.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Uncomment to enable printing out nice debug messages.
// #define PCF8591_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef PCF8591_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
#include <math.h>
#define AIN0 0b00000000
#define AIN1 0b00000001
#define AIN2 0b00000010
#define AIN3 0b00000011
#define CHANNEL0 0b00000000
#define CHANNEL1 0b00000001
#define CHANNEL2 0b00000010
#define CHANNEL3 0b00000011
#define AUTOINCREMENT_READ 0b00000100
#define SINGLE_ENDED_INPUT 0b00000000
#define TREE_DIFFERENTIAL_INPUT 0b00010000
#define TWO_SINGLE_ONE_DIFFERENTIAL_INPUT 0b00100000
#define TWO_DIFFERENTIAL_INPUT 0b00110000
#define ENABLE_OUTPUT 0b01000000
#define DISABLE_OUTPUT 0b01000000
#define OUTPUT_MASK 0b01000000
class PCF8591 {
public:
struct AnalogInput {
uint8_t ain0;
uint8_t ain1;
uint8_t ain2;
uint8_t ain3;
} analogInput;
PCF8591(uint8_t address);
#if !defined(__AVR) && !defined(ARDUINO_ARCH_SAMD) && !defined(TEENSYDUINO) && !defined(ARDUINO_ARCH_RENESAS)
PCF8591(uint8_t address, int sda, int scl);
#endif
#if defined(ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS)
///// changes for second i2c bus
PCF8591(TwoWire *pWire, uint8_t address);
#endif
#if defined(ESP32)
PCF8591(TwoWire *pWire, uint8_t address, int sda, int scl);
#endif
// PCF8591(uint8_t address);
//
//#if !defined(__AVR) && !defined(__STM32F1__)
// PCF8591(uint8_t address, uint8_t sda, uint8_t scl);
//
// #ifdef ESP32
// PCF8591(TwoWire *pWire, uint8_t address);
// PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
// #endif
//
//#endif
void begin();
void begin(uint8_t address);
struct AnalogInput analogReadAll(byte readType = SINGLE_ENDED_INPUT);
uint8_t analogRead(uint8_t channel, byte readType = SINGLE_ENDED_INPUT);
void analogWrite(uint8_t value);
void voltageWrite(float value, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
float voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
private:
TwoWire *_wire;
uint8_t _address;
uint8_t _sda = SDA;
uint8_t _scl = SCL;
byte _outputStatus = DISABLE_OUTPUT;
long readVcc(void);
};
#endif