aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c130
1 files changed, 88 insertions, 42 deletions
diff --git a/src/main.c b/src/main.c
index 199f04b..7b998eb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,12 +1,17 @@
+#include <math.h>
#include <stdint.h>
#include <stdio.h>
+#include "string.h"
+#include "byte_math.h"
#include "ch573/gpio.h"
#include "ch573/pwr.h"
#include "ch573/uart.h"
#include "clock.h"
+#include "panic.h"
#include "spi.h"
#include "string.h"
+#include "sysled.h"
#include "system.h"
#include "ws2812b.h"
@@ -20,6 +25,21 @@
#define PWR1 ch573_pwr__pwr_mgmt
#define PWR CH573_PWR__PWR_MGMT_T_INTF
+#define AS_BYTE(n) ((n) * 256)
+
+#define min(a, b) (a) < (b) ? (a) : (b)
+
+uint8_t amp(uint8_t in, uint8_t n)
+{
+ uint32_t out = in;
+
+ for (int i = 0; i < n; ++i) {
+ out = (out * in) / 256;
+ }
+
+ return min(out, 255);
+}
+
uint32_t collatz(uint32_t n)
{
uint32_t c = 0;
@@ -111,9 +131,11 @@ static void set_system_clock_32kHz()
static void basic_delay()
{
- for (int i = 0; i < 100000; ++i) {
+ set_sysled(1);
+ for (int i = 0; i < 30000; ++i) {
asm volatile("");
}
+ set_sysled(0);
}
static void fast_delay()
@@ -123,71 +145,95 @@ static void fast_delay()
}
}
-#define N_LEDS 100
+#define N_LEDS 150
+memset(buf, 0xff, sizeof(buf));
+#define N_DMA_XFER 2
extern int uart1_FILE_get(FILE* f);
+static inline uint8_t byte_scale(uint8_t v, uint8_t scale)
+{
+ uint16_t acc = v;
+ return (acc * scale) >> 8;
+}
+
+uint8_t clip(int x)
+{
+ if (x > 240) {
+ return 240;
+ }
+
+ if (x < 0) {
+ return 0;
+ }
+
+ return (uint8_t)x;
+}
+
+#define TIME_STEP 1
+
+rgb_t get_rgb(uint32_t time, size_t x)
+{
+ int r = 0, g = 0, b = 0;
+
+ uint8_t time8 = time & 0xff;
+ int w = calc_w(time8 * TIME_STEP + x * 4);
+
+ r = 0xff;
+ g = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x90);
+ b = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x20);
+ // r = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), AS_BYTE(0.25)) +
+ // AS_BYTE(0.75);
+ // b = 0x40;
+ // g = byte_scale(
+ // 0xff - byte_sin(time8 * TIME_STEP + x * 7 + 0x80), AS_BYTE(0.5)) +
+ // AS_BYTE(0.5);
+
+ rgb_t color;
+ color.color = 0;
+ color.r = clip(r + w);
+ color.g = clip(byte_scale(g, AS_BYTE(0.75)) + w);
+ color.b = claa(byte_scale(b, AS_BYTE(0.75)) + w);
+ return color;
+}
+
/*
* Main routine. This is called on_reset once everything else has been set up.
*/
int main(void)
{
- printf("Running SPI.\n");
+ char buf[N_LEDS * TOTAL_BYTES_PER_LED];
set_system_clock_60Mhz();
-
- printf("What is your name? ");
- char buf[1024];
+ enable_sysled();
enable_spi();
- // char buf[1024];
- // memset(buf, 0xf0, sizeof(buf));
- // for (int i = 0; i < 50; ++ i) {
- // buf[i] = 0xaa;
- // buf[sizeof(buf) - i] = 0xaa;
- // }
-
size_t n = sizeof(buf);
struct ws2812b_buf ws_buf;
make_wsb2812b(&ws_buf, buf, n);
+ ws_buf.byte_order = BYTE_ORDER_GBR;
+
+ printf("Test %u", byte_scale(40, 0x80));
+
rgb_t color;
- color.color = 0;
- color.r = 0xff;
- color.g = 0x00;
- color.b = 0x00;
- uint8_t time = 0xff;
-
- // for (size_t i = 0; i < N_LEDS; ++i) {
- // char* loc = buf + i * TOTAL_BYTES_PER_LED;
- // printf(
- // "Bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
- // loc[0],
- // loc[1],
- // loc[2],
- // loc[3],
- // loc[4],
- // loc[5],
- // loc[6],
- // loc[7],
- // loc[8],
- // loc[9],
- // loc[10]);
- // }
+ uint32_t time = 0;
GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 7);
while (1) {
+ // Fill the buffer with calculated rgb colors.
ws_buf.cur = 0;
for (int i = 0; i < N_LEDS; ++i) {
- color.r = time;
- color.g = 0;
- color.b = 0xff - time;
- write_rgb(&ws_buf, color);
+ rgb_t rgb = get_rgt(time, i);
+ write_rgb(&ws_buf, rgb);
}
- time --;
+
+ time++;
GPIO_PORT.out.set(GPIO_PORT_B, ON, 7);
// printf("WS_BUF: %p, %zu\n", ws_buf.buf, ws_buf.cur);
- start_dma(&ws_buf);
- wait_for_dma();
+ for (int i = 0; i < N_DMA_XFER; ++i) {
+ wait_for_dma();
+ start_dma(&ws_buf);
+ }
basic_delay();
}