Skip to content

Instantly share code, notes, and snippets.

@RickKimball
Last active November 22, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickKimball/0edca71bbd6caa2a450e67570859febf to your computer and use it in GitHub Desktop.
Save RickKimball/0edca71bbd6caa2a450e67570859febf to your computer and use it in GitHub Desktop.
#include <Streaming.h>
#include "StreamingEx.h"
void setup() {
Serial.begin(115200);
}
int counter = 0;
int max_counter = 1000;
int8_t hexdec = 0;
void loop() {
if ( hexdec ) {
Serial << "counter=0x" << _HEXZ(counter, 4) << endl;
}
else {
Serial << "counter=" << _DECZ(counter, 4) << endl;
}
if ( counter + 1 <= max_counter ) {
counter = counter + 1;
}
else {
counter = 0;
hexdec ^= 1;
max_counter = (hexdec) ? 0x1000 : 1000;
delay(20000);
}
}
#pragma once
struct _HEXZ {
unsigned value;
unsigned len;
_HEXZ(int i, int l = 2): value(i), len(l) {}
};
inline Print &operator <<(Print &obj, const _HEXZ &arg) {
if ( arg.len > 1 && arg.value < 0x10 ) obj.print('0');
if ( arg.len > 2 && arg.value < 0x100 ) obj.print('0');
if ( arg.len > 3 && arg.value < 0x1000 ) obj.print('0');
obj.print(arg.value, HEX);
return obj;
}
struct _DECZ {
unsigned value;
unsigned len;
_DECZ(int i, int l = 2): value(i), len(l) {}
};
inline Print &operator <<(Print &obj, const _DECZ &arg) {
if ( arg.len > 1 && arg.value < 10 ) obj.print('0');
if ( arg.len > 2 && arg.value < 100 ) obj.print('0');
if ( arg.len > 3 && arg.value < 1000 ) obj.print('0');
if ( arg.len > 4 && arg.value < 10000 ) obj.print('0');
obj.print(arg.value, DEC);
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment