led_bikewheel/led_bikewheel.ino
2020-12-29 21:37:39 +01:00

87 lines
1.9 KiB
C++

#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
#include <APA102.h>
#include "images.h"
const int HALL_PIN = 2;
const int ROUND_COUNT = 2;
// Define which pins to use.
const uint8_t dataPin = 10;
const uint8_t clockPin = 11;
const int num_segments = 360;
const uint16_t led_count = 28;
const uint8_t brightness = 1;
const int state_count = 4;
rgb_color color_buffer[led_count];
float passed = 0;
int current_image_index = 0;
uint8_t *current_palette;
uint8_t *current_pixels;
// Create an object for writing to the LED strip.
APA102<dataPin, clockPin> ledStrip;
void setup()
{
Serial.begin(115200);
pinMode(HALL_PIN, INPUT);
pinMode(LED_BUILTIN, INPUT);
current_palette = (uint8_t *)pgm_read_word(&images[current_image_index].palette);
current_pixels = (uint8_t *)pgm_read_word(&images[current_image_index].pixels);
}
void loop()
{
float start = micros();
int count = 0;
bool change = false;
while (count < ROUND_COUNT)
{
// check HALL if turned
if (digitalRead(HALL_PIN) == LOW)
{
digitalWrite(LED_BUILTIN, HIGH);
if (!change)
{
change = true;
count++;
}
}
else
{
digitalWrite(LED_BUILTIN, LOW);
change = false;
}
// calc led for last turn
float current_diff = micros() - start;
int state = ((float)passed / current_diff * num_segments);
uint8_t pixel_color_index;
uint8_t *current_pixel = (uint8_t *)&current_pixels[state * led_count];
for (int i = 0; i < led_count; i++)
{
pixel_color_index = pgm_read_byte(current_pixel++) * 3;
color_buffer[i] = rgb_color(
pgm_read_byte(&current_palette[pixel_color_index]),
pgm_read_byte(&current_palette[pixel_color_index + 1]),
pgm_read_byte(&current_palette[pixel_color_index + 2]));
}
ledStrip.write(color_buffer, led_count, brightness);
}
passed = (micros() - start);
}