From 4900ee74b30fcb6e5a4292cbc4df6cd66def8b40 Mon Sep 17 00:00:00 2001 From: aster94 Date: Mon, 25 Mar 2019 11:51:01 +0100 Subject: [PATCH] added splitString, reworked printArray --- .gitignore | 3 ++ README.md | 10 +++- Utilities.h | 98 ++++++++++++++++++++++++++++++++-------- examples/basic/basic.ino | 45 ++++++++++++++++-- keywords.txt | 5 +- library.properties | 6 +-- 6 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ad8bb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.pioenvs +.vscode +platformio.ini \ No newline at end of file diff --git a/README.md b/README.md index c92c5d7..27036da 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # Utilities -This is a collection of useful functions for the Arduino Framerwork \ No newline at end of file +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 \ No newline at end of file diff --git a/Utilities.h b/Utilities.h index 66cf4fc..09942ad 100644 --- a/Utilities.h +++ b/Utilities.h @@ -3,22 +3,17 @@ #include -#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) @@ -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 -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 \ No newline at end of file diff --git a/examples/basic/basic.ino b/examples/basic/basic.ino index b970c02..20ee915 100644 --- a/examples/basic/basic.ino +++ b/examples/basic/basic.ino @@ -1,24 +1,59 @@ #include -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() {} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index 845ab52..9671277 100644 --- a/keywords.txt +++ b/keywords.txt @@ -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 \ No newline at end of file diff --git a/library.properties b/library.properties index 46d7c0f..d6a4713 100644 --- a/library.properties +++ b/library.properties @@ -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=* \ No newline at end of file