ESPDMX.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // - - - - -
  2. // ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port.
  3. // ESPDMX.cpp: Library implementation file
  4. //
  5. // Copyright (C) 2015 Rick <ricardogg95@gmail.com>
  6. // This work is licensed under a GNU style license.
  7. //
  8. // Last change: Marcel Seerig <https://github.com/mseerig>
  9. //
  10. // Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx
  11. // - - - - -
  12. /* ----- LIBRARIES ----- */
  13. #include <Arduino.h>
  14. #include "ESPDMX.h"
  15. #include <HardwareSerial.h>
  16. #define dmxMaxChannel 512
  17. #define defaultMax 32
  18. #define DMXSPEED 250000
  19. #define DMXFORMAT SERIAL_8N2
  20. #define BREAKSPEED 83333
  21. #define BREAKFORMAT SERIAL_8N1
  22. bool dmxStarted = false;
  23. int sendPin = 22; //dafault on ESP32
  24. int receivePin = -1;
  25. //DMX value array and size. Entry 0 will hold startbyte
  26. uint8_t dmxData[dmxMaxChannel] = {};
  27. int chanSize;
  28. HardwareSerial DMXSerial(1);
  29. void DMXESPSerial::init() {
  30. chanSize = defaultMax;
  31. DMXSerial.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin);
  32. pinMode(sendPin, OUTPUT);
  33. dmxStarted = true;
  34. }
  35. // Set up the DMX-Protocol
  36. void DMXESPSerial::init(int chanQuant, int dmxPin) {
  37. sendPin = dmxPin;
  38. if (chanQuant > dmxMaxChannel || chanQuant <= 0) {
  39. chanQuant = defaultMax;
  40. }
  41. chanSize = chanQuant;
  42. DMXSerial.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin);
  43. pinMode(sendPin, OUTPUT);
  44. dmxStarted = true;
  45. }
  46. // Function to read DMX data
  47. uint8_t DMXESPSerial::read(int Channel) {
  48. if (dmxStarted == false) init();
  49. if (Channel < 1) Channel = 1;
  50. if (Channel > dmxMaxChannel) Channel = dmxMaxChannel;
  51. return(dmxData[Channel]);
  52. }
  53. // Function to send DMX data
  54. void DMXESPSerial::write(int Channel, uint8_t value) {
  55. if (dmxStarted == false) init();
  56. if (Channel < 1) Channel = 1;
  57. if (Channel > chanSize) Channel = chanSize;
  58. if (value < 0) value = 0;
  59. if (value > 255) value = 255;
  60. dmxData[Channel] = value;
  61. }
  62. void DMXESPSerial::end() {
  63. delete dmxData;
  64. chanSize = 0;
  65. DMXSerial.end();
  66. dmxStarted == false;
  67. }
  68. void DMXESPSerial::update() {
  69. if (dmxStarted == false) init();
  70. //Send break
  71. digitalWrite(sendPin, HIGH);
  72. DMXSerial.begin(BREAKSPEED, BREAKFORMAT, receivePin, sendPin);
  73. DMXSerial.write(0);
  74. DMXSerial.flush();
  75. delay(1);
  76. DMXSerial.end();
  77. //send data
  78. DMXSerial.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin);
  79. digitalWrite(sendPin, LOW);
  80. DMXSerial.write(dmxData, chanSize);
  81. DMXSerial.flush();
  82. delay(1);
  83. DMXSerial.end();
  84. }
  85. // Function to update the DMX bus