Initial version
authorCamil Staps <info@camilstaps.nl>
Sun, 7 May 2017 12:35:35 +0000 (12:35 +0000)
committerCamil Staps <info@camilstaps.nl>
Sun, 7 May 2017 12:35:35 +0000 (12:35 +0000)
terminal/terminal.ino [new file with mode: 0644]

diff --git a/terminal/terminal.ino b/terminal/terminal.ino
new file mode 100644 (file)
index 0000000..643f2c2
--- /dev/null
@@ -0,0 +1,134 @@
+#include <LiquidCrystal.h>
+
+LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
+
+#define COLS 20
+#define ROWS 4
+
+/**
+ * Special characters.
+ * See https://www.quinapalus.com/hd44780udg.html to make more.
+ * Define which ones are used (max. 8) in setup().
+ * When they need to be changed at runtime, make sure the cursor is disabled
+ * and call setCursor afterwards.
+ */
+byte char_music[8] = {0x02,0x03,0x02,0x0e,0x1e,0x0c,0x00};
+byte char_bar_1[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x1f};
+byte char_bar_2[8] = {0x00,0x00,0x00,0x00,0x00,0x1f,0x1f};
+byte char_bar_3[8] = {0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f};
+byte char_bar_4[8] = {0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f};
+byte char_bar_5[8] = {0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f};
+byte char_bar_6[8] = {0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
+byte char_bar_7[8] = {0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
+byte char_hbar_start_empty[8] = {0x1f,0x10,0x10,0x10,0x10,0x10,0x1f};
+byte char_hbar_start_full[8]  = {0x1f,0x10,0x17,0x17,0x17,0x10,0x1f};
+byte char_hbar_inner_empty[8] = {0x1f,0x00,0x00,0x00,0x00,0x00,0x1f};
+byte char_hbar_inner_half[8]  = {0x1f,0x00,0x1c,0x1c,0x1c,0x00,0x1f};
+byte char_hbar_inner_full[8]  = {0x1f,0x00,0x1f,0x1f,0x1f,0x00,0x1f};
+byte char_hbar_end_empty[8]   = {0x1f,0x01,0x01,0x01,0x01,0x01,0x1f};
+byte char_hbar_end_full[8]    = {0x1f,0x01,0x1d,0x1d,0x1d,0x01,0x1f};
+
+struct terminal {
+       byte x;
+       byte y;
+       char contents[COLS * ROWS];
+};
+
+void redraw(struct terminal *term)
+{
+       lcd.noCursor();
+       lcd.clear();
+       lcd.home();
+       for (byte i = 0; i < COLS * ROWS; i++) {
+               if (i % COLS == 0)
+                       lcd.setCursor(i % COLS, i / COLS);
+               lcd.write(term->contents[i]);
+       }
+       lcd.cursor();
+}
+
+void scroll_up(struct terminal *term)
+{
+       for (byte i = 0; i < COLS * (ROWS - 1); i++)
+               term->contents[i] = term->contents[i + COLS];
+       for (byte i = COLS * (ROWS - 1); i < COLS * ROWS; i++)
+               term->contents[i] = ' ';
+       redraw(term);
+}
+
+/**
+ * Special character meanings are defined here.
+ */
+void write(struct terminal *term, char ch)
+{
+       switch (ch) {
+               case '\r':
+                       term->x = 0;
+                       lcd.setCursor(term->x, term->y);
+                       break;
+               case '\n':
+                       if (++term->y >= ROWS) {
+                               term->y--;
+                               scroll_up(term);
+                       }
+                       lcd.setCursor(term->x, term->y);
+                       break;
+               case 0x7f:
+                       if (term->x != 0) {
+                               term->x--;
+                               term->contents[term->x + term->y * COLS] = 0x00;
+                               lcd.setCursor(term->x, term->y);
+                               lcd.write(' ');
+                               lcd.setCursor(term->x, term->y);
+                       }
+                       break;
+               default:
+                       term->contents[term->x + term->y * COLS] = ch;
+                       lcd.write(ch);
+                       if (++term->x >= COLS) {
+                               term->x = 0;
+                               if (++term->y >= ROWS) {
+                                       term->y--;
+                                       scroll_up(term);
+                               }
+                               lcd.setCursor(term->x, term->y);
+                       }
+       }
+}
+
+struct terminal term;
+
+char message[] =
+       "Beste Mart, van\r\n"
+       "harte gefeliciteerd!"
+       "Hier een herprogram-"
+       "meerbare terminal. ";
+
+void setup()
+{
+       lcd.createChar(0, char_music);
+       lcd.createChar(1, char_hbar_start_full);
+       lcd.createChar(2, char_hbar_start_empty);
+       lcd.createChar(3, char_hbar_inner_full);
+       lcd.createChar(4, char_hbar_inner_half);
+       lcd.createChar(5, char_hbar_inner_empty);
+       lcd.createChar(6, char_hbar_end_full);
+       lcd.createChar(7, char_hbar_end_empty);
+
+       lcd.begin(COLS, ROWS);
+       lcd.blink();
+       Serial.begin(9600);
+
+       for (byte i = 0; i < ROWS * COLS; i++)
+               term.contents[i] = ' ';
+
+       for (byte i = 0; i < sizeof(message) - 1; i++)
+               write(&term, message[i]);
+}
+
+void loop()
+{
+       if (Serial.available()) {
+               write(&term, Serial.read());
+       }
+}