portail.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, 375}, // bit 0
  23. {400, 1140}, // bit 1
  24. {790, 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. frame[0]=0xB6;
  43. frame[1]=0xF7;
  44. frame[2]=0x7B;
  45. frame[3]=0xA1;
  46. frame[4]=0x00;
  47. size = 36;
  48. return;
  49. }
  50. /**
  51. * Send n times a data frame
  52. *
  53. * @param frame the data to send
  54. * @param repeat number of repeatition
  55. */
  56. void sendPortailFrame(unsigned int repeat)
  57. {
  58. unsigned int i;
  59. // switch to real time
  60. cpuMode(REAL_TIME);
  61. // repeat the command
  62. for(i=0; i<repeat; i++) {
  63. sendPortailBit(START_OF_DATA);
  64. sendPortailBytes();
  65. sendPortailBit(BIT0);
  66. }
  67. digitalWrite(homeEasyPinOut, LOW);
  68. // Exit real time mode
  69. cpuMode(STANDARD);
  70. }
  71. /**
  72. * Send a complete command according to Chacon protocole
  73. *
  74. * @param id command id (refer to your remote)
  75. * @param section button section ('A' | 'B' | 'C' | 'D' | 'G')
  76. * @param nb button number(1, 2, 3, 4)
  77. * @param on boolean for on/off
  78. * @param repeat number of repeatition
  79. */
  80. void sendPortailCommand(unsigned int id, char button, unsigned char repeat)
  81. {
  82. unsigned long int command;
  83. // build the command
  84. createPortailCommand(id, button);
  85. // send data
  86. sendPortailFrame(repeat);
  87. // release the memory
  88. }
  89. /**
  90. * Send a bit to the RF transmitter
  91. *
  92. * @param bit the bit to transmit (0 | 1 | TRIGGER0 | TRIGGER1 | END_OF_FRAME)
  93. */
  94. void sendPortailBit(unsigned char bit)
  95. {
  96. digitalWrite(homeEasyPinOut, LOW);
  97. delayMicroseconds(portail_timings[bit][0]);
  98. digitalWrite(homeEasyPinOut, HIGH);
  99. delayMicroseconds(portail_timings[bit][1]);
  100. }
  101. /**
  102. * Send a byte to the RF transmitter
  103. *
  104. * @param byte the byte to transmit
  105. */
  106. void sendPortailByte(unsigned char byte, unsigned int bits)
  107. {
  108. int i;
  109. for(i=(0x01 << (bits-1));i>0; i>>=1) {
  110. sendPortailBit((byte & i) == i);
  111. }
  112. }
  113. /**
  114. * Send the content of a buffer to the RF transmitter
  115. *
  116. * @param buffer the buffer to transmit
  117. */
  118. void sendPortailBytes(void)
  119. {
  120. int i;
  121. for(i = 0; i < size; i+=8 ) {
  122. register unsigned int bits = (size - i >= 8) ? 8: (size - i);
  123. sendPortailByte(frame[i],bits);
  124. }
  125. }