portail.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #ifdef __arm__
  6. #include <wiringPi.h>
  7. #else
  8. #define LOW 0
  9. #define HIGH 1
  10. #define INPUT 0
  11. #define OUTPUT 1
  12. int wiringPiSetup(void) {return 0;}
  13. void pinMode (int a, int b) {a=b;}
  14. void delayMicroseconds(unsigned int a) {a=a;}
  15. void digitalWrite(int a, int b) {a=b;}
  16. int digitalRead(int a) {return a;}
  17. #endif
  18. #include "home_easy.h"
  19. #include "buffer.h"
  20. #include "utils.h"
  21. unsigned int portail_timings[3][2] = {
  22. {1140, 380}, // bit 0
  23. {380, 1140}, // bit 1
  24. {380, 790}, // start of data
  25. };
  26. extern unsigned char homeEasyPinOut;
  27. extern unsigned char homeEasyPinIn;
  28. unsigned char frame[5];
  29. unsigned int size = 0;
  30. /**
  31. * Create a complete command according to Chacon protocole
  32. *
  33. * @param id command id (refer to your remote)
  34. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  35. * @param nb button number(1, 2, 3, 4)
  36. * @param on boolean for on/off
  37. *
  38. * @return HomeEasy frame
  39. */
  40. void createPortailCommand(unsigned long int id, char section)
  41. {
  42. int btn = 0;
  43. if(section == 'B'){
  44. btn = 0x7B;
  45. }else if(section == 'A'){
  46. btn = 0x7D;
  47. }else if(section == 'C'){
  48. btn = 0x79;
  49. }else if(section == 'D'){
  50. btn = 0x7F;
  51. }
  52. printf(" section :%c btn : %x id: %x \n",section,btn,id);
  53. frame[0] = id;
  54. frame[1]=0xF7;
  55. frame[2]=btn;
  56. frame[3]=0xA1;
  57. frame[4]=0x00;
  58. size = 36;
  59. return;
  60. }
  61. /**
  62. * Send n times a data frame
  63. *
  64. * @param frame the data to send
  65. * @param repeat number of repeatition
  66. */
  67. void sendPortailFrame(unsigned int repeat)
  68. {
  69. unsigned int i;
  70. // switch to real time
  71. cpuMode(REAL_TIME);
  72. // repeat the command
  73. for(i=0; i<repeat; i++) {
  74. sendPortailBit(START_OF_DATA);
  75. sendPortailBytes();
  76. sendPortailBit(BIT0);
  77. delayMicroseconds(53000);
  78. }
  79. digitalWrite(homeEasyPinOut, LOW);
  80. // Exit real time mode
  81. cpuMode(STANDARD);
  82. }
  83. /**
  84. * Send a complete command according to Chacon protocole
  85. *
  86. * @param id command id (refer to your remote)
  87. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  88. * @param nb button number(1, 2, 3, 4)
  89. * @param on boolean for on/off
  90. * @param repeat number of repeatition
  91. */
  92. void sendPortailCommand(unsigned int id, char button, unsigned char repeat)
  93. {
  94. unsigned long int command;
  95. // build the command
  96. createPortailCommand(id, button);
  97. // send data
  98. sendPortailFrame(repeat);
  99. // release the memory
  100. }
  101. /**
  102. * Send a bit to the RF transmitter
  103. *
  104. * @param bit the bit to transmit (0 | 1 | TRIGGER0 | TRIGGER1 | END_OF_FRAME)
  105. */
  106. void sendPortailBit(unsigned char bit)
  107. {
  108. digitalWrite(homeEasyPinOut, LOW);
  109. delayMicroseconds(portail_timings[bit][0]);
  110. digitalWrite(homeEasyPinOut, HIGH);
  111. delayMicroseconds(portail_timings[bit][1]);
  112. digitalWrite(homeEasyPinOut, LOW);
  113. }
  114. /**
  115. * Send a byte to the RF transmitter
  116. *
  117. * @param byte the byte to transmit
  118. */
  119. void sendPortailByte(unsigned char byte, unsigned int bits)
  120. {
  121. int i;
  122. for(i=0; i < bits; i++) {
  123. sendPortailBit((byte & (0x01 << i)) == (0x01 << i));
  124. }
  125. }
  126. /**
  127. * Send the content of a buffer to the RF transmitter
  128. *
  129. * @param buffer the buffer to transmit
  130. */
  131. void sendPortailBytes(void)
  132. {
  133. int i;
  134. for(i = 0; i < size; i+=8 ) {
  135. register unsigned int bits = (size - i >= 8) ? 8: (size - i);
  136. sendPortailByte(frame[i/8],bits);
  137. }
  138. }