Skip to content

Commit

Permalink
Merge pull request #448 from tock/u8g2-increment
Browse files Browse the repository at this point in the history
U8G2: Add increment/decrement example app
  • Loading branch information
ppannuto authored Jun 21, 2024
2 parents bd49a00 + 58e0c97 commit 6ecb786
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/tests/u8g2-demos/increment/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/u8g2

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
7 changes: 7 additions & 0 deletions examples/tests/u8g2-demos/increment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
U8G2 Demo: Increment
====================

Draw a counter with up and down arrows. Pressing the buttons will increase or
decrease the count.

![Increment](increment.gif)
Binary file added examples/tests/u8g2-demos/increment/increment.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions examples/tests/u8g2-demos/increment/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>

#include <libtock/interface/button.h>

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

u8g2_t u8g2;

#define BTN_NUM_UP 0
#define BTN_NUM_DOWN 2

const char* UP_ARROW = "\x93";
const char* DOWN_ARROW = "\x8b";

int counter = 10;

static void button_callback(__attribute__ ((unused)) returncode_t ret, int btn_num, bool pressed) {
if (pressed) {
if (btn_num == BTN_NUM_UP) {
counter += 1;
} else if (btn_num == BTN_NUM_DOWN) {
counter -= 1;
}
}
}

int main(void) {
int ret = u8g2_tock_init(&u8g2);
if (ret != 0) return -100;

// Setup increment and decrement buttons.
libtock_button_notify_on_press(BTN_NUM_UP, button_callback);
libtock_button_notify_on_press(BTN_NUM_DOWN, button_callback);

// Loop, displaying the updated counter.
while (1) {
u8g2_ClearBuffer(&u8g2);

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

u8g2_SetFont(&u8g2, u8g2_font_cursor_tf);

// UP ARROW
u8g2_SetFontPosTop(&u8g2);
u8g2_DrawStr(&u8g2, width / 2, -7, UP_ARROW);

// DOWN ARROW
u8g2_SetFontPosBottom(&u8g2);
u8g2_DrawStr(&u8g2, width / 2, height + 6, DOWN_ARROW);

// COUNTER
u8g2_SetFont(&u8g2, u8g2_font_helvB14_tr);
u8g2_SetFontPosCenter(&u8g2);

char buf[100];
snprintf(buf, 100, "%i", counter);
int str_width = u8g2_GetStrWidth(&u8g2, buf);
u8g2_DrawStr(&u8g2, (width - str_width) / 2, height / 2, buf);

u8g2_SendBuffer(&u8g2);

// The only pending upcall we have at this point is the button, so this will
// only return when a button is pressed.
yield();
}
}

0 comments on commit 6ecb786

Please sign in to comment.