Skip to content

Instantly share code, notes, and snippets.

@teddokano
Created August 17, 2023 19:21
Show Gist options
  • Save teddokano/f2a63ee5aa0a8c0afc1d273c47d67145 to your computer and use it in GitHub Desktop.
Save teddokano/f2a63ee5aa0a8c0afc1d273c47d67145 to your computer and use it in GitHub Desktop.
zprintf sample code
void zprintf(const char *format, ...);
#define COLS 8
#define ROWS (128 / 8)
void setup() {
Serial.begin(9600);
while (!Serial)
;
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLS; x++) {
int n = x * ROWS + y;
zprintf("%c%3d (0x%02X) '%c',", x ? ' ' : '\n', n, n, isprint(n) ? n: ' ' );
}
}
}
void loop() {
}
#define MAX_STR_LENGTH 80
void zprintf(const char *format, ...) {
char s[MAX_STR_LENGTH];
va_list args;
va_start(args, format);
vsnprintf(s, MAX_STR_LENGTH, format, args);
va_end(args);
Serial.print(s);
}
// プロトタイプ宣言
void zprintf(const char *format, ...);
// プログラム本体
void setup() {
Serial.begin(9600);
while (!Serial)
;
zprintf("Hello, world!\n");
}
// 繰り返しやらせたいことは特にないのでここは空っぽ
void loop() {
}
// ここからが`zprintf`に関連する部分
#define MAX_ZPRINTF_LENGTH 80 // 一度に変換する最大文字数
void zprintf(const char *format, ...) {
char s[MAX_ZPRINTF_LENGTH];
va_list args;
va_start(args, format);
vsnprintf(s, MAX_ZPRINTF_LENGTH, format, args);
va_end(args);
Serial.print(s);
}
void setup() {
Serial.begin(9600);
while (!Serial)
;
Serial.println("Hello, world!\n");
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment