Skip to content
Snippets Groups Projects
main.c 1.26 KiB
Newer Older
// Copyright (c) Charles Lohr
// All rights reserved

#include <mdk.h>
#include "esp32-c3-uart-interface.c"

// On the ESP32C3 dev boards, the WS2812 LED is connected to GPIO 8
static int ws_2812_pin = 8;

// Simple hue function for generation of smooth rainbow.
static uint8_t hueval(int value) {
  value = value % 1536;
  if (value < 256) {
    return (uint8_t) value;
  } else if (value < 768) {
    return 255;
  } else if (value < 1024) {
    return (uint8_t) (1023 - value);
  } else {
    return 0;
  }
}

static void blink(int count, unsigned long delay_millis) {
  uint8_t green[3] = {1, 0, 0}, black[3] = {0, 0, 0};
  for (int i = 0; i < count; i++) {
    ws2812_show(ws_2812_pin, green, sizeof(green));
    delay_ms(delay_millis);
    ws2812_show(ws_2812_pin, black, sizeof(black));
    delay_ms(delay_millis);
  }
}

static void rainbow(int count) {
  for (int i = 0; i < count; i++) {
    uint8_t val = hueval(i);
    val = (uint8_t) (val/100);
    uint8_t rgb[3] = {0, val, val};
    ws2812_show(ws_2812_pin, rgb, sizeof(rgb));
    delay_ms(1);
  }
}

int main(void) {
  wdt_disable();
  gpio_output(ws_2812_pin);
  init_uart();
  config_uart();
  uint8_t cycle = 0;

  for (;;) {
    printf("Cycle %d\n", cycle++);
    blink(2, 200);
    rainbow(2500);
  }

  return 0;
}