Written by Ayrton Leyssens DMX library, bitbang method for LPC1768
DMX.cpp@0:55edcc97ba17, 2016-03-09 (annotated)
- Committer:
- Ayrton_L
- Date:
- Wed Mar 09 03:58:29 2016 +0000
- Revision:
- 0:55edcc97ba17
- Child:
- 1:4eb21fcb621a
DMX LIbrary LPC1768 BitBang Method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ayrton_L | 0:55edcc97ba17 | 1 | #include "DMX.h" |
Ayrton_L | 0:55edcc97ba17 | 2 | |
Ayrton_L | 0:55edcc97ba17 | 3 | DMX::DMX(PinName pin) : _pin(pin) |
Ayrton_L | 0:55edcc97ba17 | 4 | { |
Ayrton_L | 0:55edcc97ba17 | 5 | this->I32_m_BreakTime = 88; |
Ayrton_L | 0:55edcc97ba17 | 6 | this->I32_m_MABTime = 8; |
Ayrton_L | 0:55edcc97ba17 | 7 | this->I32_m_StartCodeTime = 44; |
Ayrton_L | 0:55edcc97ba17 | 8 | this->I32_m_BitTime = 4; |
Ayrton_L | 0:55edcc97ba17 | 9 | this->I32_m_ChannelCounter = 0; |
Ayrton_L | 0:55edcc97ba17 | 10 | this->I32_m_BitCounter = 0; |
Ayrton_L | 0:55edcc97ba17 | 11 | uint32_t i; |
Ayrton_L | 0:55edcc97ba17 | 12 | for(i = 0; i < 512; i++) |
Ayrton_L | 0:55edcc97ba17 | 13 | { |
Ayrton_L | 0:55edcc97ba17 | 14 | this->I8_m_Data[i] = 0; |
Ayrton_L | 0:55edcc97ba17 | 15 | } |
Ayrton_L | 0:55edcc97ba17 | 16 | this->_pin = 0; |
Ayrton_L | 0:55edcc97ba17 | 17 | return; |
Ayrton_L | 0:55edcc97ba17 | 18 | } |
Ayrton_L | 0:55edcc97ba17 | 19 | |
Ayrton_L | 0:55edcc97ba17 | 20 | DMX::~DMX() |
Ayrton_L | 0:55edcc97ba17 | 21 | { |
Ayrton_L | 0:55edcc97ba17 | 22 | delete[] &I32_m_BreakTime; |
Ayrton_L | 0:55edcc97ba17 | 23 | delete[] &I32_m_MABTime; |
Ayrton_L | 0:55edcc97ba17 | 24 | delete[] &I32_m_StartCodeTime; |
Ayrton_L | 0:55edcc97ba17 | 25 | delete[] &I32_m_BitTime; |
Ayrton_L | 0:55edcc97ba17 | 26 | delete[] &_pin; |
Ayrton_L | 0:55edcc97ba17 | 27 | delete[] &I8_m_Data; |
Ayrton_L | 0:55edcc97ba17 | 28 | return; |
Ayrton_L | 0:55edcc97ba17 | 29 | } |
Ayrton_L | 0:55edcc97ba17 | 30 | |
Ayrton_L | 0:55edcc97ba17 | 31 | void DMX::V_SendDMX() |
Ayrton_L | 0:55edcc97ba17 | 32 | { |
Ayrton_L | 0:55edcc97ba17 | 33 | _pin = 0; |
Ayrton_L | 0:55edcc97ba17 | 34 | t.attach_us(this, &DMX::V_SendMAB, 88); |
Ayrton_L | 0:55edcc97ba17 | 35 | } |
Ayrton_L | 0:55edcc97ba17 | 36 | |
Ayrton_L | 0:55edcc97ba17 | 37 | void DMX::V_SendMAB() |
Ayrton_L | 0:55edcc97ba17 | 38 | { |
Ayrton_L | 0:55edcc97ba17 | 39 | _pin = 1; |
Ayrton_L | 0:55edcc97ba17 | 40 | t.detach(); |
Ayrton_L | 0:55edcc97ba17 | 41 | t.attach_us(this, &DMX::V_SendSC, 8); |
Ayrton_L | 0:55edcc97ba17 | 42 | } |
Ayrton_L | 0:55edcc97ba17 | 43 | |
Ayrton_L | 0:55edcc97ba17 | 44 | void DMX::V_SendSC() |
Ayrton_L | 0:55edcc97ba17 | 45 | { |
Ayrton_L | 0:55edcc97ba17 | 46 | _pin = 0; |
Ayrton_L | 0:55edcc97ba17 | 47 | t.detach(); |
Ayrton_L | 0:55edcc97ba17 | 48 | t.attach_us(this, &DMX::V_SendData, 44); |
Ayrton_L | 0:55edcc97ba17 | 49 | } |
Ayrton_L | 0:55edcc97ba17 | 50 | |
Ayrton_L | 0:55edcc97ba17 | 51 | void DMX::V_SendData() |
Ayrton_L | 0:55edcc97ba17 | 52 | { |
Ayrton_L | 0:55edcc97ba17 | 53 | if(I32_m_BitCounter == 0) |
Ayrton_L | 0:55edcc97ba17 | 54 | { |
Ayrton_L | 0:55edcc97ba17 | 55 | _pin = 0; |
Ayrton_L | 0:55edcc97ba17 | 56 | I32_m_BitCounter++; |
Ayrton_L | 0:55edcc97ba17 | 57 | t.detach(); |
Ayrton_L | 0:55edcc97ba17 | 58 | t.attach_us(this, &DMX::V_SendData, 4); |
Ayrton_L | 0:55edcc97ba17 | 59 | } |
Ayrton_L | 0:55edcc97ba17 | 60 | else if(I32_m_BitCounter == 9 ) |
Ayrton_L | 0:55edcc97ba17 | 61 | { |
Ayrton_L | 0:55edcc97ba17 | 62 | _pin = 1; |
Ayrton_L | 0:55edcc97ba17 | 63 | I32_m_BitCounter = 0; |
Ayrton_L | 0:55edcc97ba17 | 64 | I32_m_ChannelCounter++; |
Ayrton_L | 0:55edcc97ba17 | 65 | t.detach(); |
Ayrton_L | 0:55edcc97ba17 | 66 | t.attach_us(this, &DMX::V_SendData, 8); |
Ayrton_L | 0:55edcc97ba17 | 67 | } |
Ayrton_L | 0:55edcc97ba17 | 68 | else |
Ayrton_L | 0:55edcc97ba17 | 69 | { |
Ayrton_L | 0:55edcc97ba17 | 70 | if(I32_m_ChannelCounter < 511) |
Ayrton_L | 0:55edcc97ba17 | 71 | { |
Ayrton_L | 0:55edcc97ba17 | 72 | if(I8_m_Data[I32_m_ChannelCounter] & 0x01 == 0x01) |
Ayrton_L | 0:55edcc97ba17 | 73 | { |
Ayrton_L | 0:55edcc97ba17 | 74 | _pin = 1; |
Ayrton_L | 0:55edcc97ba17 | 75 | } |
Ayrton_L | 0:55edcc97ba17 | 76 | else if(I8_m_Data[I32_m_ChannelCounter] & 0x01 == 0x00) |
Ayrton_L | 0:55edcc97ba17 | 77 | { |
Ayrton_L | 0:55edcc97ba17 | 78 | _pin =0; |
Ayrton_L | 0:55edcc97ba17 | 79 | } |
Ayrton_L | 0:55edcc97ba17 | 80 | I8_m_Data[I32_m_ChannelCounter] = I8_m_Data[I32_m_ChannelCounter] >> 1; |
Ayrton_L | 0:55edcc97ba17 | 81 | } |
Ayrton_L | 0:55edcc97ba17 | 82 | else |
Ayrton_L | 0:55edcc97ba17 | 83 | { |
Ayrton_L | 0:55edcc97ba17 | 84 | I32_m_ChannelCounter = 0; |
Ayrton_L | 0:55edcc97ba17 | 85 | I32_m_BitCounter = 0; |
Ayrton_L | 0:55edcc97ba17 | 86 | DMX::V_SendDMX(); |
Ayrton_L | 0:55edcc97ba17 | 87 | } |
Ayrton_L | 0:55edcc97ba17 | 88 | I32_m_BitCounter++; |
Ayrton_L | 0:55edcc97ba17 | 89 | t.detach(); |
Ayrton_L | 0:55edcc97ba17 | 90 | t.attach_us(this, &DMX::V_SendData, 4); |
Ayrton_L | 0:55edcc97ba17 | 91 | } |
Ayrton_L | 0:55edcc97ba17 | 92 | } |
Ayrton_L | 0:55edcc97ba17 | 93 | |
Ayrton_L | 0:55edcc97ba17 | 94 | void DMX::V_SetBreak(uint32_t I32_BreakTime) |
Ayrton_L | 0:55edcc97ba17 | 95 | { |
Ayrton_L | 0:55edcc97ba17 | 96 | I32_m_BreakTime = I32_BreakTime; |
Ayrton_L | 0:55edcc97ba17 | 97 | } |
Ayrton_L | 0:55edcc97ba17 | 98 | |
Ayrton_L | 0:55edcc97ba17 | 99 | void DMX::V_SetMAB(uint32_t I32_MABTime) |
Ayrton_L | 0:55edcc97ba17 | 100 | { |
Ayrton_L | 0:55edcc97ba17 | 101 | I32_m_MABTime = I32_MABTime; |
Ayrton_L | 0:55edcc97ba17 | 102 | } |
Ayrton_L | 0:55edcc97ba17 | 103 | |
Ayrton_L | 0:55edcc97ba17 | 104 | void DMX::V_SetSC(uint32_t I32_StartCodeTime) |
Ayrton_L | 0:55edcc97ba17 | 105 | { |
Ayrton_L | 0:55edcc97ba17 | 106 | I32_m_StartCodeTime = I32_StartCodeTime; |
Ayrton_L | 0:55edcc97ba17 | 107 | } |
Ayrton_L | 0:55edcc97ba17 | 108 | |
Ayrton_L | 0:55edcc97ba17 | 109 | void DMX::V_SetBitTime(uint32_t I32_BitTime) |
Ayrton_L | 0:55edcc97ba17 | 110 | { |
Ayrton_L | 0:55edcc97ba17 | 111 | I32_m_BitTime = I32_BitTime; |
Ayrton_L | 0:55edcc97ba17 | 112 | } |
Ayrton_L | 0:55edcc97ba17 | 113 | |
Ayrton_L | 0:55edcc97ba17 | 114 | void DMX::V_SetData(uint8_t I8_Data[512]) |
Ayrton_L | 0:55edcc97ba17 | 115 | { |
Ayrton_L | 0:55edcc97ba17 | 116 | uint32_t i; |
Ayrton_L | 0:55edcc97ba17 | 117 | for(i = 0; i < 512; i++) |
Ayrton_L | 0:55edcc97ba17 | 118 | { |
Ayrton_L | 0:55edcc97ba17 | 119 | I8_m_Data[i] = I8_Data[i]; |
Ayrton_L | 0:55edcc97ba17 | 120 | } |
Ayrton_L | 0:55edcc97ba17 | 121 | } |