Skip to content

Commit 14283a3

Browse files
Added IO Expander beta library
1 parent c40ec0c commit 14283a3

File tree

15 files changed

+1239
-0
lines changed

15 files changed

+1239
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/arduino.json
2+
/c_cpp_properties.json
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Arduino.h"
2+
#include "TinyPICOExpander.h"
3+
4+
TinyPICOExpander tpio = TinyPICOExpander();
5+
6+
// Connect an LED from the ALERT pin on the TinyPICO Expander Shield to GND.
7+
// This LED will assert (active LOW / turn off) when the comparator condition is met (1.5v / reading of 500)
8+
9+
void setup()
10+
{
11+
// Serial.begin(460800);
12+
Serial.begin(115200);
13+
Serial.println("Asserting comparator interrupt on ALERT (Active LOW) when voltage on channel 0 exceeds ~1.5v (a reading of 500)");
14+
15+
tpio.begin();
16+
tpio.analogSetGain(GAIN_TWOTHIRDS);
17+
tpio.startComparator(0, 500); // ~1.5v
18+
}
19+
20+
void loop()
21+
{
22+
Serial.printf("Comparator Reading: %06u\r\n",
23+
tpio.getLastConversionResults());
24+
delay(100);
25+
}

TinyPICO-IOExpander/examples/Complete/Complete.ino

