Skip to content

Commit 01a3d75

Browse files
committed
Nicla: add overloads for LED*
1 parent 1fda0f0 commit 01a3d75

File tree

6 files changed

+127
-9
lines changed

6 files changed

+127
-9
lines changed

libraries/Nicla_System/Nicla_System.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
RGBled nicla::leds;
1313
BQ25120A nicla::_pmic;
1414
rtos::Mutex nicla::i2c_mutex;
15+
bool nicla::started = false;
1516

1617
void nicla::pingI2CThd() {
1718
while(1) {
@@ -30,6 +31,7 @@ bool nicla::begin()
3031
static rtos::Thread th(osPriorityHigh, 1024, nullptr, "ping_thread");
3132
th.start(&nicla::pingI2CThd);
3233
#endif
34+
started = true;
3335
}
3436

3537
void nicla::enableCD()
@@ -109,3 +111,39 @@ uint8_t nicla::readLDOreg()
109111
disableCD();
110112
return ldo_reg;
111113
}
114+
115+
I2CLed LEDR(red);
116+
I2CLed LEDG(green);
117+
I2CLed LEDB(blue);
118+
I2CLed LED_BUILTIN(white);
119+
120+
void pinMode(I2CLed pin, PinMode mode)
121+
{
122+
if (!nicla::started) {
123+
nicla::begin();
124+
nicla::leds.begin();
125+
nicla::leds.setColor(off);
126+
}
127+
}
128+
129+
void digitalWrite(I2CLed pin, PinStatus value)
130+
{
131+
switch (pin.get()) {
132+
case red:
133+
nicla::leds.setColorRed(value == LOW ? 0 : 0xFF);
134+
break;
135+
case blue:
136+
nicla::leds.setColorBlue(value == LOW ? 0 : 0xFF);
137+
break;
138+
case green:
139+
nicla::leds.setColorGreen(value == LOW ? 0 : 0xFF);
140+
break;
141+
case white:
142+
if (value == LOW) {
143+
nicla::leds.setColor(0, 0, 0);
144+
} else {
145+
nicla::leds.setColor(0xFF, 0xFF, 0xFF);
146+
}
147+
break;
148+
}
149+
}

libraries/Nicla_System/Nicla_System.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class nicla {
2323
friend class BQ25120A;
2424
friend class Arduino_BHY2;
2525

26+
static bool started;
27+
2628
private:
2729
static void pingI2CThd();
2830
static void enableCD();

libraries/Nicla_System/RGBled.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,32 @@ void RGBled::setColor(RGBColors color)
7171
_red = 0x20;
7272
}
7373

74-
setColor(_blue, _green, _red);
74+
setColor(_red, _green, _blue);
7575

7676
}
7777

78-
void RGBled::setColor(uint8_t blue, uint8_t green, uint8_t red)
78+
void RGBled::setColorBlue(uint8_t blue) {
79+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue >> scale_factor);
80+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
81+
}
82+
83+
void RGBled::setColorRed(uint8_t red) {
84+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red >> scale_factor);
85+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
86+
}
87+
88+
void RGBled::setColorGreen(uint8_t green) {
89+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green >> scale_factor);
90+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
91+
}
92+
93+
void RGBled::setColor(uint8_t red, uint8_t green, uint8_t blue)
7994
{
8095
// set rgb led current
81-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue); //maximum current
82-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green);
83-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red);
96+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue >> scale_factor); //maximum current
97+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green >> scale_factor);
98+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red >> scale_factor);
8499
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect
85-
86100
}
87101

88102
// Read the Chip ID register, this is a good test of communication
@@ -141,7 +155,7 @@ void RGBled::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
141155
uint8_t RGBled::readByte(uint8_t address, uint8_t subAddress)
142156
{
143157
nicla::i2c_mutex.lock();
144-
char response = 0xFF;
158+
//char response = 0xFF;
145159
Wire1.beginTransmission(address);
146160
Wire1.write(subAddress);
147161
Wire1.endTransmission(false);

libraries/Nicla_System/RGBled.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ enum RGBColors {
132132
blue = 3,
133133
yellow = 4,
134134
magenta = 5,
135-
cyan = 6
135+
cyan = 6,
136+
white = 7
136137
};
137138

138139
/*
@@ -156,7 +157,30 @@ class RGBled
156157
void begin();
157158
void end();
158159
void setColor(RGBColors color);
159-
void setColor(uint8_t blue, uint8_t green, uint8_t red);
160+
void setColor(uint8_t red, uint8_t green, uint8_t blue);
161+
162+
void setColorBlue(uint8_t blue = 0xFF);
163+
void setColorRed(uint8_t red = 0xFF);
164+
void setColorGreen(uint8_t green = 0xFF);
165+
166+
/* intensity from 1 (lowest) to 8 (full) */
167+
void setIntensity(uint8_t power) {
168+
scale_factor = 8 - power;
169+
if (scale_factor < 0) {
170+
scale_factor = 0;
171+
}
172+
/*
173+
if (power > 8) {
174+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_CURRENT_BAND, 0x15);
175+
}
176+
if (power > 16) {
177+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_CURRENT_BAND, 0x2A);
178+
}
179+
if (power > 32) {
180+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_CURRENT_BAND, 0x3F);
181+
}
182+
*/
183+
}
160184

161185
private:
162186
void init();
@@ -173,6 +197,7 @@ class RGBled
173197
uint8_t _green;
174198
uint8_t _red;
175199

200+
int8_t scale_factor = 4;
176201
};
177202

178203
#endif

libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
#define led2 LEDG
3535
#define led3 LEDB
3636

37+
// On Nicla Sense ME, RGB leds are connected via an I2C module
38+
// The user APIs are the same, but we can't convert to int, so use defines
39+
#elif defined(ARDUINO_NICLA)
40+
41+
#include "Nicla_System.h"
42+
#define led1 LEDR
43+
#define led2 LEDG
44+
#define led3 LEDB
45+
3746
#else
3847

3948
int led1 = LEDR;

variants/NICLA/pins_arduino.h

+30
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,34 @@ static const uint8_t SCK = PIN_SPI_SCK;
139139
uint8_t getUniqueSerialNumber(uint8_t* name);
140140
void _ontouch1200bps_();
141141

142+
#if __has_include("Nicla_System.h")
143+
# define NICLA_SYSTEM_ATTRIBUTE
144+
#else
145+
# define NICLA_SYSTEM_ATTRIBUTE __attribute__ ((error("Please include Nicla_System.h to use this pin")))
146+
#endif
147+
148+
class I2CLed {
149+
public:
150+
I2CLed(int _pin) : pin(_pin) {};
151+
int get() {
152+
return pin;
153+
};
154+
bool operator== (I2CLed const & other) const {
155+
return pin == other.pin;
156+
}
157+
//operator int() = delete;
158+
__attribute__ ((error("Change me to a #define"))) operator int();
159+
private:
160+
int pin;
161+
};
162+
163+
extern I2CLed LEDR;
164+
extern I2CLed LEDG;
165+
extern I2CLed LEDB;
166+
extern I2CLed LED_BUILTIN;
167+
168+
void NICLA_SYSTEM_ATTRIBUTE pinMode (I2CLed pin, PinMode mode);
169+
PinStatus NICLA_SYSTEM_ATTRIBUTE digitalRead (I2CLed pin);
170+
void NICLA_SYSTEM_ATTRIBUTE digitalWrite(I2CLed pin, PinStatus value);
171+
142172
#endif //__PINS_ARDUINO__

0 commit comments

Comments
 (0)