1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "SPI_DMX.h"
-
- #define SPI_FREQHz (8.0*8.0e5)
- #define SPI_BYTEus ((8.0e6/SPI_FREQHz)*1.6) //weird translation factor idk
- #define DMX_BITus 4
- #define BREAKus (25*DMX_BITus)
- #define MABus (3*DMX_BITus)
- #define STARTus (DMX_BITus)
- #define STOPus (2*DMX_BITus)
-
- #define BY_HIGH 0x00
- #define BY_LOW 0xFF
-
-
-
- SPI_DMX::SPI_DMX(unsigned char serial_port)
- {
- this->serial_port = serial_port;
- for(int i = 0; i < BREAKus/SPI_BYTEus; i++)
- {
- this->message[i] = BY_LOW;
- }
- for(int i = BREAKus/SPI_BYTEus; i < (BREAKus+MABus)/SPI_BYTEus; i++)
- {
- this->message[i] = BY_HIGH;
- }
- wiringPiSPISetup(serial_port,SPI_FREQHz);
- printf("%f\n",SPI_FREQHz);
- }
-
- SPI_DMX::~SPI_DMX()
- {
-
- }
-
- void SPI_DMX::TX_Bytes(unsigned char* tx_buffer, unsigned char tx_size)
- {
- //unsigned int SPIbits = (BREAKus+MABus+(size*(STARTus+8*DMX_BITus+STOPus)))/SPI_BITus;
- //unsigned char message[SPIbits];
-
- int messind = (BREAKus+MABus)/SPI_BYTEus;
- printf("%i\n",messind);
-
- for(int b = 0; b < tx_size; b++)
- {
- for(int i = 0; i < STARTus/SPI_BYTEus; i++)
- {
- message[messind++] = BY_LOW;
- }
- for(int i = 0; i < 8; i++)
- {
- unsigned char txbit = tx_buffer[b];
- txbit = ~((((txbit >> i) & 0x01) ^ 0x01) * 0xFF); //NEEDS TO BE ADJUSTED IF LOGIC LEVEL IS REVERSED
- for(int i = 0; i < DMX_BITus/SPI_BYTEus; i++)
- {
- message[messind++] = txbit;
- }
- }
- for(int i = 0; i < STOPus/SPI_BYTEus; i++)
- {
- message[messind++] = BY_HIGH;
- }
- }
- printf("%i\n",messind);
- /*messind = 0;
- for(int i = 0; i < 8; i++)
- {
- unsigned char txbit = tx_buffer[0];
- txbit = ((txbit >> i) & 0x01) * 0xFF;
- for(int i = 0; i < DMX_BITus/SPI_BYTEus; i++)
- {
- message[messind++] = txbit;
- }
- printf("%u %u %f %i \n",tx_buffer[0],txbit,DMX_BITus/SPI_BYTEus,messind);
- } */
- wiringPiSPIDataRW(this->serial_port, message, messind-1);
- }
|