Added C-implementation for NeoPixel.

Changed a few colors/animation settings for demo purposes.

Signed-off-by: Dirk Jahnke <dirk@familie-jahnke.de>
This commit is contained in:
Dirk Jahnke
2017-12-05 12:40:37 +01:00
parent 2479cc62fd
commit b79d6dfd48
4 changed files with 103 additions and 20 deletions

81
src/NeoPixel.c Normal file
View File

@@ -0,0 +1,81 @@
#include "mgos_bitbang.h"
#include "mgos_gpio.h"
#include "mgos_system.h"
enum NeoPixel_ColorOrder {
RGB = 0,
GRB = 1,
BGR = 2,
};
#define MAX_LEDS 100
static uint8_t NeoPixel_pin = 0;
static uint8_t NeoPixel_numPixels = 0;
static uint8_t NeoPixel_leds[3 * MAX_LEDS];
static NeoPixel_ColorOrder NeoPixel_colorOrder = RGB;
// ## **`NeoPixel_create(pin, numPixels, order)`**
// Create and return a NeoPixel strip object. Example:
// ```javascript
// let pin = 5, numPixels = 16, colorOrder = NeoPixel.GRB;
// let strip = NeoPixel.create(pin, numPixels, colorOrder);
// strip.setPixel(0 /* pixel */, 12, 34, 56);
// strip.show();
//
// strip.clear();
// strip.setPixel(1 /* pixel */, 12, 34, 56);
// strip.show();
// ```
void NeoPixel_create(uint8_t, pin, uint8_t numPixels, enum NeoPixel_ColorOrder order) {
mgos_gpio_init();
NeoPixel_pin = pin;
NeoPixel_numPixels = numPixels;
NeoPixel_colorOrder = order;
// GPIO.set_mode(pin, GPIO.MODE_OUTPUT);
mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_OUTPUT);
// GPIO.write(pin, 0); // Keep in reset.
mgos_gpio_write(pin, false);
NeoPixel_clear();
}
// ## **`NeoPixel_clear()`**
// Clear in-memory values of the pixels.
void NeoPixel_clear() {
for (int i=0; i<NeoPixel_numPixels * 3; ++i) {
NeoPixel_leds[i] = 0;
}
}
// ## **`NeoPixel_set(i, r, g, b)`**
// Set i-th's pixel's RGB value.
// Note that this only affects in-memory value of the pixel.
void NeoPixel_set(uint8_t pixel, uint8_t red, uint8_t green, uint8_t blue) {
uint8_t v0, v1, v2;
if (NeoPixel_colorOrder == RGB) {
v0 = red; v1 = green; v2 = blue;
} else if (NeoPixel_colorOrder == GRB) {
v0 = green; v1 = red; v2 = blue;
} else if (NeoPixel_colorOrder == BGR) {
v0 = blue; v1 = green; v2 = red;
} else return;
NeoPixel_leds[pixel] = v0;
NeoPixel_leds[pixel+1] = v1;
NeoPixel_leds[pixel+2] = v2;
}
// ## **`NeoPixel_show()`**
// Output values of the pixels.
void NeoPixel_show() {
// GPIO.write(this.pin, 0);
mgos_gpio_write(pin, false);
// Sys.usleep(60);
mgos_usleep(60);
// BitBang.write(this.pin, BitBang.DELAY_100NSEC, 3, 8, 8, 6, this.data, this.len);
mgos_bitbang_write_bits(NeoPixel_pin, DELAY_100NSEC, 3, 8, 8, 6, NeoPixel_leds, NeoPixel_numPixels);
// GPIO.write(this.pin, 0);
mgos_gpio_write(pin, false);
// Sys.usleep(60);
mgos_usleep(60);
// GPIO.write(this.pin, 1);
mgos_gpio_write(pin, true);
}