Lines changed: 394 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "Arduino.h"
2+
#include "TinyPICOExpander.h"
3+
4+
TinyPICOExpander tpio = TinyPICOExpander();
5+
6+
void setup()
7+
{
8+
// Serial.begin(460800);
9+
Serial.begin(115200);
10+
Serial.println("Getting differential readings from Channel 0 (A0/Positive & A1/Negative) and Channel 1 (A2/Positive & A3/Negative)");
11+
12+
tpio.begin();
13+
tpio.analogSetGain(GAIN_TWOTHIRDS);
14+
}
15+
16+
void loop()
17+
{
18+
Serial.printf("A0: %06u | A1: %06u\r\n",
19+
tpio.analogReadDifferential(0),
20+
tpio.analogReadDifferential(1));
21+
delay(100);
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "Arduino.h"
2+
#include "TinyPICOExpander.h"
3+
4+
// Note: These samples do not debounce inputs
5+
6+
TinyPICOExpander tpio = TinyPICOExpander();
7+
8+
void GPIOChangeCallback(uint16_t ports, uint8_t button, bool state) {
9+
Serial.printf("Button: %02d | State: %s\r\n", button, state ? "TRUE " : "FALSE");
10+
}
11+
12+
void setup()
13+
{
14+
// Serial.begin(460800);
15+
Serial.begin(115200);
16+
17+
tpio.begin();
18+
19+
// configure 4 buttons
20+
tpio.pinMode(4, INPUT);
21+
tpio.pinMode(5, INPUT);
22+
tpio.pinMode(6, INPUT);
23+
tpio.pinMode(7, INPUT);
24+
25+
tpio.pullUp(4, HIGH);
26+
tpio.pullUp(5, HIGH);
27+
tpio.pullUp(6, HIGH);
28+
tpio.pullUp(7, HIGH);
29+
30+
// Register callback
31+
tpio.RegisterChangeCB(GPIOChangeCallback);
32+
}
33+
34+
void loop()
35+
{
36+
tpio.update();
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Arduino.h"
2+
#include "TinyPICOExpander.h"
3+
4+
TinyPICOExpander tpio = TinyPICOExpander();
5+
6+
void setup()
7+
{
8+
// Serial.begin(460800);
9+
Serial.begin(115200);
10+
Serial.println("Getting single ended readings from Channel 0-4");
11+
12+
tpio.begin();
13+
tpio.analogSetGain(GAIN_TWOTHIRDS);
14+
}
15+
16+
void loop()
17+
{
18+
Serial.printf("A0: %06u | A1: %06u | A2: %06u | A3: %06u\r\n",
19+
tpio.analogReadSingleEnded(0),
20+
tpio.analogReadSingleEnded(1),
21+
tpio.analogReadSingleEnded(2),
22+
tpio.analogReadSingleEnded(3));
23+
24+
delay(100);
25+
}

TinyPICO-IOExpander/keywords.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
###################################
2+
# Syntax Coloring Map For TinyPicoExpanderShield
3+
###################################
4+
5+
###################################
6+
# Datatypes (KEYWORD1)
7+
###################################
8+
9+
TinyPicoExpanderShield KEYWORD1
10+
11+
###################################
12+
# Methods and Functions (KEYWORD2)
13+
###################################
14+
15+
16+
###################################
17+
# Constants (LITERAL1)
18+
###################################
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=TinyPICO Expander Shield Helper Library
2+
version=1.0.0
3+
author=Unexpected Maker
4+
maintainer=Adam Keher
5+
sentence=A expander Shield Helper Library
6+
paragraph=A helper library for the expander shield made especially for the tinyPICO development board.
7+
category=Uncategorized
8+
url=https://github.com/UnexpectedMaker/??
9+
architectures=*

TinyPICO-IOExpander/readme.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TinyPICO IO Expander Library
2+
============================
3+
4+
This is a library for the TinyPICO IO Expander that groups all of it's functionality into a single wrapper.
5+
6+
The library comes with a range of examples that cover all aspects of the shields functionality.
7+
8+
Special thanks to Adam Keher for helping put this together!
9+
10+
For more information about the IO Expander Shield, visit:
11+
https://unexpectedmaker.com/shop/tinypico-ioexpander
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#include "ADS1015.h"
2+
3+
bool UM_ADS1015::write(uint8_t addr, uint16_t value)
4+
{
5+
Wire.beginTransmission(m_i2cAddress);
6+
Wire.write(addr);
7+
Wire.write((uint8_t)(value >> 8));
8+
Wire.write((uint8_t)(value & 0xFF));
9+
if (Wire.endTransmission() == 0)
10+
return true;
11+
return false;
12+
}
13+
14+
uint16_t UM_ADS1015::read(unsigned int addr)
15+
{
16+
unsigned int value;
17+
Wire.beginTransmission(m_i2cAddress);
18+
Wire.write(addr);
19+
if (Wire.endTransmission(false) != 0)
20+
return 0;
21+
if (Wire.requestFrom(m_i2cAddress, (uint8_t)2) < 2)
22+
return 0;
23+
return ((Wire.read() << 8) | Wire.read());
24+
}
25+
26+
void UM_ADS1015::begin(uint8_t addr)
27+
{
28+
m_i2cAddress = addr;
29+
m_conversionDelay = ADS1015_CONVERSIONDELAY;
30+
m_bitShift = 4;
31+
m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
32+
33+
Wire.begin();
34+
}
35+
36+
void UM_ADS1015::analogSetGain(adsGain_t gain)
37+
{
38+
m_gain = gain;
39+
}
40+
41+
adsGain_t UM_ADS1015::analogGetGain()
42+
{
43+
return m_gain;
44+
}
45+
46+
uint16_t UM_ADS1015::analogReadSingleEnded(uint8_t channel)
47+
{
48+
if (channel > 3)
49+
{
50+
return 0;
51+
}
52+
53+
// Start with default values
54+
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
55+
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
56+
ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
57+
ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
58+
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
59+
ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
60+
61+
// Set PGA/voltage range
62+
config |= m_gain;
63+
64+
// Set single-ended input channel
65+
switch (channel)
66+
{
67+
case (0):
68+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
69+
break;
70+
case (1):
71+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
72+
break;
73+
case (2):
74+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
75+
break;
76+
case (3):
77+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
78+
break;
79+
}
80+
81+
// Set 'start single-conversion' bit
82+
config |= ADS1015_REG_CONFIG_OS_SINGLE;
83+
84+
// Write config register to the ADC
85+
write(ADS1015_REG_POINTER_CONFIG, config);
86+
87+
// Wait for the conversion to complete
88+
delay(m_conversionDelay);
89+
90+
// Read the conversion results
91+
// Shift 12-bit results right 4 bits for the ADS1015
92+
return read(ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
93+
}
94+
95+
int16_t UM_ADS1015::analogReadDifferential(uint8_t channel)
96+
{
97+
// Start with default values
98+
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
99+
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
100+
ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
101+
ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
102+
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
103+
ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
104+
105+
// Set PGA/voltage range
106+
config |= m_gain;
107+
108+
// Set channels
109+
if (channel == 0)
110+
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
111+
if (channel == 1)
112+
config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
113+
114+
// Set 'start single-conversion' bit
115+
config |= ADS1015_REG_CONFIG_OS_SINGLE;
116+
117+
// Write config register to the ADC
118+
write(ADS1015_REG_POINTER_CONFIG, config);
119+
120+
// Wait for the conversion to complete
121+
delay(m_conversionDelay);
122+
123+
// Read the conversion results
124+
uint16_t res = read(ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
125+
if (m_bitShift == 0)
126+
{
127+
return (int16_t)res;
128+
}
129+
else
130+
{
131+
// Shift 12-bit results right 4 bits for the ADS1015,
132+
// making sure we keep the sign bit intact
133+
if (res > 0x07FF)
134+
{
135+
// negative number - extend the sign to 16th bit
136+
res |= 0xF000;
137+
}
138+
return (int16_t)res;
139+
}
140+
}
141+
142+
void UM_ADS1015::startComparator(uint8_t channel, int16_t threshold)
143+
{
144+
// Start with default values
145+
uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
146+
ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
147+
ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
148+
ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
149+
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
150+
ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
151+
ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
152+
153+
// Set PGA/voltage range
154+
config |= m_gain;
155+
156+
// Set single-ended input channel
157+
switch (channel)
158+
{
159+
case (0):
160+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
161+
break;
162+
case (1):
163+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
164+
break;
165+
case (2):
166+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
167+
break;
168+
case (3):
169+
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
170+
break;
171+
}
172+
173+
// Set the high threshold register
174+
// Shift 12-bit results left 4 bits for the ADS1015
175+
write(ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift);
176+
177+
// Write config register to the ADC
178+
write(ADS1015_REG_POINTER_CONFIG, config);
179+
}
180+
181+
int16_t UM_ADS1015::getLastConversionResults()
182+
{
183+
// Wait for the conversion to complete
184+
delay(m_conversionDelay);
185+
186+
// Read the conversion results
187+
uint16_t res = read(ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
188+
if (m_bitShift == 0)
189+
{
190+
return (int16_t)res;
191+
}
192+
else
193+
{
194+
// Shift 12-bit results right 4 bits for the ADS1015,
195+
// making sure we keep the sign bit intact
196+
if (res > 0x07FF)
197+
{
198+
// negative number - extend the sign to 16th bit
199+
res |= 0xF000;
200+
}
201+
return (int16_t)res;
202+
}
203+
}

0 commit comments

Comments
 (0)