Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * IDE_Plotter_v2.ino
- *
- * library of functions to make displaying multiple data in the Arduino IDE Plotter easier
- *
- * version 1.0 - September 2023 - Trent M. Wyatt
- *
- */
- #include <Stream.h>
- #include <String.h>
- #define NUM_GROUPS 3
- #define NUM_VALUES 3
- #define MAX_TITLE 16
- #define ARRAYSZ(A) (sizeof(A) / sizeof(*(A)))
- typedef int type_t;
- struct group_t {
- type_t values[NUM_VALUES];
- char titles[NUM_VALUES][MAX_TITLE];
- int max_val;
- uint8_t count;
- uint8_t overlay : 1,
- unused : 7;
- group_t() :
- max_val(256),
- count(0),
- overlay(true)
- {
- for (type_t &t : values) {
- t = 0;
- }
- }
- group_t(uint16_t max) :
- max_val(max),
- count(0),
- overlay(true)
- {
- for (type_t &t : values) {
- t = 0;
- }
- }
- bool create(char const * const names[], int const num) {
- if (num > NUM_VALUES) { return false; }
- for (int index=0; index < num; index++) {
- strncpy(titles[index], names[index], MAX_TITLE);
- }
- count = num;
- return true;
- }
- void set(uint8_t const index, type_t const value) {
- values[index] = value;
- }
- type_t get(uint8_t const index) const {
- return values[index];
- }
- }; // struct group_t
- struct plotter_t {
- Stream *stream;
- group_t groups[NUM_GROUPS];
- uint8_t count;
- plotter_t(Stream &serial) : stream(&serial), count(0)
- {
- }
- void set_groups(int const size, group_t *init=nullptr) {
- count = size;
- if (init) {
- for (int i=0; i < size; i++) {
- groups[i] = init[i];
- }
- }
- }
- bool create(int const group, char const *names[], int const num) {
- if (num > NUM_VALUES) { return false; }
- groups[group].create(names, num);
- return true;
- }
- void set(int const group, uint8_t const index, type_t const value) {
- groups[group].set(index, value);
- }
- type_t get(int const group, uint8_t const index) const {
- return groups[group].get(index);
- }
- void begin() const
- {
- if (nullptr == stream) { return; }
- Stream &out = *stream;
- while (out.available()) { out.read(); }
- out.write('\n');
- for (int group=0; group < count; group++) {
- for (int which=0; which < groups[group].count; which++) {
- out.print(groups[group].titles[which]);
- out.write(',');
- }
- }
- out.write('\n');
- }
- void update() const
- {
- if (nullptr == stream) { return; }
- Stream &out = *stream;
- long top = 0;
- int min_vres = 256;
- top += min_vres / 2; // bottom band
- // inter-group vertical separation
- int const vsep = min_vres + (min_vres / 8);
- for (int group=0; group < count; group++) {
- for (int which=0; which < groups[group].count; which++) {
- out.print(groups[group].values[which] + top, DEC);
- out.write(',');
- if (!groups[group].overlay && (which < (groups[group].count - 1))) {
- // inter-group vertical separation
- top += vsep;
- }
- }
- int const gvsep = min_vres + min_vres / 2;
- top += gvsep;
- }
- out.write('\n');
- }
- }; // struct plotter_t
- static double const pi = 3.1415927;
- static double const rad = pi / 2.0;
- static double const inc1 = 0.075;
- static double const inc2 = 0.1;
- double f1 = 0.0;
- double f2 = 0.0;
- bool count_up1 = true;
- bool count_up2 = true;
- int values[NUM_GROUPS][NUM_VALUES];
- plotter_t plotter(Serial);
- void setup() {
- Serial.begin(115200);
- while (!Serial);
- delay(1500);
- plotter.set_groups(NUM_GROUPS);
- char const *names1[] = { "analog1", "analog2" };
- plotter.create(0, names1, ARRAYSZ(names1));
- char const *names2[] = { "digital1", "digital2", "clock" };
- plotter.create(1, names2, ARRAYSZ(names2));
- plotter.groups[1].overlay = false;
- char const *names3[] = { "signal1" };
- plotter.create(2, names3, ARRAYSZ(names3));
- plotter.begin();
- randomSeed(analogRead(A0) + analogRead(A2));
- }
- void loop() {
- static uint8_t digitalPass = 0;
- static bool climb = true;
- for (int group=0; group < NUM_GROUPS; group++) {
- switch (group) {
- default:
- break;
- // some analog values
- case 0:
- for (int which=0; which < plotter.groups[group].count; which++) {
- switch (which) {
- case 0:
- {
- type_t &value = values[group][which];
- int const inc_amt = plotter.groups[group].max_val / 32;
- plotter.set(group, which, value);
- if (climb) {
- if (value + inc_amt >= plotter.groups[group].max_val) {
- climb = !climb;
- }
- } else {
- if (value - inc_amt <= 0) {
- climb = !climb;
- }
- }
- value += climb ? +inc_amt : -inc_amt;
- }
- break;
- case 1:
- values[group][which] = (plotter.groups[group].max_val - 1) - values[group][0];
- plotter.set(group, which, values[group][which]);
- break;
- case 2:
- if (((f1 + inc1) >= rad) || ((f1 - inc1) < -rad)) {
- count_up1 = !count_up1;
- }
- f1 += count_up1 ? +inc1 : -inc1;
- // f1 = -rad + ((pi / (plotter.min_vres / 2)) * values[group][0]);
- values[group][which] = (1.0 + sin(f1)) * (plotter.groups[group].max_val / 2);
- plotter.set(group, which, values[group][which]);
- break;
- }
- }
- break;
- // some digital values
- case 1:
- if (digitalPass == 0) {
- for (int which=0; which < plotter.groups[group].count - 1; which++) {
- values[group][which] = random(0, 2) ? plotter.groups[group].max_val : 0;
- plotter.set(group, which, values[group][which]);
- }
- // make the last one (first displayed) a clock that just toggles back and forth:
- if (values[group][plotter.groups[group].count - 1] == 0) {
- values[group][plotter.groups[group].count - 1] = plotter.groups[group].max_val;
- plotter.set(group, plotter.groups[group].count - 1, values[group][plotter.groups[group].count - 1]);
- }
- else {
- values[group][plotter.groups[group].count - 1] = 0;
- plotter.set(group, plotter.groups[group].count - 1, values[group][plotter.groups[group].count - 1]);
- }
- }
- ++digitalPass %= 8;
- break;
- case 2:
- for (int which=0; which < plotter.groups[group].count; which++) {
- switch (which) {
- case 0:
- if (((f2 + inc2) >= rad) || ((f2 - inc2) < -rad)) {
- count_up2 = !count_up2;
- }
- f2 += count_up2 ? +inc2 : -inc2;
- // f2 = -rad + ((pi / (plotter.min_vres / 2)) * values[group][1]);
- values[group][which] = (1.0 + sin(f2)) * (plotter.groups[group].max_val / 2);
- plotter.set(group, which, values[group][which]);
- break;
- case 1:
- {
- type_t &value = values[group][which];
- int const inc_amt = plotter.groups[group].max_val / 16;
- plotter.set(group, which, value);
- if (climb) {
- if (value + inc_amt >= plotter.groups[group].max_val) {
- climb = !climb;
- }
- } else {
- if (value - inc_amt <= 0) {
- climb = !climb;
- }
- }
- value += climb ? +inc_amt : -inc_amt;
- }
- break;
- case 2:
- values[group][which] = (plotter.groups[group].max_val - 1) - values[group][1];
- plotter.set(group, which, values[group][which]);
- break;
- }
- }
- break;
- }
- }
- plotter.update();
- delay(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement