Skip to content

Commit

Permalink
added splitString, reworked printArray
Browse files Browse the repository at this point in the history
  • Loading branch information
aster94 committed Mar 25, 2019
1 parent d1bc241 commit 4900ee7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pioenvs
.vscode
platformio.ini
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Utilities

This is a collection of useful functions for the Arduino Framerwork
This is a collection of useful functions for the Arduino Framerwork, like:
- printArray
- pinModeGroup
- digitalWriteGroup
- digitalToggle
- digitalToggleGroup
- split

Upload the example to see them working
98 changes: 79 additions & 19 deletions Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@

#include <Arduino.h>

#if defined (ARDUINO_ARCH_SAMD)
#if defined(ARDUINO_ARCH_SAMD)
#define UniversalSerial Serial_
#else
#define UniversalSerial HardwareSerial
#endif

// MACROS
#define TO_FAHRENHEIT(x) x * 1.8 + 32 // the formula for the conversion
#define LEN(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) // complex but safe macro for the lenght, won't work with matrix
#define maybe 2
// true or false is not enough
// Toggle the state of a pin
void digitalToggle(uint8_t pin)
{
digitalWrite(pin, !digitalRead(pin));
}
#define TO_FAHRENHEIT(x) x * 1.8 + 32
#define TO_CELSIUS(x) (x - 32) * 0.55
#define LEN(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) // complex but safe macro for the lenght
#define maybe 2 // true or false is not enough

// Change the state of a group of pins
void pinModeGroup(uint8_t pins[], size_t len, uint8_t state)
Expand All @@ -38,31 +33,96 @@ void digitalWriteGroup(uint8_t pins[], size_t len, uint8_t state)
}
}

// Echo between two serial ports
void echo(UniversalSerial *one, UniversalSerial *two)
// Toggle the state of a pin
void digitalToggle(uint8_t pin)
{
digitalWrite(pin, !digitalRead(pin));
}

// Toggle the state of a group of pins
void digitalToggleGroup(uint8_t pins[], size_t len)
{
for (uint8_t i = 0; i < len; i++)
{
digitalWrite(pins[i], !digitalRead(pins[i]));
}
}

// Echo between two serial ports, bi or mono directional
void echo(UniversalSerial *one, UniversalSerial *two, bool mono_directional = false)
{
if (one->available())
{
two->write(one->read());
}
if (two->available())
if (mono_directional == false)
{
one->write(two->read());
if (two->available())
{
one->write(two->read());
}
}
}

// Print an array of any kind
template <class T>
void printArray(T array, size_t len, UniversalSerial *_port = &Serial)
void printArray(T array, size_t len, char delimiter[] = "\n", uint8_t formatter = DEC, bool invert = false, bool index = false, UniversalSerial *_port = &Serial)
{
if (len >= 65535)
{
_port->println("ARRAY TOO BIG");
}

if (invert == false)
{
for (uint16_t i = 0; i < len; i++)
{
if (index)
{
_port->print(i);
_port->print(": ");
}
_port->print(array[i], formatter);
if (i < len - 1)
{
_port->print(delimiter);
}
}
}
else
{
for (uint16_t i = len; i > 0; i--)
{
if (index)
{
_port->print(i - 1);
_port->print(": ");
}
_port->print(array[i - 1], formatter);
if (i > 1)
{
_port->print(delimiter);
}
}
}
_port->println();
}

// Split a cstring into token and get one of them
char *splitString(char str[], uint8_t index, char delimiter[] = " ")
{
for (uint16_t i = 0; i < len; i++)
uint8_t counter = 0;
char *token = strtok(str, delimiter);
while (token != NULL)
{
_port->println(array[i]);
if (i == 65535)
if (counter == index)
{
_port->println("MAX SIZE REACHED");
return token;
}
token = strtok(NULL, delimiter);
counter++;
}
return NULL;
}

#endif // Utilities_h
45 changes: 40 additions & 5 deletions examples/basic/basic.ino
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
#include <Utilities.h>

void setup() {
void setup()
{
Serial.begin(115200);
delay(1000);

// Create a group of pin
uint8_t group[] = {LED_BUILTIN, 12, 11, 10, 9, 8};

// Print that group
printArray(group, LEN(group));

// Set their pinMode
pinModeGroup(group, LEN(group), OUTPUT);

// Write their state
digitalWriteGroup(group, LEN(group), HIGH);
delay(1000);
digitalWriteGroup(group, LEN(group), LOW);

for (uint8_t n = 0; n < 5; n++) {
// Toggle the state of a pin
for (uint8_t n = 0; n < 5; n++)
{
digitalToggle(LED_BUILTIN);
delay(500);
}

printArray(group, LEN(group));
// Or toggle a group of pins
for (uint8_t n = 0; n < 5; n++)
{
digitalToggleGroup(group, LEN(group));
delay(500);
}

// Create a string
char str[] = "hello, this is a test";

// Split it and get the 4th part (starting from 0)
char *substring = splitString(str, 4, " ,");

// Print it
Serial.print("substring: ");
Serial.println(substring); // this will output "test"

// A more complex array for printArray
uint8_t array[] = {1, 0x56, 0b1011};

// Print the array as hexadecimal values with a ":" between the items and invert it (from the last to the first)
printArray(array, LEN(array), ":", HEX, true);

// Print the array as binary values with a newline ("\n") between the items and write the index before every element
printArray(array, LEN(array), "\n", BIN, false, true);

echo(&Serial, &Serial2);
// If you have more then one serial (like Serial2 or SoftwareSerial) you can make an echo between them
//echo(&Serial, &Serial2);
}

void loop() {}
void loop() {}
5 changes: 4 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
#######################################
# Methods and Functions (KEYWORD2)
#######################################
digitalToggle KEYWORD2
pinModeGroup KEYWORD2
digitalWriteGroup KEYWORD2
digitalToggle KEYWORD2
digitalToggleGroup KEYWORD2
echo KEYWORD2
printArray KEYWORD2
splitString KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
TO_FAHRENHEIT LITERAL1
TO_CELSIUS LITERAL1
LEN LITERAL1
maybe LITERAL1
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Utilities
version=0.0.2
version=0.1.0
author=Vincenzo G.
maintainer=Vincenzo G.
sentence=A library that makes using Arduino a breeze.
paragraph=Useful functions for the hobbist
paragraph=Useful functions for the hobbyist, like: printArray, digitalToggle, pinModeGroup, digitalWriteGroup and others!
category=Other
url=https://github.com/aster94/Utilities
architectures=*
architectures=*

0 comments on commit 4900ee7

Please sign in to comment.