aboutsummaryrefslogtreecommitdiff
path: root/main/main.c
blob: 4caa26ae550efa772d64e153f0d5c2e5c3ca3716 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "driver/spi_master.h"
#include "drv/ws2812b.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/sockets.h"
#include "sdkconfig.h"
#include "station.h"

#ifdef CONFIG_IDF_TARGET_ESP32
#define LCD_HOST HSPI_HOST

#define PIN_NUM_MISO 25
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK 19
#define PIN_NUM_CS 22

#define PIN_NUM_DC 21
#define PIN_NUM_RST 18
#define PIN_NUM_BCKL 5
#elif defined CONFIG_IDF_TARGET_ESP32S2
#define LCD_HOST SPI2_HOST

#define PIN_NUM_MISO 37
#define PIN_NUM_MOSI 35
#define PIN_NUM_CLK 36
#define PIN_NUM_CS 34

#define PIN_NUM_DC 4
#define PIN_NUM_RST 5
#define PIN_NUM_BCKL 6
#elif defined CONFIG_IDF_TARGET_ESP32C3
#define LCD_HOST SPI2_HOST

#define PIN_NUM_MISO 2
#define PIN_NUM_MOSI 7
#define PIN_NUM_CLK 6
#define PIN_NUM_CS 10

#define PIN_NUM_DC 9
#define PIN_NUM_RST 4
#define PIN_NUM_BCKL 5
#endif

struct {
  char color[32];
  ws2812b_t* drv;
} ws_params;

#define SIZE 300
void calculate_colors(ws2812b_buffer_t* buffer)
{
  /* RACE Conditions for sure. Need to fix that. */
  ws2812b_rgb_t color = {0};
  if (!strcmp(ws_params.color, "black\n")) {
  } else if (!strcmp(ws_params.color, "green\n")) {
    color.g = 255;
  } else if (!strcmp(ws_params.color, "red\n")) {
    color.r = 255;
  } else if (!strcmp(ws_params.color, "blue\n")) {
    color.b = 255;
  } else if (!strcmp(ws_params.color, "yellow\n")) {
    color.r = 255;
    color.g = 255;
  } else if (!strcmp(ws_params.color, "magenta\n")) {
    color.r = 255;
    color.b = 255;
  } else if (!strcmp(ws_params.color, "teal\n")) {
    color.g = 255;
    color.b = 255;
  } else {
    color.r = 128;
    color.g = 128;
    color.b = 128;
  }

  int i;
  for (i = 0; i < SIZE; ++i) {
    ws2812b_buffer_set_color(buffer, i, &color);
  }
}

portTASK_FUNCTION(ws2812b_write_task, params)
{
  strcpy(ws_params.color, "red\n");

  ws2812b_buffer_t* buffer = ws2812b_new_buffer(SIZE);
  for (;;) {
    calculate_colors(buffer);
    ws2812b_write(ws_params.drv, buffer);
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}

portTASK_FUNCTION(tcp_server, params)
{
  wifi_init_station("Wort", "JoshIsBau5");

  int s = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_port = htons(1234);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  int err = bind(s, (struct sockaddr*)&addr, sizeof(addr));
  if (err) {
    printf("Error binding to sockaddr: %d\n", err);
    return;
  }

  printf("Listening ...\n");
  listen(s, 1);

  while (true) {
    printf("Waiting for connection... \n");

    struct sockaddr_in client_addr;
    socklen_t size;
    int sock = accept(s, (struct sockaddr*)&client_addr, &size);

    printf("Accepted connection\n");
    ssize_t len;

    while ((len = read(sock, ws_params.color, sizeof(ws_params.color) - 1)) >
           0) {
      ws_params.color[len] = 0;
      printf("Read %s\n", ws_params.color);
    }
  }
}

void app_main(void)
{
  esp_err_t error;
  spi_device_handle_t spi;
  spi_bus_config_t cfg = {
      .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 = 320 * 2 + 8,
  };

  spi_device_interface_config_t devcfg = {
      .clock_speed_hz = 24 * 100 * 1000, /* 2.5 MHz */
      .mode = 0,
      .spics_io_num = PIN_NUM_CS,
      .queue_size = 7,
      .pre_cb = NULL,
  };

  printf("Hello, World!\n");
  printf("miso: %d\n", PIN_NUM_MISO);
  printf("mosi: %d\n", PIN_NUM_MOSI);
  printf("sclk: %d\n", PIN_NUM_CLK);

  error = spi_bus_initialize(HSPI_HOST, &cfg, SPI_DMA_CH_AUTO);
  printf("Bus Init\n");
  ESP_ERROR_CHECK(error);

  error = spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
  printf("Add Device\n");
  ESP_ERROR_CHECK(error);

  ws_params.drv = ws2812b_init(spi);

  printf("Configuration complete!!\n");

  xTaskCreate(ws2812b_write_task, "ws2812b_writer", 4096, NULL, 1, NULL);
  xTaskCreate(tcp_server, "tcp_server", 4096, NULL, 2, NULL);
}