led_bikewheel/led_bikewheel.ino

87 lines
1.9 KiB
Arduino
Raw Normal View History

2020-12-28 19:15:19 +01:00
#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
2020-12-29 21:37:39 +01:00
2020-12-28 19:15:19 +01:00
#include <APA102.h>
2020-12-29 21:37:39 +01:00
#include "images.h"
2020-12-28 16:46:07 +01:00
const int HALL_PIN = 2;
2020-12-28 17:02:46 +01:00
const int ROUND_COUNT = 2;
2020-12-28 15:47:40 +01:00
2020-12-28 19:15:19 +01:00
// Define which pins to use.
const uint8_t dataPin = 10;
const uint8_t clockPin = 11;
2020-12-29 21:37:39 +01:00
const int num_segments = 360;
const uint16_t led_count = 28;
2020-12-28 19:15:19 +01:00
const uint8_t brightness = 1;
const int state_count = 4;
2020-12-29 21:37:39 +01:00
rgb_color color_buffer[led_count];
2020-12-28 19:15:19 +01:00
float passed = 0;
2020-12-29 21:37:39 +01:00
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;
2020-12-28 19:15:19 +01:00
void setup()
{
2020-12-28 17:02:46 +01:00
Serial.begin(115200);
2020-12-28 15:47:40 +01:00
pinMode(HALL_PIN, INPUT);
2020-12-28 19:27:15 +01:00
pinMode(LED_BUILTIN, INPUT);
2020-12-29 21:37:39 +01:00
current_palette = (uint8_t *)pgm_read_word(&images[current_image_index].palette);
current_pixels = (uint8_t *)pgm_read_word(&images[current_image_index].pixels);
2020-12-28 15:47:40 +01:00
}
2020-12-28 19:15:19 +01:00
void loop()
{
2020-12-28 17:02:46 +01:00
float start = micros();
int count = 0;
bool change = false;
2020-12-28 19:15:19 +01:00
while (count < ROUND_COUNT)
{
2020-12-28 19:21:34 +01:00
// check HALL if turned
2020-12-28 19:15:19 +01:00
if (digitalRead(HALL_PIN) == LOW)
{
2020-12-28 19:27:15 +01:00
digitalWrite(LED_BUILTIN, HIGH);
2020-12-28 19:15:19 +01:00
if (!change)
{
2020-12-28 17:02:46 +01:00
change = true;
count++;
}
2020-12-28 19:15:19 +01:00
}
else
{
2020-12-28 19:27:15 +01:00
digitalWrite(LED_BUILTIN, LOW);
2020-12-28 17:02:46 +01:00
change = false;
}
2020-12-28 19:15:19 +01:00
2020-12-28 19:21:34 +01:00
// calc led for last turn
2020-12-28 19:15:19 +01:00
float current_diff = micros() - start;
2020-12-29 21:37:39 +01:00
int state = ((float)passed / current_diff * num_segments);
2020-12-28 19:15:19 +01:00
2020-12-29 21:37:39 +01:00
uint8_t pixel_color_index;
uint8_t *current_pixel = (uint8_t *)&current_pixels[state * led_count];
for (int i = 0; i < led_count; i++)
2020-12-28 19:15:19 +01:00
{
2020-12-29 21:37:39 +01:00
pixel_color_index = pgm_read_byte(current_pixel++) * 3;
2020-12-28 19:15:19 +01:00
2020-12-29 21:37:39 +01:00
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]));
2020-12-28 19:15:19 +01:00
}
2020-12-29 21:37:39 +01:00
ledStrip.write(color_buffer, led_count, brightness);
2020-12-28 16:47:45 +01:00
}
2020-12-28 17:02:46 +01:00
2020-12-28 19:15:19 +01:00
passed = (micros() - start);
2020-12-28 15:47:40 +01:00
}