repeater.ino 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "queue.h"
  2. boolean disableRx = true;
  3. const int rxPinA = 7; // RX, Connected to pin 7
  4. const int enablePin = 4; // Enable, Connected to RTX pin 6: 0=Power down, 1=Active
  5. const int txEnablePin = 5; // rx/TX, Connected to RTX pin 5: 0=Rx, 1=Tx
  6. const int txPin = 6; // TX, Connected to RTX pin 4
  7. //********************************************************************
  8. // SETUP
  9. //********************************************************************
  10. void setup() {
  11. disableRx = true;
  12. pinMode(rxPinA, INPUT);
  13. pinMode(enablePin, OUTPUT);
  14. pinMode(txEnablePin, OUTPUT);
  15. pinMode(txPin, OUTPUT);
  16. digitalWrite(enablePin, LOW);
  17. digitalWrite(txEnablePin, LOW);
  18. digitalWrite(txPin, LOW);
  19. // ------------------ INIT THE TRANCEIVER -------------------------
  20. // From powerdown mode (pin 4-5-6 low),
  21. // 1. Drive high pin 6 (ENABLE)
  22. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  23. // 3. Drive down 20us pin 6 (ENABLE).
  24. digitalWrite(enablePin, HIGH); delayMicroseconds( 20 );
  25. digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW);
  26. delayMicroseconds( 40 );
  27. digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH);
  28. delayMicroseconds( 200 );
  29. // ------------------ INIT THE TRANCEIVER -------------------------
  30. Serial.begin(9600); // USB Serial
  31. initLuxSensor(); // Init the lux sensor on i2c
  32. rcvInitQueue();
  33. cli(); // Clear interrupt
  34. setup_timer_interrupt();
  35. sei(); // Enable interrupt
  36. disableRx = false;
  37. }
  38. //********************************************************************
  39. // INTERRUPT FUNCTIONS
  40. //********************************************************************
  41. void setup_timer_interrupt()
  42. {
  43. //set timer1 interrupt at 10Hz
  44. TCCR1A = 0;// set entire TCCR1A register to 0
  45. TCCR1B = 0;// same for TCCR1B
  46. TCNT1 = 0;//initialize counter value to 0
  47. // set compare match register for 1hz increments
  48. // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
  49. OCR1A = 40;
  50. // turn on CTC mode
  51. TCCR1B |= (1 << WGM12);
  52. // Set CS12:CS10 to 010 = /8 prescaler
  53. TCCR1B |= (1 << CS11);
  54. // enable timer compare interrupt
  55. TIMSK1 |= (1 << OCIE1A);
  56. }
  57. // 10uS timer interrupt function
  58. ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
  59. static unsigned char currValue = 0;
  60. static unsigned short samples = 0;
  61. static unsigned short newSamples = 0;
  62. //return; // FOR DEBUG, Disables nexa receive
  63. // Sample the pin value
  64. unsigned char value = digitalRead(rxPinA);
  65. if( value == currValue ) {
  66. samples++;
  67. samples+=newSamples;
  68. newSamples=0;
  69. }
  70. else {
  71. newSamples++;
  72. }
  73. if( newSamples == 3 ) {
  74. uint16_t queSamples = (uint16_t)((samples*20 > 60000)? 60000 : samples*20);
  75. rcvEnQueue(queSamples); // Max 46000
  76. samples = newSamples;
  77. newSamples = 0;
  78. currValue = value;
  79. }
  80. }
  81. void stopInterrupts() {
  82. EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register
  83. }
  84. //********************************************************************
  85. // LOOP
  86. //
  87. // Test-code: <NT1B5C1D83>
  88. //********************************************************************
  89. #define SER_RX_MODE_WAIT 0
  90. #define SER_RX_MODE_RCV 1
  91. #define SER_RX_MODE_END 2
  92. extern unsigned long x_data; // Receive data from Nexa
  93. extern unsigned long temp1_x_data; // Receive data from temp1
  94. void loop() {
  95. static int rxMode = SER_RX_MODE_WAIT;
  96. static int rxNum = 0;
  97. static unsigned long last_rx_time = 0;
  98. static unsigned char txDataReady = 0;
  99. static unsigned long previous_nexa_x_data = 0;
  100. static unsigned long nexa_old_time=0;
  101. static unsigned long previous_temp1_x_data = 0;
  102. static unsigned long temp1_old_time=0;
  103. //static boolean firstRun = true;
  104. static uint16_t width;
  105. static unsigned long txCode = 0;
  106. static unsigned long previous_lux_time = 20000; // Start 1st measure after 20 sec
  107. static unsigned long previous_now = 0; // Get the current time
  108. unsigned long now = millis(); // Get the current time
  109. // Check millis() for wrapping (after 49 days)
  110. if( previous_now > now ) {
  111. last_rx_time = 0; // Reset last rx time
  112. previous_lux_time = 0;
  113. }
  114. // Display version information 10 sec after startup
  115. /* if( firstRun == true && now > 30000 ) {
  116. Serial.println("433MHz Repeater module. (Arduino nano) Ver: 170211 14:46"); // **** VERSION ****
  117. firstRun = false;
  118. } */
  119. if( now > previous_lux_time ) {
  120. // Yes ! It's time to measure lux value
  121. uint64_t luxm=0;
  122. for(int i=1; i<=10; i++ ) {
  123. int32_t lux_temp = readLuxSensor();
  124. luxm += lux_temp;
  125. }
  126. luxm /= 10;
  127. uint32_t lux = (uint32_t) luxm;
  128. if( lux > 0xFFFF ) lux=0xFFFF;
  129. if( lux < 0 ) lux=0;
  130. Serial.println(lux);
  131. // Set next measure to 3 minutes +/- 20 sec
  132. previous_lux_time = now + 120000 + (random(20)*1000);
  133. txDataReady = 1;
  134. txCode = 0x6AD50000;
  135. txCode |= (lux & 0x0000FFFF);
  136. //blinkTheLED();
  137. }
  138. if( rcvDeQueue(&width) ) {
  139. // ********** NEXA ***********
  140. if( nextPulseNexa(width) ) {
  141. last_rx_time = now; // Set last rx time to now
  142. if( x_data != previous_nexa_x_data || now > (nexa_old_time+1000) ) {
  143. unsigned long remote_id = (x_data & 0xFFFFFFC0) >>6;
  144. int group = (x_data & 0x00000020) >>5;
  145. int onOff = (x_data & 0x00000010) >>4;
  146. int channel = (x_data & 0x0000000C) >>2;
  147. int button = (x_data & 0x00000003);
  148. //Serial.println(remote_id);
  149. if( ( remote_id == 11067169UL || remote_id == 20071093UL ) && txDataReady == 0 ) {
  150. remote_id++; // Increase the remote id with one ( = the real id of the nexa remote control )
  151. txDataReady = 1;
  152. txCode = ((remote_id << 6) & 0xFFFFFFC0 );
  153. txCode |= ((group << 5) & 0x00000020 );
  154. txCode |= ((onOff << 4) & 0x00000010 );
  155. txCode |= ((channel << 2) & 0x0000000C );
  156. txCode |= ((button ) & 0x00000003 );
  157. }
  158. previous_nexa_x_data = x_data;
  159. }
  160. nexa_old_time = now;
  161. resetDecoder();
  162. }
  163. }
  164. if( txDataReady == 1 && (now > (last_rx_time+200)) ) { // Transmit if it has passed more than 100mS since last rx
  165. txDataReady = 0;
  166. sendNexaCode(txCode);
  167. }
  168. previous_now = now; // Store to check for wrapping of millis()
  169. }