diff --git a/.cproject b/.cproject
index 5d7b493..309b751 100644
--- a/.cproject
+++ b/.cproject
@@ -1,234 +1,279 @@
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
+
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
+
-
-
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
-
+
-
+
-
-
-
-
-
-
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
diff --git a/.project b/.project
index 70a48d6..5861cb8 100644
--- a/.project
+++ b/.project
@@ -1,7 +1,7 @@
NeoPixelF103MVP
-
+
@@ -23,8 +23,6 @@
org.eclipse.cdt.managedbuilder.core.managedBuildNature
org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
fr.ac6.mcu.ide.core.MCUProjectNature
+ org.eclipse.cdt.core.ccnature
-
-
-
diff --git a/App/App.cpp b/App/App.cpp
new file mode 100644
index 0000000..570d90c
--- /dev/null
+++ b/App/App.cpp
@@ -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
+#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);
+ }
+ }
+}
diff --git a/App/LedBuffers.c b/App/LedBuffers.c
new file mode 100644
index 0000000..2c07eec
--- /dev/null
+++ b/App/LedBuffers.c
@@ -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];
+
+
+
diff --git a/App/LedBuffers.h b/App/LedBuffers.h
new file mode 100644
index 0000000..fff08a8
--- /dev/null
+++ b/App/LedBuffers.h
@@ -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_ */
diff --git a/App/Pixel.h b/App/Pixel.h
new file mode 100644
index 0000000..ac6cc26
--- /dev/null
+++ b/App/Pixel.h
@@ -0,0 +1,27 @@
+/*
+ * Pixel.h
+ *
+ * Created on: Dec 16, 2018
+ * Author: abody
+ */
+
+#ifndef PIXEL_H_
+#define PIXEL_H_
+
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ uint8_t g;
+ uint8_t r;
+ uint8_t b;
+} Pixel_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PIXEL_H_ */
diff --git a/App/Sparkle.cpp b/App/Sparkle.cpp
new file mode 100644
index 0000000..b928f82
--- /dev/null
+++ b/App/Sparkle.cpp
@@ -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;
+
+}
diff --git a/App/Sparkle.h b/App/Sparkle.h
new file mode 100644
index 0000000..be2d191
--- /dev/null
+++ b/App/Sparkle.h
@@ -0,0 +1,26 @@
+/*
+ * Sparkle.h
+ *
+ * Created on: Dec 16, 2018
+ * Author: abody
+ */
+
+#ifndef SPARKLE_H_
+#define SPARKLE_H_
+#include
+#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_ */
diff --git a/Inc/main.h b/Inc/main.h
index c59aeea..18a4596 100644
--- a/Inc/main.h
+++ b/Inc/main.h
@@ -58,8 +58,15 @@
/* #define USE_FULL_ASSERT 1U */
/* USER CODE BEGIN Private defines */
+#ifdef __cplusplus
+ extern "C" {
+#endif
extern volatile uint8_t g_done;
-/* USER CODE END Private defines */
+#ifdef __cplusplus
+}
+#endif
+
+ /* USER CODE END Private defines */
#ifdef __cplusplus
extern "C" {
diff --git a/Src/main.c b/Src/main.c
index 11481e8..f999d13 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -45,21 +45,18 @@
/* USER CODE BEGIN Includes */
#include
+#include "App/Pixel.h"
+#include "App/LedBuffers.h"
+
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
-#define NUMPIXELS 144
volatile uint8_t g_done = 0;
-typedef struct {
- uint8_t g;
- uint8_t r;
- uint8_t b;
-} pixel_t;
-pixel_t g_pixels[NUMPIXELS];
-uint8_t g_ledBits[sizeof(g_pixels) * 8 / 2 + 1];
+
+
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -67,23 +64,10 @@ void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
-
+void App();
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
-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--);
- }
-}
/* USER CODE END 0 */
@@ -124,45 +108,12 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
-
-#define BRIGHTNESS 2
-#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);
- }
-
+ while(1) {
+ App();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
- }
+ }
/* USER CODE END 3 */
}