GY-63_MS5611/libraries/PinOutGroup/examples/testPinOutGroup/testPinOutGroup.ino

398 lines
7.5 KiB
Arduino
Raw Normal View History

2017-08-20 16:11:38 -04:00
// FILE: testPinOutGroup.ino
2021-11-13 09:19:09 -05:00
// AUTHOR: Rob Tillaart
2017-08-20 16:11:38 -04:00
// PURPOSE: demo PinOutGroup library for Arduino
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
#include "PinOutGroup.h"
2021-01-29 06:31:58 -05:00
2020-05-20 03:53:04 -04:00
uint8_t ar[4] = {2, 3, 4, 13};
2017-08-20 16:11:38 -04:00
2021-11-13 09:19:09 -05:00
2017-08-20 16:11:38 -04:00
void setup()
{
Serial.begin(115200);
2020-05-20 03:53:04 -04:00
Serial.println(__FILE__);
Serial.print(F("PINOUTGROUP_LIB_VERSION: "));
2017-08-20 16:11:38 -04:00
Serial.println(PINOUTGROUP_LIB_VERSION);
test0();
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
2020-05-20 03:53:04 -04:00
Serial.println(F("done..."));
2017-08-20 16:11:38 -04:00
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void loop()
{
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test0()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Time reference functionality.\n"));
2017-08-20 16:11:38 -04:00
int i = 3;
// CHANGE 1 PINS
pinMode(2, OUTPUT);
uint32_t t1 = micros();
digitalWrite(2, i++ % 2);
digitalWrite(2, i++ % 2);
digitalWrite(2, i++ % 2);
digitalWrite(2, i++ % 2);
uint32_t t2 = micros();
2020-05-20 03:53:04 -04:00
Serial.print(F("Time: "));
2017-08-20 16:11:38 -04:00
Serial.println(t2 - t1);
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test1()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Time to change # pins.\n"));
2017-08-20 16:11:38 -04:00
uint8_t n1, n2, n3, n4;
PinOutGroup POG;
POG.add(2);
POG.add(4);
POG.add(5);
POG.add(13);
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
2017-08-20 16:11:38 -04:00
Serial.println(POG.size());
2020-05-20 03:53:04 -04:00
Serial.println("\n#PINS\tTIME");
2017-08-20 16:11:38 -04:00
// CHANGE 1 PINS
uint32_t t1 = micros();
2020-05-20 03:53:04 -04:00
n1 = POG.write(B1000);
2017-08-20 16:11:38 -04:00
uint32_t t2 = micros();
// CHANGE 2 PINS
uint32_t t3 = micros();
2020-05-20 03:53:04 -04:00
n2 = POG.write(B0100);
2017-08-20 16:11:38 -04:00
uint32_t t4 = micros();
// CHANGE 3 PINS
uint32_t t5 = micros();
2020-05-20 03:53:04 -04:00
n3 = POG.write(B1010);
2017-08-20 16:11:38 -04:00
uint32_t t6 = micros();
// CHANGE 4 PINS
uint32_t t7 = micros();
2020-05-20 03:53:04 -04:00
n4 = POG.write(B0101);
2017-08-20 16:11:38 -04:00
uint32_t t8 = micros();
// RESET ALL
2020-05-20 03:53:04 -04:00
POG.write(B0000);
2017-08-20 16:11:38 -04:00
Serial.print(n1);
Serial.print('\t');
Serial.println(t2 - t1);
Serial.print(n2);
Serial.print('\t');
Serial.println(t4 - t3);
Serial.print(n3);
Serial.print('\t');
Serial.println(t6 - t5);
Serial.print(n4);
Serial.print('\t');
Serial.println(t8 - t7);
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test2()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Does write() match subsequent read()."));
Serial.println(F("No output is good!\n"));
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
POG.add(2);
POG.add(4);
POG.add(5);
POG.add(13);
POG.add(2);
POG.add(4);
POG.add(5);
POG.add(13);
for (int i = 0; i < 256; i++)
{
2020-05-20 03:53:04 -04:00
POG.write(i);
int x = POG.read();
if (x != i)
{
Serial.print("failed value: ");
Serial.println(i);
}
2017-08-20 16:11:38 -04:00
}
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test3()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Test adding an array of pins."));
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
POG.add(4, ar );
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
2017-08-20 16:11:38 -04:00
Serial.println(POG.size());
2020-05-20 03:53:04 -04:00
POG.add(4, ar );
Serial.print(F("size: "));
Serial.println(POG.size());
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test4()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Test adding a pin multiple times.\n"));
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
Serial.println(POG.size());
2017-08-20 16:11:38 -04:00
for (int i = 0; i < 8; i++)
{
POG.add(13);
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
Serial.println(POG.size());
2017-08-20 16:11:38 -04:00
}
2020-05-20 03:53:04 -04:00
POG.write(B01010101);
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test5()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Simulates a 8 bit channel to LCD display.\n"));
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
POG.add(4, ar);
POG.add(4, ar);
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
2017-08-20 16:11:38 -04:00
Serial.println(POG.size());
int cnt = 0;
char str[] = "Hello world. This is a test. The quick brown fox jumps over the lazy dog!";
2020-05-20 03:53:04 -04:00
int len = strlen(str);
for (uint8_t i = 0; i < len; i++)
2017-08-20 16:11:38 -04:00
{
2020-05-20 03:53:04 -04:00
cnt += POG.write((uint8_t)str[i]);
2017-08-20 16:11:38 -04:00
}
2020-05-20 03:53:04 -04:00
Serial.print(F(" Printing: "));
2017-08-20 16:11:38 -04:00
Serial.println(str);
2020-05-20 03:53:04 -04:00
Serial.print(F("Bits total: "));
Serial.println(strlen(str) * 8);
Serial.print(F(" Changed: "));
Serial.println(cnt);
Serial.print(F(" ratio: "));
Serial.println(1.0 * cnt / (len * 8), 3);
Serial.println();
Serial.println(F("footnote: test 5 uses ascii that only change in 5-6 bits max, which decreases the average"));
Serial.println(F(" That said, it is a real life application e.g. updating an LCD screen."));
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2020-05-20 03:53:04 -04:00
// wrapper for test6()
2017-08-20 16:11:38 -04:00
void dw(const uint8_t pin, const uint8_t val)
{
static uint8_t preVal = -1;
if (preVal != val)
{
preVal = val;
digitalWrite(pin, val);
}
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test6()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Check if cache trick works for digitalWrite.\n"));
2017-08-20 16:11:38 -04:00
uint8_t a[1000];
randomSeed(1);
for (int i = 0; i < 1000; i++) a[i] = random(2);
pinMode(2, OUTPUT);
uint32_t t1 = micros();
for (int i = 0; i < 1000; i++)
{
digitalWrite(2, a[i]);
}
uint32_t t2 = micros();
uint32_t t3 = micros();
for (int i = 0; i < 1000; i++)
{
dw(2, a[i]);
}
uint32_t t4 = micros();
2020-05-20 03:53:04 -04:00
Serial.println(F("random: average case"));
Serial.print(F("ORG: "));
2017-08-20 16:11:38 -04:00
Serial.println(t2 - t1);
2020-05-20 03:53:04 -04:00
Serial.print(F("NEW: "));
2017-08-20 16:11:38 -04:00
Serial.println(t4 - t3);
2020-05-20 03:53:04 -04:00
Serial.print(F(" %: "));
Serial.println(1.0 * (t4 - t3) / (t2 - t1));
Serial.println();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
t1 = micros();
2017-08-20 16:11:38 -04:00
for (int i = 0; i < 1000; i++)
{
digitalWrite(2, LOW);
}
2020-05-20 03:53:04 -04:00
t2 = micros();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
t3 = micros();
2017-08-20 16:11:38 -04:00
for (int i = 0; i < 1000; i++)
{
dw(2, LOW);
}
2020-05-20 03:53:04 -04:00
t4 = micros();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
Serial.println(F("same: best case"));
Serial.print(F("ORG: "));
Serial.println(t2 - t1);
Serial.print(F("NEW: "));
Serial.println(t4 - t3);
Serial.print(F(" %: "));
Serial.println(1.0 * (t4 - t3) / (t2 - t1));
Serial.println();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
t1 = micros();
2017-08-20 16:11:38 -04:00
for (int i = 0; i < 1000; i++)
{
digitalWrite(2, i % 2);
}
2020-05-20 03:53:04 -04:00
t2 = micros();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
t3 = micros();
2017-08-20 16:11:38 -04:00
for (int i = 0; i < 1000; i++)
{
dw(2, i % 2);
}
2020-05-20 03:53:04 -04:00
t4 = micros();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
Serial.println(F("toggle: worst case"));
Serial.print(F("ORG: "));
Serial.println(t2 - t1);
Serial.print(F("NEW: "));
Serial.println(t4 - t3);
Serial.print(F(" %: "));
Serial.println(1.0 * (t4 - t3) / (t2 - t1));
Serial.println();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test7()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
Serial.println(F("Test average gain indication (simplified).\n"));
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
// effectively 16 pins...
POG.add(4, ar);
POG.add(4, ar);
POG.add(4, ar);
POG.add(4, ar);
2020-05-20 03:53:04 -04:00
2017-08-20 16:11:38 -04:00
uint8_t a[1000];
randomSeed(1);
for (int i = 0; i < 1000; i++) a[i] = random(32768);
uint32_t t1 = micros();
for (int i = 0; i < 1000; i++)
{
for (uint16_t b = 1; b; b <<= 1)
{
digitalWrite(2, a[i] & b);
}
}
uint32_t t2 = micros();
uint32_t t3 = micros();
for (int i = 0; i < 1000; i++)
{
2020-05-20 03:53:04 -04:00
POG.write(a[i]);
2017-08-20 16:11:38 -04:00
}
uint32_t t4 = micros();
2020-05-20 03:53:04 -04:00
Serial.println(F("1000x random bit: average case"));
Serial.print(F("ORG: "));
2017-08-20 16:11:38 -04:00
Serial.println(t2 - t1);
2020-05-20 03:53:04 -04:00
Serial.print(F("NEW: "));
2017-08-20 16:11:38 -04:00
Serial.println(t4 - t3);
2020-05-20 03:53:04 -04:00
Serial.print(F(" %: "));
Serial.println(1.0 * (t4 - t3) / (t2 - t1));
Serial.println();
2017-08-20 16:11:38 -04:00
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
void test8()
{
2020-05-20 03:53:04 -04:00
Serial.println(__FUNCTION__);
2021-01-29 06:31:58 -05:00
Serial.println(F("Test available()...\n"));
2020-05-20 03:53:04 -04:00
2017-08-20 16:11:38 -04:00
PinOutGroup POG;
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
2017-08-20 16:11:38 -04:00
Serial.println(POG.size());
2021-01-29 06:31:58 -05:00
Serial.print(F("available: "));
Serial.println(POG.available());
2020-05-20 03:53:04 -04:00
2017-08-20 16:11:38 -04:00
POG.add(4, ar );
2020-05-20 03:53:04 -04:00
Serial.print(F("size: "));
2017-08-20 16:11:38 -04:00
Serial.println(POG.size());
2021-01-29 06:31:58 -05:00
Serial.print(F("available: "));
Serial.println(POG.available());
2020-05-20 03:53:04 -04:00
Serial.print(__FUNCTION__);
Serial.println(F(" done..."));
2017-08-20 16:11:38 -04:00
Serial.println();
}
2021-11-13 09:19:09 -05:00
2020-05-20 03:53:04 -04:00
// -- END OF FILE --
2021-12-23 12:59:45 -05:00