From d154d22eb374a1e18d8628f62851459eff57804c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 28 Aug 2015 14:18:07 +0200 Subject: [PATCH] added missing includes & added SIPO PWM module --- lib/adc.c | 1 + lib/adc.h | 1 + lib/calc.h | 2 ++ lib/debounce.h | 1 + lib/sd_blockdev.h | 1 + lib/sd_fat.h | 2 ++ lib/sipo_pwm.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ lib/sipo_pwm.h | 44 +++++++++++++++++++++++++ lib/wsrgb.h | 2 ++ 9 files changed, 137 insertions(+) create mode 100644 lib/sipo_pwm.c create mode 100644 lib/sipo_pwm.h diff --git a/lib/adc.c b/lib/adc.c index da4bcfc..0de38eb 100644 --- a/lib/adc.c +++ b/lib/adc.c @@ -1,5 +1,6 @@ #include #include +#include #include "calc.h" #include "adc.h" diff --git a/lib/adc.h b/lib/adc.h index fdd08d2..4f7a3f6 100644 --- a/lib/adc.h +++ b/lib/adc.h @@ -5,6 +5,7 @@ // #include +#include /** Initialize the ADC */ void adc_init(); diff --git a/lib/calc.h b/lib/calc.h index 9b74562..2d150e6 100644 --- a/lib/calc.h +++ b/lib/calc.h @@ -4,6 +4,8 @@ // Bit and byte manipulation utilities // +#include + // --- Increment in range --- // when overflown, wraps within range. Lower bound < upper bound. diff --git a/lib/debounce.h b/lib/debounce.h index 42ea71c..ab903d6 100644 --- a/lib/debounce.h +++ b/lib/debounce.h @@ -29,6 +29,7 @@ #include #include +#include #include "calc.h" #include "iopins.h" diff --git a/lib/sd_blockdev.h b/lib/sd_blockdev.h index 68e53b8..fc479fa 100644 --- a/lib/sd_blockdev.h +++ b/lib/sd_blockdev.h @@ -1,6 +1,7 @@ #pragma once #include "blockdev.h" +#include /** Initialize the SD card block device */ bool sdb_init(BLOCKDEV* dev); diff --git a/lib/sd_fat.h b/lib/sd_fat.h index 1940ca0..7bbbc01 100644 --- a/lib/sd_fat.h +++ b/lib/sd_fat.h @@ -7,6 +7,8 @@ // and hides the implementation. All regular ff_* functions will work on the FFILE. // +#include + #include "fat16.h" #include "stream.h" diff --git a/lib/sipo_pwm.c b/lib/sipo_pwm.c new file mode 100644 index 0000000..3c0fab4 --- /dev/null +++ b/lib/sipo_pwm.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#include "sipo_pwm.h" +#include "iopins.h" +#include "sipo_pwm_config.h" + + +/* -------- SIPO PWM MODULE ---------- */ + +/** Buffer for sending bits to SIPO */ +bool _buff[SPWM_CHANNELS]; + +uint8_t spwm_levels[SPWM_CHANNELS]; + + +/** Send _buff to SIPO */ +void _send_buffer() +{ + for (int8_t i = SPWM_CHANNELS - 1; i >= 0; i--) + { + #if (SPWM_INVERT) + set_pin(SPWM_DATA, !_buff[i]); /* Common anode */ + #else + set_pin(SPWM_DATA, _buff[i]); /* Common cathode */ + #endif + + // send a CLK pulse + pin_high(SPWM_CLK); + pin_low(SPWM_CLK); + } + + // send a STR pulse + pin_high(SPWM_STR); + pin_low(SPWM_STR); +} + + +void spwm_init() +{ + // Pin directions + as_output(SPWM_CLK); + as_output(SPWM_STR); + as_output(SPWM_DATA); + + // Initial states + pin_low(SPWM_CLK); + pin_low(SPWM_STR); +} + + +/** + * Display PWM channels. + * This could be called in a Timer ISR. + */ +void spwm_send() +{ + // Set all bits to 1 (if their PWM level is 0, set to 0) + for (uint8_t bit = 0; bit < SPWM_CHANNELS; bit++) + { + _buff[bit] = (bool) spwm_levels[bit]; + } + + // Show initial state + _send_buffer(); + + // For each PWM level... + for (uint16_t pwm = 0; pwm < SPWM_COLOR_DEPTH; pwm++) + { + // Turn OFF bits that are below the level + for (uint8_t bit = 0; bit < SPWM_CHANNELS; bit++) + { + if (spwm_levels[bit] < pwm) + { + _buff[bit] = 0; + } + } + + // And show... + _send_buffer(); + } +} diff --git a/lib/sipo_pwm.h b/lib/sipo_pwm.h new file mode 100644 index 0000000..ed6a3df --- /dev/null +++ b/lib/sipo_pwm.h @@ -0,0 +1,44 @@ +#pragma once + +// --- SIPO PWM Module --- +// +// SIPO = Serial IN, Parallel OUT +// +// This module lets you use SIPO outputs as a "software PWM". +// +// Tested on 74HC4094, +// should also work on 74HC595 (may need some small changes) + +#include + +// Your file with configs +#include "sipo_pwm_config.h" +/* + // PWM pin aliases + #define SPWM_STR D2 + #define SPWM_CLK D3 + #define SPWM_DATA D4 + + // Number of PWM levels (color depth) + #define SPWM_COLOR_DEPTH 256 + + // Number of SIPO channels + #define SPWM_CHANNELS 24 + + // Invert outputs (for Common Anode LEDs) + #define SPWM_INVERT 1 +*/ + + +// Array for setting PWM levels (PWM_CHANNELS-long) +extern uint8_t spwm_levels[SPWM_CHANNELS]; + + +/** Configure output pins etc */ +void spwm_init(); + + +/** Display PWM channels. + * This could be called in a Timer ISR. + */ +void spwm_send(); diff --git a/lib/wsrgb.h b/lib/wsrgb.h index 391bd1f..a7d98db 100644 --- a/lib/wsrgb.h +++ b/lib/wsrgb.h @@ -8,6 +8,8 @@ // Create a config file rgb_config.h next to your main.c // +#include + #include "iopins.h" #include "color.h"