Modularization, stage 1
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/02/03.
|
||||
//
|
||||
|
||||
#include "platform.h"
|
||||
#include "unit_base.h"
|
||||
#include "unit_neopixel.h"
|
||||
|
||||
#define NPX_INTERNAL
|
||||
#include "_npx_internal.h"
|
||||
#include "ws2812.h"
|
||||
|
||||
/* Clear the strip */
|
||||
error_t UU_Npx_Clear(Unit *unit)
|
||||
{
|
||||
CHECK_TYPE(unit, &UNIT_NEOPIXEL);
|
||||
|
||||
struct priv *priv = unit->data;
|
||||
ws2812_clear(priv->port, priv->ll_pin, priv->cfg.pixels);
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/* Load packed */
|
||||
error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes)
|
||||
{
|
||||
CHECK_TYPE(unit, &UNIT_NEOPIXEL);
|
||||
|
||||
struct priv *priv = unit->data;
|
||||
if (nbytes != 3*priv->cfg.pixels) return E_BAD_COUNT;
|
||||
ws2812_load_raw(priv->port, priv->ll_pin, packed_rgb, priv->cfg.pixels);
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/* Load U32, LE or BE */
|
||||
error_t UU_Npx_Load32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool order_bgr, bool zero_before)
|
||||
{
|
||||
CHECK_TYPE(unit, &UNIT_NEOPIXEL);
|
||||
|
||||
struct priv *priv = unit->data;
|
||||
if (nbytes != 4*priv->cfg.pixels) return E_BAD_COUNT;
|
||||
ws2812_load_sparse(priv->port, priv->ll_pin, bytes, priv->cfg.pixels, order_bgr, zero_before);
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/* Get the pixel count */
|
||||
error_t UU_Npx_GetCount(Unit *unit, uint16_t *count)
|
||||
{
|
||||
CHECK_TYPE(unit, &UNIT_NEOPIXEL);
|
||||
|
||||
struct priv *priv = unit->data;
|
||||
*count = priv->cfg.pixels;
|
||||
return E_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/02/03.
|
||||
//
|
||||
|
||||
#include "platform.h"
|
||||
#include "unit_base.h"
|
||||
|
||||
#define NPX_INTERNAL
|
||||
#include "_npx_internal.h"
|
||||
#include "ws2812.h"
|
||||
|
||||
/** Allocate data structure and set defaults */
|
||||
error_t Npx_preInit(Unit *unit)
|
||||
{
|
||||
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv));
|
||||
if (priv == NULL) return E_OUT_OF_MEM;
|
||||
|
||||
// some defaults
|
||||
priv->cfg.pin = R_PA0;
|
||||
priv->cfg.pixels = 1;
|
||||
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/** Finalize unit set-up */
|
||||
error_t Npx_init(Unit *unit)
|
||||
{
|
||||
bool suc = true;
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
// --- Parse config ---
|
||||
suc = hw_pinrsc2ll(priv->cfg.pin, &priv->port, &priv->ll_pin);
|
||||
if (!suc) return E_BAD_CONFIG;
|
||||
TRY(rsc_claim(unit, priv->cfg.pin));
|
||||
|
||||
// --- Init hardware ---
|
||||
LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_OUTPUT);
|
||||
LL_GPIO_SetPinOutputType(priv->port, priv->ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
|
||||
LL_GPIO_SetPinSpeed(priv->port, priv->ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
|
||||
|
||||
// clear strip
|
||||
|
||||
ws2812_clear(priv->port, priv->ll_pin, priv->cfg.pixels);
|
||||
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/** Tear down the unit */
|
||||
void Npx_deInit(Unit *unit)
|
||||
{
|
||||
// pins are de-inited during teardown
|
||||
|
||||
// Release all resources
|
||||
rsc_teardown(unit);
|
||||
|
||||
// Free memory
|
||||
free_ck(unit->data);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/02/03.
|
||||
//
|
||||
|
||||
#ifndef GEX_F072_NPX_INTERNAL_H
|
||||
#define GEX_F072_NPX_INTERNAL_H
|
||||
|
||||
#ifndef NPX_INTERNAL
|
||||
#error bad include!
|
||||
#endif
|
||||
|
||||
#include "unit_base.h"
|
||||
|
||||
/** Private data structure */
|
||||
struct priv {
|
||||
struct {
|
||||
Resource pin;
|
||||
uint16_t pixels;
|
||||
} cfg;
|
||||
|
||||
uint32_t ll_pin;
|
||||
GPIO_TypeDef *port;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** Allocate data structure and set defaults */
|
||||
error_t Npx_preInit(Unit *unit);
|
||||
|
||||
/** Load from a binary buffer stored in Flash */
|
||||
void Npx_loadBinary(Unit *unit, PayloadParser *pp);
|
||||
|
||||
/** Write to a binary buffer for storing in Flash */
|
||||
void Npx_writeBinary(Unit *unit, PayloadBuilder *pb);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** Parse a key-value pair from the INI file */
|
||||
error_t Npx_loadIni(Unit *unit, const char *key, const char *value);
|
||||
|
||||
/** Generate INI file section for the unit */
|
||||
void Npx_writeIni(Unit *unit, IniWriter *iw);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** Finalize unit set-up */
|
||||
error_t Npx_init(Unit *unit);
|
||||
|
||||
/** Tear down the unit */
|
||||
void Npx_deInit(Unit *unit);
|
||||
|
||||
#endif //GEX_F072_NPX_INTERNAL_H
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/02/03.
|
||||
//
|
||||
|
||||
#include "platform.h"
|
||||
#include "unit_base.h"
|
||||
|
||||
#define NPX_INTERNAL
|
||||
#include "_npx_internal.h"
|
||||
|
||||
/** Load from a binary buffer stored in Flash */
|
||||
void Npx_loadBinary(Unit *unit, PayloadParser *pp)
|
||||
{
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
priv->cfg.pin = (Resource) pp_u8(pp);
|
||||
priv->cfg.pixels = pp_u16(pp);
|
||||
}
|
||||
|
||||
/** Write to a binary buffer for storing in Flash */
|
||||
void Npx_writeBinary(Unit *unit, PayloadBuilder *pb)
|
||||
{
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
pb_u8(pb, priv->cfg.pin);
|
||||
pb_u16(pb, priv->cfg.pixels);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** Parse a key-value pair from the INI file */
|
||||
error_t Npx_loadIni(Unit *unit, const char *key, const char *value)
|
||||
{
|
||||
bool suc = true;
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
if (streq(key, "pin")) {
|
||||
priv->cfg.pin = cfg_pinrsc_parse(value, &suc);
|
||||
}
|
||||
else if (streq(key, "pixels")) {
|
||||
priv->cfg.pixels = cfg_u16_parse(value, &suc);
|
||||
}
|
||||
else {
|
||||
return E_BAD_KEY;
|
||||
}
|
||||
|
||||
if (!suc) return E_BAD_VALUE;
|
||||
return E_SUCCESS;
|
||||
}
|
||||
|
||||
/** Generate INI file section for the unit */
|
||||
void Npx_writeIni(Unit *unit, IniWriter *iw)
|
||||
{
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
iw_comment(iw, "Data pin");
|
||||
iw_entry_s(iw, "pin", cfg_pinrsc_encode(priv->cfg.pin));
|
||||
|
||||
iw_comment(iw, "Number of pixels");
|
||||
iw_entry_d(iw, "pixels", priv->cfg.pixels);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/11/25.
|
||||
//
|
||||
|
||||
#include "unit_base.h"
|
||||
#include "unit_neopixel.h"
|
||||
|
||||
#define NPX_INTERNAL
|
||||
#include "_npx_internal.h"
|
||||
|
||||
enum PinCmd_ {
|
||||
CMD_CLEAR = 0,
|
||||
CMD_LOAD = 1,
|
||||
CMD_LOAD_ZRGB = 4, // 0,0 - trail zero, bgr
|
||||
CMD_LOAD_ZBGR = 5, // 0,1
|
||||
CMD_LOAD_RGBZ = 6, // 1,0
|
||||
CMD_LOAD_BGRZ = 7, // 1,1
|
||||
CMD_GET_LEN = 10,
|
||||
};
|
||||
|
||||
/** Handle a request message */
|
||||
static error_t Npx_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
|
||||
{
|
||||
uint32_t len;
|
||||
const uint8_t *bytes;
|
||||
|
||||
switch (command) {
|
||||
/** Clear the entire strip */
|
||||
case CMD_CLEAR:
|
||||
return UU_Npx_Clear(unit);
|
||||
|
||||
/** Load packed RGB colors (length must match the strip size) */
|
||||
case CMD_LOAD:;
|
||||
bytes = pp_tail(pp, &len);
|
||||
return UU_Npx_Load(unit, bytes, len);
|
||||
|
||||
/** Load sparse (uint32_t) colors */
|
||||
case CMD_LOAD_ZRGB:
|
||||
case CMD_LOAD_ZBGR:
|
||||
case CMD_LOAD_RGBZ:
|
||||
case CMD_LOAD_BGRZ:
|
||||
bytes = pp_tail(pp, &len);
|
||||
bool trail_zero = (bool) (command & 0b10);
|
||||
bool order_bgr = (bool) (command & 0b01);
|
||||
return UU_Npx_Load32(unit, bytes, len, order_bgr, !trail_zero);
|
||||
|
||||
/** Get the Neopixel strip length */
|
||||
case CMD_GET_LEN:;
|
||||
uint16_t count;
|
||||
TRY(UU_Npx_GetCount(unit, &count));
|
||||
com_respond_u16(frame_id, count);
|
||||
return E_SUCCESS;
|
||||
|
||||
default:
|
||||
return E_UNKNOWN_COMMAND;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** Unit template */
|
||||
const UnitDriver UNIT_NEOPIXEL = {
|
||||
.name = "NPX",
|
||||
.description = "Neopixel RGB LED strip",
|
||||
// Settings
|
||||
.preInit = Npx_preInit,
|
||||
.cfgLoadBinary = Npx_loadBinary,
|
||||
.cfgWriteBinary = Npx_writeBinary,
|
||||
.cfgLoadIni = Npx_loadIni,
|
||||
.cfgWriteIni = Npx_writeIni,
|
||||
// Init
|
||||
.init = Npx_init,
|
||||
.deInit = Npx_deInit,
|
||||
// Function
|
||||
.handleRequest = Npx_handleRequest,
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/11/25.
|
||||
//
|
||||
// NeoPixel RGB LED strip bit-banged output.
|
||||
// The nanosecond timing is derived from the AHB clock speed.
|
||||
//
|
||||
|
||||
#ifndef U_NEOPIXEL_H
|
||||
#define U_NEOPIXEL_H
|
||||
|
||||
#include "unit.h"
|
||||
|
||||
extern const UnitDriver UNIT_NEOPIXEL;
|
||||
|
||||
/**
|
||||
* Clear the Neopixel strip
|
||||
*
|
||||
* @param unit
|
||||
* @return success
|
||||
*/
|
||||
error_t UU_Npx_Clear(Unit *unit);
|
||||
|
||||
/**
|
||||
* Load the strip with packed bytes R,G,B.
|
||||
*
|
||||
* @param unit
|
||||
* @param packed_rgb - bytes to load
|
||||
* @param nbytes - number of bytes, must be count*3
|
||||
* @return success
|
||||
*/
|
||||
error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes);
|
||||
|
||||
/**
|
||||
* Load from 32-bit numbers
|
||||
* @param unit
|
||||
* @param bytes
|
||||
* @param nbytes
|
||||
* @param order_bgr
|
||||
* @param zero_before
|
||||
* @return success
|
||||
*/
|
||||
error_t UU_Npx_Load32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool order_bgr, bool zero_before);
|
||||
|
||||
/**
|
||||
* Get number of pixels on the strip
|
||||
*
|
||||
* @param unit
|
||||
* @param count - destination for the count value
|
||||
* @return success
|
||||
*/
|
||||
error_t UU_Npx_GetCount(Unit *unit, uint16_t *count);
|
||||
|
||||
#endif //U_NEOPIXEL_H
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "platform.h"
|
||||
#include "ws2812.h"
|
||||
|
||||
#define FREQ_STEP (PLAT_AHB_MHZ/20.0f)
|
||||
#define NPX_DELAY_SHORT (uint32_t)(FREQ_STEP*1.5f)
|
||||
#define NPX_DELAY_LONG (uint32_t)(FREQ_STEP*3.5f)
|
||||
#define NPX_DELAY_SHOW (uint32_t)(FREQ_STEP*50)
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
void ws2812_byte(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t b)
|
||||
{
|
||||
for (register volatile uint8_t i = 0; i < 8; i++) {
|
||||
LL_GPIO_SetOutputPin(port, ll_pin);
|
||||
|
||||
// duty cycle determines bit value
|
||||
if (b & 0x80) {
|
||||
__asm_loop(NPX_DELAY_LONG);
|
||||
LL_GPIO_ResetOutputPin(port, ll_pin);
|
||||
__asm_loop(NPX_DELAY_SHORT);
|
||||
} else {
|
||||
__asm_loop(NPX_DELAY_SHORT);
|
||||
LL_GPIO_ResetOutputPin(port, ll_pin);
|
||||
__asm_loop(NPX_DELAY_LONG);
|
||||
}
|
||||
|
||||
b <<= 1; // shift to next bit
|
||||
}
|
||||
}
|
||||
|
||||
/** Set many RGBs from packed stream */
|
||||
void ws2812_load_raw(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count)
|
||||
{
|
||||
vPortEnterCritical();
|
||||
uint8_t b, g, r;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
r = *rgbs++;
|
||||
g = *rgbs++;
|
||||
b = *rgbs++;
|
||||
ws2812_byte(port, ll_pin, g);
|
||||
ws2812_byte(port, ll_pin, r);
|
||||
ws2812_byte(port, ll_pin, b);
|
||||
}
|
||||
vPortExitCritical();
|
||||
__asm_loop(NPX_DELAY_SHOW);
|
||||
}
|
||||
|
||||
/** Set many RGBs from uint32 stream */
|
||||
void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count,
|
||||
bool order_bgr, bool zero_before)
|
||||
{
|
||||
vPortEnterCritical();
|
||||
uint8_t b, g, r;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
if (zero_before) rgbs++; // skip
|
||||
if (order_bgr) {
|
||||
b = *rgbs++;
|
||||
g = *rgbs++;
|
||||
r = *rgbs++;
|
||||
} else {
|
||||
r = *rgbs++;
|
||||
g = *rgbs++;
|
||||
b = *rgbs++;
|
||||
}
|
||||
if (!zero_before) rgbs++; // skip
|
||||
|
||||
ws2812_byte(port, ll_pin, g);
|
||||
ws2812_byte(port, ll_pin, r);
|
||||
ws2812_byte(port, ll_pin, b);
|
||||
}
|
||||
vPortExitCritical();
|
||||
__asm_loop(NPX_DELAY_SHOW);
|
||||
}
|
||||
|
||||
/** Set many RGBs */
|
||||
void ws2812_clear(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t count)
|
||||
{
|
||||
vPortEnterCritical();
|
||||
for (uint32_t i = 0; i < count*3; i++) {
|
||||
ws2812_byte(port, ll_pin, 0);
|
||||
}
|
||||
vPortExitCritical();
|
||||
__asm_loop(NPX_DELAY_SHOW);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef WS2812_H
|
||||
#define WS2812_H
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
/**
|
||||
* Load RGBs from a packed byte stream
|
||||
*
|
||||
* @param port
|
||||
* @param ll_pin
|
||||
* @param rgbs - packed R,G,B, R,G,B, ... array
|
||||
* @param count - number of pixels (triplets)
|
||||
*/
|
||||
void ws2812_load_raw(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count);
|
||||
|
||||
/**
|
||||
* Load all pixels with BLACK (0,0,0)
|
||||
*
|
||||
* @param port
|
||||
* @param ll_pin
|
||||
* @param count - number of pixels
|
||||
*/
|
||||
void ws2812_clear(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t count);
|
||||
|
||||
/**
|
||||
* Load from a stream of 32-bit numbers (4th or 1st byte skipped)
|
||||
* @param port
|
||||
* @param ll_pin
|
||||
* @param rgbs - payload
|
||||
* @param count - number of pixels
|
||||
* @param order_bgr - B,G,R colors, false - R,G,B
|
||||
* @param zero_before - insert padding byte before colors, false - after
|
||||
*/
|
||||
void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, const uint8_t *rgbs, uint32_t count,
|
||||
bool order_bgr, bool zero_before);
|
||||
|
||||
#endif //WS2812_H
|
||||
Reference in New Issue
Block a user