towards sparkle
This commit is contained in:
parent
d140baf50e
commit
087af69592
10 changed files with 414 additions and 204 deletions
68
App/App.cpp
Normal file
68
App/App.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* App.cpp
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "dma.h"
|
||||
#include "spi.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "App/Pixel.h"
|
||||
#include "App/LedBuffers.h"
|
||||
|
||||
void convert(uint8_t *src, uint8_t *dst, uint16_t size)
|
||||
{
|
||||
static uint8_t const bits[4] = { 0b10001000, 0b10001110, 0b11101000, 0b11101110 };
|
||||
|
||||
while(size--) {
|
||||
uint8_t byte=*src++;
|
||||
uint8_t duo = 3;
|
||||
do {
|
||||
uint8_t mask = 3 << (duo<<1);
|
||||
*dst++ = bits[(byte & (mask))>>(duo<<1)];
|
||||
} while(duo--);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void App()
|
||||
{
|
||||
#define BRIGHTNESS 4
|
||||
#define DELAY 10
|
||||
|
||||
g_ledBits[sizeof(g_ledBits)-1] = 0;
|
||||
memset(g_pixels, 0, sizeof(g_pixels));
|
||||
|
||||
while(1)
|
||||
{
|
||||
for(uint16_t idx=0; idx < NUMPIXELS; idx++)
|
||||
{
|
||||
if(idx % 3 == 0) g_pixels[idx].r = BRIGHTNESS;
|
||||
else if(idx % 3 == 1) g_pixels[idx].g = BRIGHTNESS;
|
||||
else g_pixels[idx].b = BRIGHTNESS;
|
||||
|
||||
convert((uint8_t*)g_pixels, g_ledBits, sizeof(g_pixels));
|
||||
|
||||
HAL_SPI_Transmit_DMA(&hspi1, g_ledBits, sizeof(g_ledBits));
|
||||
while(!g_done);
|
||||
HAL_Delay(DELAY);
|
||||
}
|
||||
|
||||
for(uint16_t idx=0; idx < NUMPIXELS; idx++)
|
||||
{
|
||||
if(idx % 3 == 0) g_pixels[idx].r = 0;
|
||||
else if(idx % 3 == 1) g_pixels[idx].g = 0;
|
||||
else g_pixels[idx].b = 0;
|
||||
|
||||
convert((uint8_t*)g_pixels, g_ledBits, sizeof(g_pixels));
|
||||
|
||||
HAL_SPI_Transmit_DMA(&hspi1, g_ledBits, sizeof(g_ledBits));
|
||||
while(!g_done);
|
||||
HAL_Delay(DELAY);
|
||||
}
|
||||
}
|
||||
}
|
13
App/LedBuffers.c
Normal file
13
App/LedBuffers.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* LedBuffers.c
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
#include "LedBuffers.h"
|
||||
|
||||
Pixel_t g_pixels[NUMPIXELS];
|
||||
uint8_t g_ledBits[sizeof(g_pixels) * 8 / 2 + 1];
|
||||
|
||||
|
||||
|
26
App/LedBuffers.h
Normal file
26
App/LedBuffers.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* LedBuffers.h
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#ifndef LEDBUFFERS_H_
|
||||
#define LEDBUFFERS_H_
|
||||
|
||||
#include "Pixel.h"
|
||||
|
||||
#define NUMPIXELS 144
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern Pixel_t g_pixels[NUMPIXELS];
|
||||
uint8_t g_ledBits[sizeof(g_pixels) * 8 / 2 + 1];
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LEDBUFFERS_H_ */
|
27
App/Pixel.h
Normal file
27
App/Pixel.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Pixel.h
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#ifndef PIXEL_H_
|
||||
#define PIXEL_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint8_t g;
|
||||
uint8_t r;
|
||||
uint8_t b;
|
||||
} Pixel_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* PIXEL_H_ */
|
49
App/Sparkle.cpp
Normal file
49
App/Sparkle.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Sparkle.cpp
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#include "Sparkle.h"
|
||||
|
||||
Sparkle::Sparkle()
|
||||
{
|
||||
}
|
||||
|
||||
Sparkle::~Sparkle() {
|
||||
}
|
||||
|
||||
void Sparkle::Start(Pixel_t *target, Pixel_t color, Pixel_t fadeSpeed)
|
||||
{
|
||||
m_target = target;
|
||||
*target = color;
|
||||
m_fadeSpeed = fadeSpeed;
|
||||
}
|
||||
|
||||
bool Sparkle::Step()
|
||||
{
|
||||
Pixel_t current = *m_target;
|
||||
uint8_t remaining = 3;
|
||||
|
||||
if(current.b > m_fadeSpeed.r )current.r -= m_fadeSpeed.r;
|
||||
else {
|
||||
current.b = 0;
|
||||
--remaining;
|
||||
}
|
||||
|
||||
if(current.g > m_fadeSpeed.g )current.g -= m_fadeSpeed.g;
|
||||
else {
|
||||
current.g = 0;
|
||||
--remaining;
|
||||
}
|
||||
|
||||
if(current.b > m_fadeSpeed.b )current.b -= m_fadeSpeed.b;
|
||||
else {
|
||||
current.b = 0;
|
||||
--remaining;
|
||||
}
|
||||
|
||||
return remaining != 0;
|
||||
|
||||
}
|
26
App/Sparkle.h
Normal file
26
App/Sparkle.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Sparkle.h
|
||||
*
|
||||
* Created on: Dec 16, 2018
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#ifndef SPARKLE_H_
|
||||
#define SPARKLE_H_
|
||||
#include <inttypes.h>
|
||||
#include "Pixel.h"
|
||||
|
||||
class Sparkle {
|
||||
public:
|
||||
Sparkle();
|
||||
~Sparkle();
|
||||
void Start(Pixel_t *target, Pixel_t color, Pixel_t fadeSpeed);
|
||||
bool Step();
|
||||
operator Pixel_t*() { return m_target; }
|
||||
|
||||
private:
|
||||
Pixel_t *m_target = nullptr;
|
||||
Pixel_t m_fadeSpeed = {1, 1, 1};
|
||||
};
|
||||
|
||||
#endif /* SPARKLE_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue