|
| 1 | +// SPDX-FileCopyrightText: 2013 W.A. van der Meeren <danny@illogic.nl> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | +/* |
| 5 | + Conceptinetics.h - DMX library for Arduino |
| 6 | + Copyright (c) 2013 W.A. van der Meeren <danny@illogic.nl>. All right reserved. |
| 7 | +
|
| 8 | + This library is free software; you can redistribute it and/or |
| 9 | + modify it under the terms of the GNU Lesser General Public |
| 10 | + License as published by the Free Software Foundation; either |
| 11 | + version 3 of the License, or (at your option) any later version. |
| 12 | +
|
| 13 | + This library is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + Lesser General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU Lesser General Public |
| 19 | + License along with this library; if not, write to the Free Software |
| 20 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | +*/ |
| 22 | + |
| 23 | +/* |
| 24 | + This code has been tested using the following hardware: |
| 25 | +
|
| 26 | + - Arduino / Genuino UNO R3 using a CTC-DRA-13-1 ISOLATED DMX-RDM SHIELD |
| 27 | + - Arduino / Genuino MEGA2560 R3 using a CTC-DRA-13-1 ISOLATED DMX-RDM SHIELD |
| 28 | + - Arduino / Genuino Leonardo using a CTC-DRA-13-R2 ISOLATED DMX-RDM SHIELD |
| 29 | +
|
| 30 | + - CTC-DRA-10-1 and CTC-DRA-10-R2 is the Non-isolated costs effective DMX-RDM shield |
| 31 | +*/ |
| 32 | + |
| 33 | + |
| 34 | +#ifndef CONCEPTINETICS_H_ |
| 35 | +#define CONCEPTINETICS_H_ |
| 36 | + |
| 37 | +#include <Arduino.h> |
| 38 | +#include <inttypes.h> |
| 39 | + |
| 40 | +#include "Rdm_Uid.h" |
| 41 | +#include "Rdm_Defines.h" |
| 42 | + |
| 43 | +#define DMX_MAX_FRAMESIZE 513 // Startbyte + 512 Slots |
| 44 | +#define DMX_MIN_FRAMESIZE 2 // Startbyte + 1 Slot |
| 45 | + |
| 46 | +#define DMX_MAX_FRAMECHANNELS 512 // Maxmim number of channer per frame |
| 47 | + |
| 48 | +#define DMX_STARTCODE_SIZE 1 // Size of startcode in bytes |
| 49 | + |
| 50 | +#define DMX_START_CODE 0x0 // Start code for a DMX frame |
| 51 | +#define RDM_START_CODE 0xcc // Start code for a RDM frame |
| 52 | + |
| 53 | +// Uncomment to enable Inter slot delay ) (avg < 76uSec) ... |
| 54 | +// mimum is zero according to specification |
| 55 | +// #define DMX_IBG 10 // Inter slot time |
| 56 | + |
| 57 | +// Speed your Arduino is running on in Hz. |
| 58 | +#define F_OSC 16000000UL |
| 59 | + |
| 60 | +// DMX Baudrate, this should be 250000 |
| 61 | +#define DMX_BAUD_RATE 250000 |
| 62 | + |
| 63 | +// The baudrate used to automaticly generate a break within |
| 64 | +// your ISR.. make it lower to generate longer breaks |
| 65 | +//#define DMX_BREAK_RATE 99900 |
| 66 | + |
| 67 | +// 2017, Feb 28: Set to appox 176us |
| 68 | +#define DMX_BREAK_RATE 49950 |
| 69 | + |
| 70 | +// Tabel 3-2 ANSI_E1-20-2010 |
| 71 | +// Minimum time to allow the datalink to 'turn arround' |
| 72 | +#define MIN_RESPONDER_PACKET_SPACING_USEC 176 /*176*/ |
| 73 | + |
| 74 | +// Define which serial port to use as DMX port, only one can be |
| 75 | +// selected at the time by uncommenting one of the following |
| 76 | +// lines |
| 77 | +#define USE_DMX_SERIAL_0 |
| 78 | +//#define USE_DMX_SERIAL_1 |
| 79 | +//#define USE_DMX_SERIAL_2 |
| 80 | +//#define USE_DMX_SERIAL_3 |
| 81 | + |
| 82 | +namespace dmx |
| 83 | +{ |
| 84 | + enum dmxState |
| 85 | + { |
| 86 | + dmxUnknown, |
| 87 | + dmxStartByte, |
| 88 | + dmxWaitStartAddress, |
| 89 | + dmxData, |
| 90 | + dmxFrameReady, |
| 91 | + }; |
| 92 | +}; |
| 93 | + |
| 94 | +namespace rdm |
| 95 | +{ |
| 96 | + enum rdmState |
| 97 | + { |
| 98 | + rdmUnknown, |
| 99 | + rdmStartByte, |
| 100 | + rdmSubStartCode, |
| 101 | + rdmMessageLength, |
| 102 | + rdmData, |
| 103 | + rdmChecksumHigh, |
| 104 | + rdmChecksumLow, |
| 105 | + rdmFrameReady, |
| 106 | + }; |
| 107 | +}; |
| 108 | + |
| 109 | +struct IFrameBuffer |
| 110 | +{ |
| 111 | + virtual uint16_t getBufferSize ( void ) = 0; |
| 112 | + |
| 113 | + virtual uint8_t getSlotValue ( uint16_t index ) = 0; |
| 114 | + virtual void setSlotValue ( uint16_t index, uint8_t value ) = 0; |
| 115 | +}; |
| 116 | + |
| 117 | +class DMX_FrameBuffer : IFrameBuffer |
| 118 | +{ |
| 119 | + public: |
| 120 | + // |
| 121 | + // Constructor buffersize = 1-513 |
| 122 | + // |
| 123 | + DMX_FrameBuffer ( uint16_t buffer_size ); |
| 124 | + DMX_FrameBuffer ( DMX_FrameBuffer &buffer ); |
| 125 | + ~DMX_FrameBuffer ( void ); |
| 126 | + |
| 127 | + uint16_t getBufferSize ( void ); |
| 128 | + |
| 129 | + uint8_t getSlotValue ( uint16_t index ); |
| 130 | + void setSlotValue ( uint16_t index, uint8_t value ); |
| 131 | + void setSlotRange ( uint16_t start, uint16_t end, uint8_t value ); |
| 132 | + void clear ( void ); |
| 133 | + |
| 134 | + uint8_t &operator[] ( uint16_t index ); |
| 135 | + |
| 136 | + private: |
| 137 | + |
| 138 | + uint8_t *m_refcount; |
| 139 | + uint16_t m_bufferSize; |
| 140 | + uint8_t *m_buffer; |
| 141 | +}; |
| 142 | + |
| 143 | + |
| 144 | +// |
| 145 | +// DMX Master controller |
| 146 | +// |
| 147 | +class DMX_Master |
| 148 | +{ |
| 149 | + public: |
| 150 | + // Run the DMX master from a pre allocated frame buffer which |
| 151 | + // you have fully under your own control |
| 152 | + DMX_Master ( DMX_FrameBuffer &buffer, int readEnablePin ); |
| 153 | + |
| 154 | + // Run the DMX master by giving a predefined maximum number of |
| 155 | + // channels to support |
| 156 | + DMX_Master ( uint16_t maxChannel, int readEnablePin ); |
| 157 | + |
| 158 | + ~DMX_Master ( void ); |
| 159 | + |
| 160 | + void enable ( void ); // Start transmitting |
| 161 | + void disable ( void ); // Stop transmitting |
| 162 | + |
| 163 | + // Get reference to the internal framebuffer |
| 164 | + DMX_FrameBuffer &getBuffer ( void ); |
| 165 | + |
| 166 | + // Update channel values |
| 167 | + void setChannelValue ( uint16_t channel, uint8_t value ); |
| 168 | + void setChannelRange ( uint16_t start, uint16_t end, uint8_t value ); |
| 169 | + |
| 170 | + public: |
| 171 | + // |
| 172 | + // Manual control over the break period |
| 173 | + // |
| 174 | + void setAutoBreakMode ( void ); // Generated from ISR |
| 175 | + void setManualBreakMode ( void ); // Generate manually |
| 176 | + |
| 177 | + uint8_t autoBreakEnabled ( void ); |
| 178 | + |
| 179 | + // We are waiting for a manual break to be generated |
| 180 | + uint8_t waitingBreak ( void ); |
| 181 | + |
| 182 | + // Generate break and start transmission of frame |
| 183 | + void breakAndContinue ( uint8_t breakLength_us = 100 ); |
| 184 | + |
| 185 | + |
| 186 | + protected: |
| 187 | + void setStartCode ( uint8_t value ); |
| 188 | + |
| 189 | + |
| 190 | + private: |
| 191 | + DMX_FrameBuffer m_frameBuffer; |
| 192 | + uint8_t m_autoBreak; |
| 193 | +}; |
| 194 | + |
| 195 | + |
| 196 | +// |
| 197 | +// DMX Slave controller |
| 198 | +// |
| 199 | +class DMX_Slave : public DMX_FrameBuffer |
| 200 | +{ |
| 201 | + public: |
| 202 | + DMX_Slave ( DMX_FrameBuffer &buffer, int readEnablePin = -1 ); |
| 203 | + |
| 204 | + // nrChannels is the consecutive DMX512 slots required |
| 205 | + // to operate this slave device |
| 206 | + DMX_Slave ( uint16_t nrChannels, int readEnablePin = -1 ); |
| 207 | + |
| 208 | + ~DMX_Slave ( void ); |
| 209 | + |
| 210 | + void enable ( void ); // Enable receiver |
| 211 | + void disable ( void ); // Disable receiver |
| 212 | + |
| 213 | + |
| 214 | + // Get reference to the internal framebuffer |
| 215 | + DMX_FrameBuffer &getBuffer ( void ); |
| 216 | + |
| 217 | + uint8_t getChannelValue ( uint16_t channel ); |
| 218 | + |
| 219 | + uint16_t getStartAddress ( void ); |
| 220 | + void setStartAddress ( uint16_t ); |
| 221 | + |
| 222 | + |
| 223 | + // Process incoming byte from USART |
| 224 | + bool processIncoming ( uint8_t val, bool first = false ); |
| 225 | + |
| 226 | + // Register on receive complete callback in case |
| 227 | + // of time critical applications |
| 228 | + void onReceiveComplete ( void (*func)(unsigned short) ); |
| 229 | + |
| 230 | + protected: |
| 231 | + |
| 232 | + |
| 233 | + private: |
| 234 | + uint16_t m_startAddress; // Slave start address |
| 235 | + dmx::dmxState m_state; |
| 236 | + |
| 237 | + static void (*event_onFrameReceived)(unsigned short channelsReceived); |
| 238 | +}; |
| 239 | + |
| 240 | + |
| 241 | +class RDM_FrameBuffer : public IFrameBuffer |
| 242 | +{ |
| 243 | + public: |
| 244 | + // |
| 245 | + // Constructor |
| 246 | + // |
| 247 | + RDM_FrameBuffer ( void ) {}; |
| 248 | + ~RDM_FrameBuffer ( void ) {}; |
| 249 | + |
| 250 | + uint16_t getBufferSize ( void ); |
| 251 | + |
| 252 | + uint8_t getSlotValue ( uint16_t index ); |
| 253 | + void setSlotValue ( uint16_t index, uint8_t value ); |
| 254 | + void clear ( void ); |
| 255 | + |
| 256 | + uint8_t &operator[] ( uint16_t index ); |
| 257 | + |
| 258 | + public: // functions to provide access from USART |
| 259 | + // Process incoming byte from USART, |
| 260 | + // returns false when no more data is accepted |
| 261 | + bool processIncoming ( uint8_t val, bool first = false ); |
| 262 | + |
| 263 | + // Process outgoing byte to USART |
| 264 | + // returns false when no more data is available |
| 265 | + bool fetchOutgoing ( volatile uint8_t *udr, bool first = false ); |
| 266 | + |
| 267 | + protected: |
| 268 | + // Process received frame |
| 269 | + virtual void processFrame ( void ) = 0; |
| 270 | + |
| 271 | + //private: |
| 272 | + protected: |
| 273 | + rdm::rdmState m_state; // State for pushing the message in |
| 274 | + RDM_Message m_msg; |
| 275 | + RDM_Checksum m_csRecv; // Checksum received in rdm message |
| 276 | +}; |
| 277 | + |
| 278 | +// |
| 279 | +// RDM_Responder |
| 280 | +// |
| 281 | +class RDM_Responder : public RDM_FrameBuffer |
| 282 | +{ |
| 283 | + public: |
| 284 | + // |
| 285 | + // m = manufacturer id (16bits) |
| 286 | + // d1-d4 = device id (32bits) |
| 287 | + // |
| 288 | + RDM_Responder ( uint16_t m, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, DMX_Slave &slave); |
| 289 | + ~RDM_Responder ( void ); |
| 290 | + |
| 291 | + void setDeviceInfo |
| 292 | + ( |
| 293 | + uint16_t deviceModelId, |
| 294 | + rdm::RdmProductCategory productCategory, |
| 295 | + uint8_t personalities = 1, |
| 296 | + uint8_t personality = 1 |
| 297 | + ) |
| 298 | + { |
| 299 | + m_DeviceModelId = deviceModelId; |
| 300 | + m_ProductCategory = productCategory; |
| 301 | + m_Personalities = personalities; |
| 302 | + m_Personality = personality; |
| 303 | + }; |
| 304 | + |
| 305 | + // |
| 306 | + // Set vendor software version id |
| 307 | + // |
| 308 | + // v1 = MOST SIGNIFICANT |
| 309 | + // v2... |
| 310 | + // v3... |
| 311 | + // v4 = LEAST SIGNIFICANT |
| 312 | + // |
| 313 | + void setSoftwareVersionId ( uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4 ) |
| 314 | + { |
| 315 | + m_SoftwareVersionId[0] = v1; |
| 316 | + m_SoftwareVersionId[1] = v2; |
| 317 | + m_SoftwareVersionId[2] = v3; |
| 318 | + m_SoftwareVersionId[3] = v4; |
| 319 | + } |
| 320 | + |
| 321 | + // Currently no sensors and subdevices supported |
| 322 | + // void AddSensor ( void ); |
| 323 | + // void AddSubDevice ( void ); |
| 324 | + |
| 325 | + uint8_t getPersonality ( void ) { return m_Personality; }; |
| 326 | + void setPersonality ( uint8_t personality ) { m_Personality = personality; }; |
| 327 | + |
| 328 | + // Register on identify device event handler |
| 329 | + void onIdentifyDevice ( void (*func)(bool) ); |
| 330 | + void onDeviceLabelChanged ( void (*func) (const char*, uint8_t) ); |
| 331 | + void onDMXStartAddressChanged ( void (*func) (uint16_t) ); |
| 332 | + void onDMXPersonalityChanged ( void (*func) (uint8_t) ); |
| 333 | + |
| 334 | + |
| 335 | + // Set the device label |
| 336 | + void setDeviceLabel ( const char *label, size_t len ); |
| 337 | + |
| 338 | + // Enable, Disable rdm responder |
| 339 | + void enable ( void ) { m_rdmStatus.enabled = true; m_rdmStatus.mute = false; }; |
| 340 | + void disable ( void ) { m_rdmStatus.enabled = false; }; |
| 341 | + |
| 342 | + union |
| 343 | + { |
| 344 | + uint8_t raw; |
| 345 | + struct |
| 346 | + { |
| 347 | + uint8_t mute:1; |
| 348 | + uint8_t ident:1; |
| 349 | + uint8_t enabled:1; // Rdm responder enable/disable |
| 350 | + }; |
| 351 | + } m_rdmStatus; |
| 352 | + |
| 353 | + |
| 354 | + protected: |
| 355 | + virtual void processFrame ( void ); |
| 356 | + |
| 357 | + // Discovery to unque brach packets only requires |
| 358 | + // the data part of the packet to be transmitted |
| 359 | + // without breaks or header |
| 360 | + void repondDiscUniqueBranch ( void ); |
| 361 | + |
| 362 | + // Helpers for generating response packets which |
| 363 | + // have larger datafields |
| 364 | + void populateDeviceInfo ( void ); |
| 365 | + |
| 366 | + private: |
| 367 | + RDM_Uid m_devid; // Holds our unique device ID |
| 368 | + uint8_t m_Personalities; // The total number of supported personalities |
| 369 | + uint8_t m_Personality; // The currently active personality |
| 370 | + uint16_t m_DeviceModelId; |
| 371 | + uint8_t m_SoftwareVersionId[4]; // 32 bit Software version |
| 372 | + rdm::RdmProductCategory m_ProductCategory; |
| 373 | + |
| 374 | + char m_deviceLabel[32]; // Device label |
| 375 | + |
| 376 | + static void (*event_onIdentifyDevice)(bool); |
| 377 | + static void (*event_onDeviceLabelChanged)(const char*, uint8_t); |
| 378 | + static void (*event_onDMXStartAddressChanged)(uint16_t); |
| 379 | + static void (*event_onDMXPersonalityChanged)(uint8_t); |
| 380 | +}; |
| 381 | + |
| 382 | + |
| 383 | +#endif /* CONCEPTINETICS_H_ */ |
0 commit comments