Skip to content

Commit

Permalink
hotp: add code for displaying keys on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Jun 19, 2024
1 parent 8f82f76 commit bf3477b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/screen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include <u8g2-tock.h>
#include <u8g2.h>

#include "step0.h"
#include "screen.h"

u8g2_t u8g2;
bool inited = false;

int display_hotp_keys(hotp_key_t* hotp_key, int num_keys) {
// Only init if not previously init-ed.
if (!inited) {
int ret = u8g2_tock_init(&u8g2);
if (ret != 0) {
return -1;
}
inited = true;
}


// int height = u8g2_GetDisplayHeight(&u8g2);
int width = u8g2_GetDisplayWidth(&u8g2);

u8g2_SetFont(&u8g2, u8g2_font_profont17_tr);
u8g2_SetFontPosTop(&u8g2);

u8g2_ClearBuffer(&u8g2);

// Draw title.
u8g2_DrawStr(&u8g2, 0, 0, "HOTP App");

// Draw line below title.
int title_height = u8g2_GetMaxCharHeight(&u8g2);
u8g2_DrawHLine(&u8g2, 0, title_height , width);

// Draw for each key.
u8g2_SetFont(&u8g2, u8g2_font_helvR08_tr);
int lines_start = title_height+1;
int offset = u8g2_GetMaxCharHeight(&u8g2) + 1;

for (int i=0;i<num_keys;i++) {
char buf[100];

int y_pos = lines_start + (offset * i);

if (hotp_key[i].len == 0) {
snprintf(buf, 100, "Key %i: (unused)", i);
} else {
snprintf(buf, 100, "Key %i: %li", i, (uint32_t) hotp_key[i].counter);
}
u8g2_DrawStr(&u8g2, 0, y_pos, buf);
}

// Write to display.
u8g2_SendBuffer(&u8g2);


return 0;

}
4 changes: 4 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once


int display_hotp_keys(hotp_key_t* hotp_key, int num_keys);

0 comments on commit bf3477b

Please sign in to comment.