Explorar el Código

Initial commit

main_ledos
PaulStoffregen hace 7 años
commit
5e0f7ea9fb
Se han modificado 2 ficheros con 103 adiciones y 0 borrados
  1. +50
    -0
      WS2812Serial.cpp
  2. +53
    -0
      WS2812Serial.h

+ 50
- 0
WS2812Serial.cpp Ver fichero

@@ -0,0 +1,50 @@

#include "WS2812Serial.h"

void WS2812Serial::begin()
{
if (!uart) return;
if (!dma) dma = new DMAChannel;
if (!dma) return;

SIM_SCGC4 |= SIM_SCGC4_UART0;
uint32_t divisor = BAUD2DIV(2400000);

CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);

UART0_BDH = (divisor >> 13) & 0x1F;
UART0_BDL = (divisor >> 5) & 0xFF;
UART0_C4 = divisor & 0x1F;

UART0_C1 = 0;
UART0_PFIFO = 0;
UART0_C2 = UART_C2_TE;
UART0_C3 = UART_C3_TXINV;
UART0_C5 = UART_C5_TDMAS;

frameBuffer[0] = 0xDB;
frameBuffer[1] = 0xDA;
frameBuffer[2] = 0xDB;

dma->sourceBuffer(frameBuffer, 3);
dma->destination(UART0_D);
dma->transferSize(1);
dma->transferCount(3);
dma->disableOnCompletion();

dma->triggerAtHardwareEvent(DMAMUX_SOURCE_UART0_TX);
}

void WS2812Serial::show()
{

//UART0_D = 0xDB;
//UART0_D = 0xDA;
//UART0_D = 0xDB;
dma->enable();
UART0_C2 = UART_C2_TE | UART_C2_TIE;

}



+ 53
- 0
WS2812Serial.h Ver fichero

@@ -0,0 +1,53 @@
#ifndef WS2812Serial_h_
#define WS2812Serial_h_

#include <Arduino.h>
#include "DMAChannel.h"

#define WS2812_RGB 0 // The WS2811 datasheet documents this way
#define WS2812_RBG 1
#define WS2812_GRB 2 // Most LED strips are wired this way
#define WS2812_GBR 3
#define WS2812_BRG 4
#define WS2812_BGR 5

class WS2812Serial {
public:
constexpr WS2812Serial(uint16_t n, void *f, void *d, HardwareSerial &s, uint8_t c) :
numled(n), config(c),
frameBuffer((uint8_t *)f), drawBuffer((uint8_t *)d),
uart( &s == &Serial1 ? &KINETISK_UART0 : nullptr)
{
}
void begin();
void setPixel(uint32_t num, int color) {
if (num >= numled) return;
num *= 3;
frameBuffer[num+0] = color;
frameBuffer[num+1] = color >> 8;
frameBuffer[num+2] = color >> 16;
}
void setPixel(uint32_t num, uint8_t red, uint8_t green, uint8_t blue) {
if (num >= numled) return;
num *= 3;
frameBuffer[num+0] = blue;
frameBuffer[num+1] = green;
frameBuffer[num+2] = red;
}
void show();
bool busy();
uint16_t numPixels() {
return numled;
}
private:
const uint16_t numled;
const uint8_t config;
uint8_t *frameBuffer;
uint8_t *drawBuffer;
//HardwareSerial &serial;
KINETISK_UART_t *uart;
DMAChannel *dma = nullptr;

};

#endif

Cargando…
Cancelar
Guardar