#include "include.h"
#include <Arduino.h>
//#include <stdarg.h>
#define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
#define MYMACRO(T,...) literal<T>(__VA_ARGS__);
#define FOO(...) "" #__VA_ARGS__
/*
typedef enum {
OFF,
ON,
} state;
*/
typedef enum : byte {OFF, ON} state;
//#define setHigh(...)
#define setLow(myArg, ...) \
do { int _local[] = __VA_ARGS__; \
_setLow((sizeof _local)/sizeof(int), _local, myArg); } \
while (0)
#define setHigh(...) \
do { int _local[] = __VA_ARGS__; \
_setHigh((sizeof _local)/sizeof(int), _local); } \
while (0)
void _setLow(int n, int args[], byte myArg) {
Serial1.printf("myArg: %d \n", myArg);
Serial1.printf("n: %d \n", n);
for (byte i = 0; i < n; i++) {
Serial1.printf("args[i]: %d \n", args[i]);
digitalWrite(args[i], 1);
}
Serial1.println();
}
void _setHigh(int n, int args[]) {
for (byte i = 0; i < n; i++) {
Serial1.println(args[i]);
digitalWrite(args[i], 0);
}
Serial1.println();
//for (byte i = 0; i < n; i++) digitalWrite(args[i], state);
}
int myArray[] = {2,3,4,5,6}; // Let the computer count how many elements you need
/* ---------------------------------------------------------------------------------------- */
const char *debugString = "_"; // string literal (read-only) with pointer
const char str[] = "this is a string"; // string literal (read-only) w/o pointer
char okay[] {"255.255.255.255"}; // writeable char byte array
char charArray[] = {'a', 'b', 'c', 'd'}; // array of chars with no pointer
char *ptrToArray; // initialize pointer for charArray
char myString[10] = "HELLO"; // string as writeable char array with fixed length
char some[10] = "ABCD";
char FOO[16];
/* ---------------------------------------------------------------------------------------- */
enum {
TRIGGER = 13,
BTN = 14,
SOLENOID = 16,
RUMBLE = 17,
BULBS = 18,
CLICK = 19
} pinNames;
struct OUTS {
const char *label;
uint8_t pin;
uint16_t thresholds[2] = {};
uint8_t state;
bool done;
}
outputs[] = {
{"SOLENOID", SOLENOID, {0,5000}, 0, false},
{"RUMBLE", RUMBLE, {1000,2000}, 0, false},
{"BULBS", BULBS, {2000,5000}, 0, false},
{"CLICK", CLICK, {3000,10000}, 0, false},
};
int i;
uint8_t out_pins = (sizeof(outputs)/sizeof(outputs[0]));
uint8_t done_count;
unsigned long thisMillis;
uint16_t elapsed;
/* ---------------------------------------------------------------------------------------- */
int arr1[] = {5,6,7,8,9};
template<typename T, int sz>
void getSize(T(&)[sz]) {
Serial1.printf("getSize: %d\n",sz);
}
void arrayArg(const int (&arr)[4]) {
Serial1.println("arrayArg() :");
for (byte i = 0; i < ELEMENTS(arr); i++) {
if (arr[i] != 0) {
Serial1.print(arr[i]); // Array indexing
Serial1.print(" ");
}
}
Serial1.println();
}
///////////////////////////////////////////////////////////////////////
void setup() {
for (i=0; i<out_pins; i++) {
pinMode(outputs[i].pin, OUTPUT);
digitalWrite(outputs[i].pin, HIGH);
delay(250);
digitalWrite(outputs[i].pin, LOW);
}
Serial1.begin(115200);
Serial1.println("Hello user!");
Serial1.println();
delay(1000);
arrayArg({1,2,3,4});
arrayArg({1,2});
setHigh({SOLENOID, RUMBLE, BULBS, CLICK});
delay(1000);
setLow(ON, {SOLENOID, RUMBLE, BULBS, CLICK});
//setHigh({SOLENOID, RUMBLE, BULBS, CLICK});
//arr1[] = {5,6,7,8,9};
size_t arr_size = sizeof(arr1)/sizeof(arr1[0]);
//Serial1.println(FOO(1,2,3));
//Serial1.printf("arr_size: %d\n",(sizeof arr / sizeof arr[0]));
//Serial1.printf("arr_size: %zu, %zu\n",sizeof arr, sizeof arr[0]);
//testTimers();
//Strings();
}
///////////////////////////////////////////////////////////////////////
//MYMACRO( long[2], ({1, 2}) );
template<typename T, int sz>
int arraySize(T(&)[sz]) {
Serial1.printf("array size: %d\n",sz);
return sz;
}
void parseArray() {
arraySize(arr1);
//Serial1.printf("mySize: %d\n",arraySize({1,2,3,4}));
}
void readArray(const int (&arr)[4]) {
//Serial1.println(__VA_ARGS__);
int i;
Serial1.println("readArray() :");
for (i = 0; i < ELEMENTS(arr); i++) {
Serial1.print(arr[i]); // Array indexing
Serial1.print(" ");
}
Serial1.println();
//getSize(arr);
//int mySize = getSize({1,2,3,4});
//Serial1.printf("mySize: %d\n",getSize({1,2,3,4}));
}
/*
void func5(size_t size, const int (*str_array)[size] ) {
Serial1.printf("sizeof str_array[0]: %zu\n", sizeof str_array, sizeof str_array[0]);
}
*/
void testTimers() {
uint8_t x, //, done_count = 0,
thisPin, thisState, thisBool;
uint16_t thisLo, thisHi;
thisMillis = millis();
done_count = 0;
while (done_count < 4) {
//Serial1.printf("done_count: %d \n\n", done_count);
for (x = 0; x < out_pins; x++) {
thisPin = outputs[x].pin;
thisState = outputs[x].state;
thisLo = outputs[x].thresholds[0];
thisHi = outputs[x].thresholds[1];
thisBool = outputs[x].done;
if (!outputs[x].done) { //if (!thisBool) {
elapsed = (millis()-thisMillis);
if (!outputs[x].state) {
thisLo = outputs[x].thresholds[0];
if (elapsed >= thisLo) { //if (elapsed >= thisLo && !thisState) {
digitalWrite(thisPin, HIGH);
outputs[x].state = 1;
Serial1.printf("outputs[%d]: %s HIGH \n", x, outputs[x].label);
Serial1.printf("elapsed: %d \n\n", elapsed);
}
} else {
thisHi = outputs[x].thresholds[1];
if (elapsed >= thisHi) { //if (elapsed >= thisLo && !thisState) {
digitalWrite(thisPin, LOW);
outputs[x].state = 0;
outputs[x].done = true;
done_count++;
Serial1.printf("outputs[%d]: %s LOW \n", x, outputs[x].label);
Serial1.printf("elapsed: %d \n\n", elapsed);
}
}
}
}
delay(10);
}
Serial1.println("DONE");
for (i=0; i<out_pins; i++) digitalWrite(outputs[i].pin, LOW);
}
void Strings() {
okay[0] = 65; // update first char in okay[] to 'A'
Serial1.println(myString);
/* ---------------------------------------------------------------------------------------- */
ptrToArray = charArray; // = &charArray[0] // point to array
Serial1.println((char*)ptrToArray); // cast to char array and print whole array
Serial1.println((char*)charArray); // cast original array and print whole array
/* ---------------------------------------------------------------------------------------- */
Serial1.println("\nConcat:");
char txt[25]; // initialize char array
txt[0] = '\0'; // terminating null-character on first position
strcat(txt, "Hello"), Serial1.println(txt);
strcat(txt, " World!"), Serial1.println(txt);
Serial1.println("\nClear:");
txt[0] = '\0'; // clear char array with null-char
strcat(txt, "Hello");
strcat(txt, " World!");
Serial1.println(txt);
txt[0] = '\0';
strcat(txt, "Hello");
strcpy(txt, ""); // other way of resetting string (with strcpy)
strcat(txt, " World");
Serial1.println(txt);
txt[0] = '\0';
/* ---------------------------------------------------------------------------------------- */
strcpy(FOO, txt); // copy char array to another char array
Serial.println(FOO);
char result[5];
}
void loop() {}