Skip to content

Commit a7e203d

Browse files
committed
Example updates
1 parent 0cb9e46 commit a7e203d

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ You can create matrix keyboards with any arrangement of keys, but the two most c
260260

261261
Alongside the example, there is comprehensive documentation describing the use of [matrix keyboards on Arduino](https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/matrix-keyboard-keypad-manager/).
262262

263+
## Inbuilt Unit testing framework and mocking
264+
265+
IoAbstraction has a very simple unit test framework built into it, it works on a very wide range of boards, more or less everything in our supported list. In addition to this there are mock versions of some components to make testing your code easier. See https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/ioabstraction-troubleshooting-unit-testing/
266+
263267
## ESP32 extras mode
264268

265269
On ESP32 we are slowly adding support for direct IDF, as it's been requested by one of our clients. This will slowly appear over several releases. To enable this mode you can set the flag `IOA_USE_ESP32_EXTRAS`. Once you do this IDF functions are used for all digital IO functions. We always use IDF functions for analog input and DAC output, and are slowly moving toward direct LTDC functions for PWM instead of wrappers.

examples/analogExample/analogExample.ino

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public:
3030
}
3131

3232
void exec() override {
33-
Serial.print("Trigger AnalogInEvent Threshold ");
34-
Serial.print(analogThreshold);
35-
Serial.print(", value ");
36-
Serial.println(lastReading);
33+
serdebugF3("Trigger AnalogInEvent Threshold, Last: ", analogThreshold, lastReading);
3734
}
3835
};
3936

@@ -48,41 +45,31 @@ void setup() {
4845
Serial.begin(115200);
4946

5047
// set up the device pin directions upfront.
51-
analog.initPin(ANALOG_IN_PIN, DIR_IN);
52-
analog.initPin(PWM_OR_DAC_PIN, DIR_OUT);
48+
internalAnalogDevice().initPin(ANALOG_IN_PIN, DIR_IN);
49+
internalAnalogDevice().initPin(PWM_OR_DAC_PIN, DIR_OUT);
5350

5451
// this is how to register an event with task manager
55-
taskManager.registerEvent(new MyAnalogExceedsEvent(internalAnalogIo(), ANALOG_IN_PIN), true);
52+
taskManager.registerEvent(new MyAnalogExceedsEvent(internalAnalogDevice(), ANALOG_IN_PIN), true);
5653

5754
// we schedule a task to run every 500 millis that reads the value from A1 and prints it output
5855
// along with the largest possible value
5956
taskManager.scheduleFixedRate(500, [] {
60-
Serial.print("Analog input value is ");
61-
Serial.print(analog.getCurrentValue(ANALOG_IN_PIN));
62-
Serial.print("/");
63-
Serial.print(analog.getMaximumRange(DIR_IN, ANALOG_IN_PIN));
64-
Serial.print(" - ");
65-
Serial.print(analog.getCurrentFloat(ANALOG_IN_PIN) * 100.0F);
66-
Serial.println('%');
57+
serdebugF4("Analog input value is ", internalAnalogDevice().getCurrentValue(ANALOG_IN_PIN),
58+
internalAnalogDevice().getMaximumRange(DIR_IN, ANALOG_IN_PIN),
59+
internalAnalogDevice().getCurrentFloat(ANALOG_IN_PIN) * 100.0F);
6760

6861
#ifdef ESP32
69-
auto* espAnalog = reinterpret_cast<ESP32AnalogDevice*>(analog);
7062
// On ESP32 boards, where the analogWrite function doesn't exist we use the underlying functions
7163
// to access either the DAC or LEDC subsystem, if you want to get hold of the ledc channel you can.
72-
EspAnalogOutputMode* outputMode = espAnalog->getEspOutputMode(PWM_OR_DAC_PIN);
64+
EspAnalogOutputMode* outputMode = internalAnalogDevice().getEspOutputMode(PWM_OR_DAC_PIN);
7365
if(outputMode != nullptr) {
74-
Serial.print("ESP32 Output type: ");
75-
Serial.print(outputMode->isDac());
76-
Serial.print(", ledc (pwm channel): ");
77-
Serial.println(outputMode->getPwmChannel());
66+
serdebugF2("ESP32 Output type: ", outputMode->isDac());
67+
serdebugF2("LEDC (pwm channel): ", outputMode->getPwmChannel());
7868
}
7969

80-
EspAnalogInputMode* inputMode = espAnalog->getEspInputMode(ANALOG_IN_PIN);
70+
EspAnalogInputMode* inputMode = internalAnalogDevice().getEspInputMode(ANALOG_IN_PIN);
8171
if(inputMode != nullptr) {
82-
Serial.print("ESP32 Input on dac1: ");
83-
Serial.print(inputMode->isOnDAC1());
84-
Serial.print(", channel: ");
85-
Serial.println(inputMode->getChannel());
72+
serdebugF3("ESP32 Input (ondac1, channel): ", inputMode->isOnDAC1(), inputMode->getChannel());
8673
}
8774
#endif
8875
});
@@ -94,7 +81,7 @@ void setup() {
9481
if(ledCycleValue >= 0.98) ledCycleAdj = -0.01;
9582
if(ledCycleValue <= 0.02) ledCycleAdj = 0.01;
9683

97-
analog.setCurrentFloat(PWM_OR_DAC_PIN, ledCycleValue);
84+
internalAnalogDevice().setCurrentFloat(PWM_OR_DAC_PIN, ledCycleValue);
9885
}, TIME_MILLIS);
9986
}
10087

