]> Devi Nivas Git - 4dulcd-nolibrary.git/commitdiff
Add moving circle demo
authorAdvaith Menon <noreply-git@bp4k.net>
Thu, 22 Jan 2026 20:10:09 +0000 (15:10 -0500)
committerAdvaith Menon <noreply-git@bp4k.net>
Thu, 22 Jan 2026 20:10:09 +0000 (15:10 -0500)
* Add demo of moving circle which shows change in
  baud rates

main/main.c

index 73c962d9bbffb84e16bf1684c6a09ebe0a3f8b66..19285b2eee3403582d7b08bfdf4fe9df6f738386 100644 (file)
@@ -3,6 +3,7 @@
 #include "freertos/FreeRTOS.h"
 #include "esp_log.h"
 #include "esp_system.h"
+#include <stdint.h>
 #include "driver/uart.h"
 
 #define PIN_UART_RXD 4
 #define ULCD_DEFAULT_STOP_BITS UART_STOP_BITS_1
 #define TIME_LIMIT 2000
 
+/* screen width and height */
+#define SCR_W   128
+#define SCR_H   128
+
 
 static const char *TAG = "main";
 static const uart_port_t uart_port = UART_NUM_1;
@@ -39,8 +44,12 @@ void update_baud(int baud);
 
 
 void ulcd_init(void);
-void ulcd_txt_demo(void);
 void ulcd_cls(void);
+void ulcd_txt_demo(void);
+void ulcd_moving_circle(void);
+void ulcd_faster_baud(void);
+void ulcd_default_baud(void);
+void ulcd_baud(int, int);
 
 
 void app_main(void)
@@ -76,12 +85,25 @@ void app_main(void)
 
     ulcd_cls();
     ulcd_txt_demo();
+    vTaskDelay(pdMS_TO_TICKS(3000)); /* wait for a bit */
 
+    ulcd_cls();
+    ulcd_moving_circle();
+    vTaskDelay(pdMS_TO_TICKS(1000)); /* let the user digest how bad that was */
 
+    ulcd_faster_baud();
+    ulcd_cls();
+    ulcd_moving_circle();
+    /* Hey, it's Andy from LTT here and look at my crazy-fast 60fps gaming
+     * monitor! */
+    vTaskDelay(pdMS_TO_TICKS(1000));
+
+    /* Allows the program to loop w/o resetting uLCD */
+    ulcd_default_baud();
 
 
     ESP_LOGI(TAG, "Work is done");
-    vTaskDelay(pdMS_TO_TICKS(10000));
+    vTaskDelay(pdMS_TO_TICKS(1000));
     esp_restart();
 }
 
@@ -115,7 +137,7 @@ void ulcd_cls(void) {
 
 
 void ulcd_txt_demo(void) {
-    unsigned char instr[100];
+    unsigned char instr[16];
     unsigned char ack;
     int written; /* or read */
 
@@ -178,6 +200,123 @@ void ulcd_txt_demo(void) {
 }
 
 
+void ulcd_moving_circle(void) {
+    /* This demo generates a circle that moves from left to right of the screen.
+     * It is supposed to show that having a higher baud rate makes a difference.
+     */
+    uint16_t current_x = 0;
+    uint16_t current_y = 0;
+    uint16_t step_x = 2;
+    uint16_t step_y = 2;
+    const uint16_t rad = 15;
+    int written;
+    unsigned char ack;
+    unsigned char instr[10];
+    /* using gfx_CircleFilled */
+    instr[0] = 0xFF;
+    instr[1] = 0xCC;
+
+    while (current_x < SCR_W && current_y < SCR_H) {
+        /* we draw a moving circle as long as it is in bounds */
+        uint16_t prev_x = (current_x - step_x) % SCR_W;
+        uint16_t prev_y = (current_y - step_y) % SCR_H;
+
+        /* draw a black circle to cover our tracks */
+
+        /* x coord */
+        instr[2] = (prev_x >> 8);
+        instr[3] = prev_x & 0xFF;
+        /* y coord */
+        instr[4] = (prev_y >> 8);
+        instr[5] = prev_y & 0xFF;
+
+        /* rad */
+        instr[6] = (rad >> 8);
+        instr[7] = rad;
+
+        /* color (British spelling used in docs) */
+        instr[8] = 0x00;
+        instr[9] = 0x00;
+
+        written = write_tx(instr, 10);
+        flush_tx(TIME_LIMIT);
+
+        if (written != 10) ESP_LOGE(TAG, "Failed to write gfx_CircleFilled!");
+
+        written = read_rx(&ack, 1, TIME_LIMIT);
+        if (written != 1 || ack != 6)
+            ESP_LOGE(TAG, "Failed to get acknowledge");
+
+
+        /* draw blue circle */
+        instr[2] = (current_x >> 8);
+        instr[3] = current_x & 0xFF;
+        /* y coord */
+        instr[4] = (current_y >> 8);
+        instr[5] = current_y & 0xFF;
+
+        /* rad */
+        instr[6] = (rad >> 8);
+        instr[7] = rad;
+
+        /* 5 bits for red, 6 for green and 5 for blue */
+        instr[8] = 0x00;
+        instr[9] = 0x1F;
+
+        written = write_tx(instr, 10);
+        flush_tx(TIME_LIMIT);
+
+        if (written != 10) ESP_LOGE(TAG, "Failed to write gfx_CircleFilled!");
+
+        written = read_rx(&ack, 1, TIME_LIMIT);
+        if (written != 1 || ack != 6)
+            ESP_LOGE(TAG, "Failed to get acknowledge");
+
+
+        /* increment motion counters */
+        current_x += step_x;
+        current_y += step_y;
+    }
+}
+
+
+void ulcd_faster_baud(void) {
+    /* this makes the baud rate the max possible */
+    ulcd_baud(4, 600000);
+}
+
+
+void ulcd_default_baud(void) {
+    /* this makes the baud rate the max possible */
+    ulcd_baud(312, 9600);
+}
+
+
+void ulcd_baud(int baud_table_idx, int actual_baud) {
+    unsigned char instr[4], ack;
+    int written;
+
+    /* see the setbaudWait function */
+    instr[0] = 0x00;
+    instr[1] = 0x0B;
+    instr[2] = (baud_table_idx >> 8);
+    instr[3] = baud_table_idx;
+
+    written = write_tx(instr, 4);
+    flush_tx(TIME_LIMIT);
+
+    if (written != 4) ESP_LOGE(TAG, "Failed to write setbaudWait!");
+
+    update_baud(actual_baud);
+
+    /* wait 100 ms for the acknowledge in the new baud rate */
+    vTaskDelay(pdMS_TO_TICKS(100));
+
+    written = read_rx(&ack, 1, TIME_LIMIT);
+    if (written != 1 || ack != 6)
+        ESP_LOGE(TAG, "Failed to get acknowledge");
+}
+
 /* ================================================================= */
 
 int write_tx(const void *data, size_t sz) {