Skip to content

Commit cfac4e5

Browse files
committed
LED section added
1 parent 89119fc commit cfac4e5

File tree

2 files changed

+154
-15
lines changed

2 files changed

+154
-15
lines changed
503 KB
Loading

content/hardware/07.opta/opta-family/opta/tutorials/01.user-manual/content.md

Lines changed: 154 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ void optaAnalogTask() {
37333733
static long int start = millis();
37343734
37353735
/* using this the code inside the if will run every PERIODIC_UPDATE_TIME ms
3736-
assuming the function is called repeteadly in the loop() function */
3736+
assuming the function is called repeatedly in the loop() function */
37373737
37383738
if (millis() - start > PERIODIC_UPDATE_TIME) {
37393739
start = millis();
@@ -3922,7 +3922,7 @@ void optaAnalogTask() {
39223922
static long int start = millis();
39233923
39243924
/* using this the code inside the if will run every PERIODIC_UPDATE_TIME ms
3925-
assuming the function is called repeteadly in the loop() function */
3925+
assuming the function is called repeatedly in the loop() function */
39263926
39273927
if (millis() - start > PERIODIC_UPDATE_TIME) {
39283928
start = millis();
@@ -4030,19 +4030,6 @@ exp.setPwm(ch, <period_us>, <pulse_us>); // the first argument is to define the
40304030
The following example will let you set a **PWM** signal on channel **P1**, increasing and decreasing its duty-cycle sequentially, this sketch is based on the built-in example found in **File > Examples > Arduino_Opta_Blueprint > Analog > PWM**:
40314031

40324032
```arduino
4033-
/* -------------------------------------------------------------------------- */
4034-
/* FILE NAME: Pwm.ino
4035-
AUTHOR: Daniele Aimo
4036-
4037-
DATE: 20231205
4038-
DESCRIPTION: This sketch shows basic PWM usage with the OptaBlue library.
4039-
LICENSE: Copyright (c) 2024 Arduino SA
4040-
This Source Code Form is subject to the terms fo the Mozilla
4041-
Public License (MPL), v 2.0. You can obtain a copy of the MPL
4042-
at http://mozilla.org/MPL/2.0/.
4043-
NOTES: */
4044-
/* -------------------------------------------------------------------------- */
4045-
40464033
#include "OptaBlue.h"
40474034
40484035
@@ -4211,7 +4198,159 @@ The Opta™ Analog Expansions have **8 status LEDs** on the front panel. They ar
42114198
| LED 7 | 6 or OA_LED_7 |
42124199
| LED 8 | 7 or OA_LED_8 |
42134200

4201+
To control a status LED use the built-in function `switchLedOn()` or `switchLedOff()` as shown below:
4202+
4203+
```arduino
4204+
// Turn ON
4205+
exp.switchLedOn(<LED>, <update>); // define the LED to control and set to true or false the update parameter
4206+
exp.switchLedOff(<LED>, <update>); // define the LED to control and set to true or false the update parameter
4207+
```
4208+
If the `update` parameter is set to "false", then you will be setting the desired status of the LED but it won't be applied until you call `updateLeds()` function.
4209+
4210+
```arduino
4211+
exp.switchLedOn(OA_LED_1, false); // set the desired status to the queue
4212+
exp.updateLeds(); // apply the changes and update the current LED state
4213+
```
4214+
4215+
The following example will let you control the status LEDs sequentially, this sketch can be found in **File > Examples > Arduino_Opta_Blueprint > Analog > LED**:
4216+
4217+
```arduino
4218+
#include "OptaBlue.h"
4219+
4220+
#define PERIODIC_UPDATE_TIME 2 //actually not used (it's DELAY_LED that leads the timing)
4221+
#define DELAY_AFTER_SETUP 1000
4222+
#define DELAY_LED 250
4223+
4224+
/* -------------------------------------------------------------------------- */
4225+
void printExpansionType(ExpansionType_t t) {
4226+
/* -------------------------------------------------------------------------- */
4227+
if (t == EXPANSION_NOT_VALID) {
4228+
Serial.print("Unknown!");
4229+
} else if (t == EXPANSION_OPTA_DIGITAL_MEC) {
4230+
Serial.print("Opta --- DIGITAL [Mechanical] ---");
4231+
} else if (t == EXPANSION_OPTA_DIGITAL_STS) {
4232+
Serial.print("Opta --- DIGITAL [Solid State] ---");
4233+
} else if (t == EXPANSION_DIGITAL_INVALID) {
4234+
Serial.print("Opta --- DIGITAL [!!Invalid!!] ---");
4235+
} else if (t == EXPANSION_OPTA_ANALOG) {
4236+
Serial.print("~~~ Opta ANALOG ~~~");
4237+
} else {
4238+
Serial.print("Unknown!");
4239+
}
4240+
}
4241+
4242+
/* -------------------------------------------------------------------------- */
4243+
void printExpansionInfo() {
4244+
/* -------------------------------------------------------------------------- */
4245+
static long int start = millis();
4246+
4247+
if (millis() - start > 5000) {
4248+
start = millis();
4249+
Serial.print("Number of expansions: ");
4250+
Serial.println(OptaController.getExpansionNum());
4251+
4252+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
4253+
Serial.print("Expansion n. ");
4254+
Serial.print(i);
4255+
Serial.print(" type ");
4256+
printExpansionType(OptaController.getExpansionType(i));
4257+
Serial.print(" I2C address ");
4258+
Serial.println(OptaController.getExpansionI2Caddress(i));
4259+
}
4260+
}
4261+
}
4262+
4263+
int8_t oa_index = -1;
4264+
/* -------------------------------------------------------------------------- */
4265+
/* SETUP */
4266+
/* -------------------------------------------------------------------------- */
4267+
void setup() {
4268+
/* -------------------------------------------------------------------------- */
4269+
Serial.begin(115200);
4270+
delay(2000);
4271+
Serial.println("*** Opta Analog LED example ***");
4272+
4273+
OptaController.begin();
4274+
}
4275+
4276+
4277+
4278+
/* -------------------------------------------------------------------------- */
4279+
void optaAnalogTask() {
4280+
/* -------------------------------------------------------------------------- */
4281+
static bool st = true;
4282+
static long int start = millis();
4283+
4284+
static const char *msg_on = "ON";
4285+
static const char *msg_off = "OFF";
4286+
static char *msg_ptr = (char *)msg_on;
4287+
4288+
if (millis() - start > PERIODIC_UPDATE_TIME) {
4289+
start = millis();
4290+
4291+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
4292+
4293+
AnalogExpansion exp = OptaController.getExpansion(i);
4294+
if (exp) {
4295+
4296+
/* exp is true only if exp is an actual
4297+
* AnalogExpansion and false otherwise */
4298+
4299+
for (int j = 0; j < 8; j++) {
4300+
if (st) {
4301+
msg_ptr = (char *)msg_on;
4302+
exp.switchLedOn((uint8_t)j, false);
4303+
} else {
4304+
msg_ptr = (char *)msg_off;
4305+
exp.switchLedOff((uint8_t)j, false);
4306+
}
4307+
exp.updateLeds();
4308+
delay(250);
4309+
Serial.print("switching LED ");
4310+
Serial.print(j);
4311+
Serial.print(" ");
4312+
Serial.println(msg_ptr);
4313+
}
4314+
4315+
st = !st;
4316+
}
4317+
}
4318+
}
4319+
}
4320+
4321+
/* -------------------------------------------------------------------------- */
4322+
/* LOOP */
4323+
/* -------------------------------------------------------------------------- */
4324+
void loop() {
4325+
/* -------------------------------------------------------------------------- */
4326+
OptaController.update();
4327+
//printExpansionInfo();
4328+
optaAnalogTask();
4329+
}
4330+
```
4331+
4332+
Please take into account that `OptaController.update()` must be called cyclically to support the hot plug of new expansions. In other words, by calling the update() function cyclically, the controller will discover new expansions when they are plugged in while the controller is already running.
4333+
4334+
Thanks to this function, the action of plugging in a new expansion will cause the controller to start a completely new discovery process and a new I2C address assignment.
4335+
4336+
`OptaController.update()` function DOES NOT:
4337+
* Check if an expansion has been removed and remove their objects
4338+
* Update any data from or to the expansion
4339+
4340+
The expansion object in the example above is defined using the `OptaController.getExpansion(i);` function, as follows:
4341+
4342+
```arduino
4343+
for(int i = 0; i < OptaController.getExpansionNum(); i++) { // check all the available expansion slots
4344+
AnalogExpansion exp = OptaController.getExpansion(i);
4345+
}
4346+
```
4347+
The above method will check if there is an Ext A0602 expansion connected in the `i` index from the five admitted. This will ensure which expansion the read state belongs to.
4348+
4349+
The function `optaAnalogTask()` turns on sequentially the **LEDs** and turns them off again.
4350+
4351+
After the Opta™ controller is programmed with the example sketch, you can see the onboard LEDs blinking with a pattern and experience the following behavior:
42144352

4353+
![Status LED Example Animation](assets/led-ani.gif)
42154354

42164355
## Support
42174356

0 commit comments

Comments
 (0)