examples/joystickRotaryEncoder/joystickRotaryEncoder.ino

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,41 @@
1010
#include <IoAbstraction.h>
1111
#include <JoystickSwitchInput.h>
1212

13-
#define ANALOG_INPUT_PIN 25
14-
#define ANALOG_LEFT_RIGHT_PIN 37
13+
#define ANALOG_INPUT_PIN 34
14+
#define ANALOG_LEFT_RIGHT_PIN 33
1515
#define BUTTON_PIN 2
1616
#define MULTI_IO_ARDUINO_PIN_MAX 100
1717
// we need to create an analog device that the joystick encoder will use to get readings.
1818
// In this case on arduino analog pins.
19-
AnalogDevice* analogDevice;
2019

2120
// We now want to receive button input from both Arduino pins, and two extra simulated button pins that
2221
// are acutally when the joystick moves left and right. So we need a multiIoAbstraction.
23-
MultiIoAbstractionRef multiIo = multiIoExpander(MULTI_IO_ARDUINO_PIN_MAX);
22+
MultiIoAbstraction multiIo(MULTI_IO_ARDUINO_PIN_MAX);
2423

2524
void onEncoderChange(int newValue) {
26-
Serial.print("New joystick value: ");
27-
Serial.print(newValue);
28-
Serial.print(", analog in ");
29-
Serial.print(analogDevice->getCurrentValue(ANALOG_INPUT_PIN));
30-
Serial.print(", switch ");
31-
Serial.println(analogDevice->getCurrentValue(ANALOG_LEFT_RIGHT_PIN));
25+
serdebugF4("New joystick val, in, lr: ", newValue, internalAnalogDevice().getCurrentValue(ANALOG_INPUT_PIN), internalAnalogDevice().getCurrentValue(ANALOG_LEFT_RIGHT_PIN));
3226

3327
}
3428

3529
void setup() {
3630
Serial.begin(115200);
3731

38-
analogDevice = internalAnalogIo();
39-
4032
// MKR boards require the line below to wait for the serial port, uncomment if needed
4133
// However, it doesn't work on some other boards and locks them up.
4234
//while(!Serial);
4335

44-
Serial.println("Starting joystick rotary encoder example");
36+
serdebugF("Starting joystick rotary encoder example");
4537

4638
// now we add an expander that handles the left and right function of the joystick as two buttons, these could
4739
// be useful for next and back functions for example. The mid point for calibration will be half max value in
4840
// our case, if your potentiometer differs, set the calibration accordingly. Joystick pins will start at 100.
49-
multiIoAddExpander(multiIo, joystickTwoButtonExpander(analogDevice, ANALOG_LEFT_RIGHT_PIN, .5F), 10);
41+
multiIo.addIoExpander(joystickTwoButtonExpander(internalAnalogIo(), ANALOG_LEFT_RIGHT_PIN, .5F), 10);
5042

5143
// first initialise switches using the multi io expander and pull up switch logic.
52-
switches.initialise(multiIo, true);
44+
switches.initialise(asIoRef(multiIo), true);
5345

5446
// now register the joystick
55-
setupAnalogJoystickEncoder(analogDevice, ANALOG_INPUT_PIN, onEncoderChange);
47+
setupAnalogJoystickEncoder(internalAnalogIo(), ANALOG_INPUT_PIN, onEncoderChange);
5648

5749
// once you've registed the joystick above with switches, you can then alter the mid point and tolerance if needed
5850
// here we set the midpoint to 65% and the tolerance (or point at which we start reading) at +-5%.

0 commit comments

Comments
 (0)