aboutsummaryrefslogtreecommitdiff
path: root/main/main.c
diff options
context:
space:
mode:
authorgamerdonkey2024-09-14 14:48:59 -0500
committergamerdonkey2024-09-14 14:48:59 -0500
commit35ff74c9be46e16030d1184a506451e1d3805c25 (patch)
tree0ee143a6a3ddf74ee985165cb10a1c44d9b8377f /main/main.c
parent5d3f7c6d67dd70b644c5527b07b72f03459413dd (diff)
downloadesp32-inova-led-controller-35ff74c9be46e16030d1184a506451e1d3805c25.tar.gz
esp32-inova-led-controller-35ff74c9be46e16030d1184a506451e1d3805c25.tar.bz2
esp32-inova-led-controller-35ff74c9be46e16030d1184a506451e1d3805c25.zip
First attempt at integrating Adafruit_GFX and converting to C++
Diffstat (limited to 'main/main.c')
-rw-r--r--main/main.c170
1 files changed, 0 insertions, 170 deletions
diff --git a/main/main.c b/main/main.c
deleted file mode 100644
index 34ac38e..0000000
--- a/main/main.c
+++ /dev/null
@@ -1,170 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "driver/spi_master.h"
-#include "driver/gpio.h"
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "atascii.h"
-#include "esp_log.h"
-
-
-#define NUM_COL 160
-#define NUM_ROW 8
-#define CHAR_HEIGHT 8
-#define CHAR_WIDTH 8
-#define RED 1
-#define GREEN 2
-#define ORANGE 3
-
-#define PIN_NUM_MISO 12
-#define PIN_NUM_MOSI 13
-#define PIN_NUM_CLK 14
-#define PIN_NUM_CS 15
-#define PIN_NUM_R_LATCH 16
-#define PIN_NUM_R_CLK 17
-#define PIN_NUM_R_ADDR_0 5
-#define PIN_NUM_R_ADDR_1 18
-#define PIN_NUM_R_ADDR_2 19
-
-#define TAG "led_display"
-#define STACK_SIZE 2000
-
-static uint16_t pattern[NUM_ROW][NUM_COL / 8]; // each column is two bits, so 8 can fit in 16-bit integer
-
-
-void setPixel(uint16_t x, uint16_t y, uint16_t color) {
- pattern[y][x / 8] = pattern[y][x / 8] & ~(0b0000000000000011 << (14 - (2 * (x % 8)))); // clear this column (note the bitwise inversion)
- pattern[y][x / 8] = pattern[y][x / 8] | (color << (14 - (2 * (x % 8)))); // write color to column
-}
-
-
-void drawCharAtPosition(char character, int x_start, int y_start, uint16_t color) {
- uint8_t cur_char_row;
- for(int y = y_start; (y - y_start) < CHAR_HEIGHT && y < NUM_ROW; y++) {
- cur_char_row = atascii_font[(uint8_t) character][y - y_start];
- for(int x = x_start; (x - x_start) < CHAR_WIDTH && x < NUM_COL; x++) {
- if(x >= 0) {
- if((0b10000000 >> (x - x_start)) & cur_char_row) {
- setPixel(x, y, color);
- }
- else {
- setPixel(x, y, 0);
- }
- }
- }
- }
-}
-
-
-void updateDisplay(void* params)
-{
- esp_err_t ret;
- spi_device_handle_t spi;
-
- spi_bus_config_t buscfg={
- .miso_io_num=PIN_NUM_MISO,
- .mosi_io_num=PIN_NUM_MOSI,
- .sclk_io_num=PIN_NUM_CLK,
- .quadwp_io_num=-1,
- .quadhd_io_num=-1,
- .max_transfer_sz=3*600*2*8,
- .flags=SPICOMMON_BUSFLAG_DUAL
- };
-
- spi_device_interface_config_t devcfg={
- .clock_speed_hz=1*1000*1000, //Clock out at 1 MHz
- .mode=0, //SPI mode 0
- .spics_io_num=PIN_NUM_CS, //CS pin
- .queue_size=1, //We want to be able to queue 7 transactions at a time
- .flags=SPI_DEVICE_HALFDUPLEX, //Needed to use both data lines for output
- };
-
- //Initialize the SPI bus and attach our device handle
- ret=spi_bus_initialize(HSPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
- ESP_ERROR_CHECK(ret);
- ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
- ESP_ERROR_CHECK(ret);
-
- ESP_LOGI(TAG, "Initialized SPI host.");
-
- gpio_set_direction(PIN_NUM_R_LATCH, GPIO_MODE_OUTPUT);
- gpio_set_direction(PIN_NUM_R_CLK, GPIO_MODE_OUTPUT);
- gpio_set_direction(PIN_NUM_R_ADDR_0, GPIO_MODE_OUTPUT);
- gpio_set_direction(PIN_NUM_R_ADDR_1, GPIO_MODE_OUTPUT);
- gpio_set_direction(PIN_NUM_R_ADDR_2, GPIO_MODE_OUTPUT);
-
- ESP_LOGI(TAG, "Setup GPIO.");
-
- TickType_t xLastWakeTime;
- const TickType_t xFrequency = 1 / portTICK_PERIOD_MS;
- xLastWakeTime = xTaskGetTickCount();
-
- uint8_t current_row = 0;
- uint16_t refresh_count = 0;
- TickType_t startMillis = xTaskGetTickCount();
- TickType_t endMillis;
- uint8_t i = 0;
- uint16_t data;
- while(true) {
- vTaskDelayUntil(&xLastWakeTime, xFrequency);
-
- for(i = 0; i < (NUM_COL / 8); i++) {
- spi_transaction_t t;
- memset(&t, 0, sizeof(t));
- t.length = 16;
- data = SPI_SWAP_DATA_TX(pattern[current_row][i], 16);
- t.tx_buffer = &data;
- t.flags = SPI_TRANS_MODE_DIO; // output on two data lines. Even bits go to GREEN, odd to RED
- ret = spi_device_polling_transmit(spi, &t);
- ESP_ERROR_CHECK(ret);
- }
-
- gpio_set_level(PIN_NUM_R_LATCH, 0);
- gpio_set_level(PIN_NUM_R_CLK, 1);
-
- gpio_set_level(PIN_NUM_R_ADDR_0, 1 & current_row);
- gpio_set_level(PIN_NUM_R_ADDR_1, 1 & (current_row >> 1));
- gpio_set_level(PIN_NUM_R_ADDR_2, 1 & (current_row >> 2));
-
- gpio_set_level(PIN_NUM_R_LATCH, 1);
- gpio_set_level(PIN_NUM_R_CLK, 0);
-
- if(++current_row == NUM_ROW) {
- current_row = 0;
-
- if(++refresh_count == 1000) {
- endMillis = xTaskGetTickCount();
- ESP_LOGI(TAG, "Refresh Rate: %f Hz", (float) refresh_count * 1000 / (endMillis - startMillis));
- refresh_count = 0;
- startMillis = xTaskGetTickCount();
- }
- }
- }
-}
-
-
-void app_main(void)
-{
- TaskHandle_t taskHandle;
- xTaskCreate(updateDisplay, "updateDisplay", STACK_SIZE, NULL, tskIDLE_PRIORITY, &taskHandle);
-
- int j = NUM_COL;
- int color = 1;
- char* hello = "Hello World!! 0123456789";
- int hello_length = strlen(hello);
- int min_x = hello_length * -CHAR_WIDTH;
- while(true) {
- for(int i = 0; i < strlen(hello); i++) {
- drawCharAtPosition(hello[i], j + (i * CHAR_WIDTH), 0, color);
- }
-
- if(--j == min_x) {
- j = NUM_COL;
- if(++color > 3) {
- color = 1;
- }
- }
- vTaskDelay(50 / portTICK_PERIOD_MS);
- }
-}