You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SPI_DMX.cpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "SPI_DMX.h"
  2. #define SPI_FREQHz (8.0*8.0e5)
  3. #define SPI_BYTEus ((8.0e6/SPI_FREQHz)*1.6) //weird translation factor idk
  4. #define DMX_BITus 4
  5. #define BREAKus (25*DMX_BITus)
  6. #define MABus (3*DMX_BITus)
  7. #define STARTus (DMX_BITus)
  8. #define STOPus (2*DMX_BITus)
  9. #define BY_HIGH 0x00
  10. #define BY_LOW 0xFF
  11. SPI_DMX::SPI_DMX(unsigned char serial_port)
  12. {
  13. this->serial_port = serial_port;
  14. for(int i = 0; i < BREAKus/SPI_BYTEus; i++)
  15. {
  16. this->message[i] = BY_LOW;
  17. }
  18. for(int i = BREAKus/SPI_BYTEus; i < (BREAKus+MABus)/SPI_BYTEus; i++)
  19. {
  20. this->message[i] = BY_HIGH;
  21. }
  22. wiringPiSPISetup(serial_port,SPI_FREQHz);
  23. printf("%f\n",SPI_FREQHz);
  24. }
  25. SPI_DMX::~SPI_DMX()
  26. {
  27. }
  28. void SPI_DMX::TX_Bytes(unsigned char* tx_buffer, unsigned char tx_size)
  29. {
  30. //unsigned int SPIbits = (BREAKus+MABus+(size*(STARTus+8*DMX_BITus+STOPus)))/SPI_BITus;
  31. //unsigned char message[SPIbits];
  32. int messind = (BREAKus+MABus)/SPI_BYTEus;
  33. printf("%i\n",messind);
  34. for(int b = 0; b < tx_size; b++)
  35. {
  36. for(int i = 0; i < STARTus/SPI_BYTEus; i++)
  37. {
  38. message[messind++] = BY_LOW;
  39. }
  40. for(int i = 0; i < 8; i++)
  41. {
  42. unsigned char txbit = tx_buffer[b];
  43. txbit = ~((((txbit >> i) & 0x01) ^ 0x01) * 0xFF); //NEEDS TO BE ADJUSTED IF LOGIC LEVEL IS REVERSED
  44. for(int i = 0; i < DMX_BITus/SPI_BYTEus; i++)
  45. {
  46. message[messind++] = txbit;
  47. }
  48. }
  49. for(int i = 0; i < STOPus/SPI_BYTEus; i++)
  50. {
  51. message[messind++] = BY_HIGH;
  52. }
  53. }
  54. printf("%i\n",messind);
  55. /*messind = 0;
  56. for(int i = 0; i < 8; i++)
  57. {
  58. unsigned char txbit = tx_buffer[0];
  59. txbit = ((txbit >> i) & 0x01) * 0xFF;
  60. for(int i = 0; i < DMX_BITus/SPI_BYTEus; i++)
  61. {
  62. message[messind++] = txbit;
  63. }
  64. printf("%u %u %f %i \n",tx_buffer[0],txbit,DMX_BITus/SPI_BYTEus,messind);
  65. } */
  66. wiringPiSPIDataRW(this->serial_port, message, messind-1);
  67. }