aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/lcdlib.c71
-rw-r--r--include/lcdlib.h19
2 files changed, 90 insertions, 0 deletions
diff --git a/include/lcdlib.c b/include/lcdlib.c
new file mode 100644
index 0000000..a22a1e2
--- /dev/null
+++ b/include/lcdlib.c
@@ -0,0 +1,71 @@
+#include "lcdlib.h"
+
+// ===================================================================
+// Globals
+// ===================================================================
+
+static char previous_lines[LCD_HEIGHT][LCD_WIDTH+1];
+static FILE *lcd = NULL;
+static int line_idx;
+
+static const char* esc = "\x1B[L";
+
+int
+lcd_begin(void) {
+ lcd = fopen("/dev/lcd", "w");
+ if (lcd == NULL) return EXIT_FAILURE;
+ fprintf(lcd, "%sB", esc);
+ fflush(lcd);
+ lcd_clear();
+ return EXIT_SUCCESS;
+}
+
+void
+lcd_end(void) {
+ fclose(lcd);
+}
+
+void
+lcd_clear(void) {
+ line_idx = 0;
+ for (int y = 0; y + 1 < LCD_HEIGHT; y++) {
+ fprintf(lcd, "%sy%dx0;%sk", esc, y, esc);
+ previous_lines[y][0] = 0;
+ }
+ fflush(lcd);
+}
+
+void
+lcd_put_line(const enum LCD_WRITE_MODE mode, const char *line) {
+ if (mode == L_SCROLL && line_idx == LCD_HEIGHT) {
+ // If we're at the bottom, ``shift'' everything up
+ for (int y = 1; y < LCD_HEIGHT; y++) {
+ // Go to the start of row y, clear the line, and print the
+ // previous string there
+ fprintf(lcd, "%sy%dx0;%sk%s", esc, y-1, esc, previous_lines[y]);
+ // Overwrite the stored previous line with the next
+ strncpy(previous_lines[y-1], previous_lines[y], 16);
+ }
+ line_idx--;
+ }
+ // Go to the correct line, clear it, and write the input.
+ fprintf(lcd, "%sy%dx0;%sk%s", esc, line_idx, esc, line);
+ fflush(lcd);
+ // Store this line, null-terminate!
+ strncpy(previous_lines[line_idx], line, 16);
+ previous_lines[line_idx][16] = 0;
+ if (mode == L_SCROLL && line_idx < LCD_HEIGHT) line_idx++;
+}
+
+void
+lcd_printf_line(const enum LCD_WRITE_MODE mode,
+ const char *format, ...) {
+ char buf[16];
+
+ va_list(args);
+ va_start(args, format);
+ vsnprintf(buf, 16, format, args);
+ va_end(args);
+
+ lcd_put_line(mode, buf);
+}
diff --git a/include/lcdlib.h b/include/lcdlib.h
new file mode 100644
index 0000000..fd28a0e
--- /dev/null
+++ b/include/lcdlib.h
@@ -0,0 +1,19 @@
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#define LCD_WIDTH 16
+#define LCD_HEIGHT 2
+
+enum LCD_WRITE_MODE { L_OVERWRITE, L_SCROLL };
+
+int lcd_begin(void);
+void lcd_end(void);
+void lcd_clear(void);
+
+void lcd_put_line(const enum LCD_WRITE_MODE mode,
+ const char *line);
+
+void lcd_printf_line(const enum LCD_WRITE_MODE mode,
+ const char *format, ...);