发新帖我要提问
123
返回列表

STM32 操作 DMX512 探讨 の 2------arduino-DMX

[复制链接]
楼主: wangjiahao88
手机看帖
扫描二维码
随时随地手机跟帖
wangjiahao88|  楼主 | 2017-12-7 13:27 | 显示全部楼层
// Initialize DMXSerial and neo pixel output
void setup () {
  int n;
  DMXSerial.init(DMXProbe);

  // enable pwm outputs
  pinMode(RedPin,   OUTPUT); // sets the digital pin as output
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
  
  DMXSerial.maxChannel(DMXLENGTH); // after 3 * pixel channels, the onUpdate will be called when new data arrived.

  // setup the neopixel output
  setupNeopixel();

  // give them a decent color...
  n = 1;
  for (int p = 0; p < PIXELS; p++) {
    DMXSerial.write(n++, 5);
    DMXSerial.write(n++, 10);
    DMXSerial.write(n++, 20);
  }
  updateNeopixel(DMXSerial.getBuffer() + DMXSTART, PIXELS);

} // setup ()

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:27 | 显示全部楼层
// do constantly fetch DMX data and update the neopixels.
void loop() {
  // wait for an incomming DMX packet.
  if (DMXSerial.receive()) {
    analogWrite(RedPin,   DMXSerial.read(1));
    analogWrite(GreenPin, DMXSerial.read(2));
    analogWrite(BluePin,  DMXSerial.read(3));
    updateNeopixel(DMXSerial.getBuffer() + DMXSTART, PIXELS);

  } else {
    // don't update the Neopixels but signal a red.
    analogWrite(RedPin,   100);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin,  0);
  } // if
  
} // loop()

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:28 | 显示全部楼层
WS2812 的头文件

ws2812.zip

2.63 KB

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:28 | 显示全部楼层
DmxSerialRecv
--------------------------------------------

arduino 的例程 3

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:29 | 显示全部楼层
// - - - - -
// DmxSerial - A hardware supported interface to DMX.
// DmxSerialRecv.ino: Sample DMX application for retrieving 3 DMX values:
// address 1 (red) -> PWM Port 9
// address 2 (green) -> PWM Port 6
// address 3 (blue) -> PWM Port 5
//
// Copyright (c) 2011-2015 by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
//
// Documentation and samples are available at http://www.mathertel.de/Arduino
// 25.07.2011 creation of the DmxSerial library.
// 10.09.2011 fully control the serial hardware register
//            without using the Arduino Serial (HardwareSerial) class to avoid ISR implementation conflicts.
// 01.12.2011 include file and extension changed to work with the Arduino 1.0 environment
// 28.12.2011 changed to channels 1..3 (RGB) for compatibility with the DmxSerialSend sample.
// 10.05.2012 added some lines to loop to show how to fall back to a default color when no data was received since some time.
// - - - - -

#include <DMXSerial.h>

// Constants for demo program

const int RedPin =    9;  // PWM output pin for Red Light.
const int GreenPin =  6;  // PWM output pin for Green Light.
const int BluePin =   5;  // PWM output pin for Blue Light.

#define RedDefaultLevel   100
#define GreenDefaultLevel 200
#define BlueDefaultLevel  255

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:29 | 显示全部楼层
void setup () {
  DMXSerial.init(DMXReceiver);
  
  // set some default values
  DMXSerial.write(1, 80);
  DMXSerial.write(2, 0);
  DMXSerial.write(3, 0);
  
  // enable pwm outputs
  pinMode(RedPin,   OUTPUT); // sets the digital pin as output
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
}

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:29 | 显示全部楼层
void loop() {
  // Calculate how long no data backet was received
  unsigned long lastPacket = DMXSerial.noDataSince();
  
  if (lastPacket < 5000) {
    // read recent DMX values and set pwm levels
    analogWrite(RedPin,   DMXSerial.read(1));
    analogWrite(GreenPin, DMXSerial.read(2));
    analogWrite(BluePin,  DMXSerial.read(3));

  } else {
    // Show pure red color, when no data was received since 5 seconds or more.
    analogWrite(RedPin,   RedDefaultLevel);
    analogWrite(GreenPin, GreenDefaultLevel);
    analogWrite(BluePin,  BlueDefaultLevel);
  } // if
}

// End.

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:33 | 显示全部楼层
#include <DMXSerial.h>

// Constants for demo program

const int RedPin =    9;  // PWM output pin for Red Light.
const int GreenPin =  6;  // PWM output pin for Green Light.
const int BluePin =   5;  // PWM output pin for Blue Light.

// The color fading pattern

int RedList[]   = {255, 128,   0,   0,   0, 128};
int GreenList[] = {  0, 128, 255, 128,   0,   0};
int BlueList[]  = {  0,   0,   0, 128, 255, 128};

int RedLevel, GreenLevel, BlueLevel;

int RedNow = 0;
int GreenNow = 0;
int BlueNow = 0;

int state = 0;

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:34 | 显示全部楼层
void setup() {
  DMXSerial.init(DMXController);

  pinMode(RedPin,   OUTPUT); // sets the digital pin as output
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
} // setup

使用特权

评论回复
wangjiahao88|  楼主 | 2017-12-7 13:34 | 显示全部楼层
// loop through the rainbow colors
void loop() {
  RedLevel = RedList[state];
  GreenLevel = GreenList[state];
  BlueLevel = BlueList[state];
  
  if ((RedLevel == RedNow) && (GreenLevel == GreenNow) && (BlueLevel == BlueNow)) {
    state += 1;
    if (state == 6)
      state = 0;

  } else {
    if (RedNow < RedLevel)  RedNow++;
    if (RedNow > RedLevel)  RedNow--;
    DMXSerial.write(1, RedNow);
    analogWrite(RedPin,   RedNow);

    if (GreenNow < GreenLevel)  GreenNow++;
    if (GreenNow > GreenLevel)  GreenNow--;
    DMXSerial.write(2, GreenNow);
    analogWrite(GreenPin, GreenNow);

    if (BlueNow < BlueLevel)  BlueNow++;
    if (BlueNow > BlueLevel)  BlueNow--;
    DMXSerial.write(3, BlueNow);
    analogWrite(BluePin,  BlueNow);
  } // if

  delayMicroseconds(2000); // wait a little bit
} // loop

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则