BrickUp API Service for Docker version.

irSend.cpp 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "IRremote.h"
  2. #include "IRremoteInt.h"
  3. //+=============================================================================
  4. void IRsend::sendRaw (const unsigned int buf[], unsigned int len, unsigned int hz)
  5. {
  6. // Set IR carrier frequency
  7. enableIROut(hz);
  8. for (unsigned int i = 0; i < len; i++) {
  9. if (i & 1) space(buf[i]) ;
  10. else mark (buf[i]) ;
  11. }
  12. space(0); // Always end with the LED off
  13. }
  14. //+=============================================================================
  15. // Sends an IR mark for the specified number of microseconds.
  16. // The mark output is modulated at the PWM frequency.
  17. //
  18. void IRsend::mark (unsigned int time)
  19. {
  20. TIMER_ENABLE_PWM; // Enable pin 3 PWM output
  21. if (time > 0) custom_delay_usec(time);
  22. }
  23. //+=============================================================================
  24. // Leave pin off for time (given in microseconds)
  25. // Sends an IR space for the specified number of microseconds.
  26. // A space is no output, so the PWM output is disabled.
  27. //
  28. void IRsend::space (unsigned int time)
  29. {
  30. TIMER_DISABLE_PWM; // Disable pin 3 PWM output
  31. if (time > 0) IRsend::custom_delay_usec(time);
  32. }
  33. //+=============================================================================
  34. // Enables IR output. The khz value controls the modulation frequency in kilohertz.
  35. // The IR output will be on pin 3 (OC2B).
  36. // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
  37. // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
  38. // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
  39. // controlling the duty cycle.
  40. // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
  41. // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
  42. // A few hours staring at the ATmega documentation and this will all make sense.
  43. // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
  44. //
  45. void IRsend::enableIROut (int khz)
  46. {
  47. // Disable the Timer2 Interrupt (which is used for receiving IR)
  48. TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
  49. pinMode(TIMER_PWM_PIN, OUTPUT);
  50. digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low
  51. // COM2A = 00: disconnect OC2A
  52. // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
  53. // WGM2 = 101: phase-correct PWM with OCRA as top
  54. // CS2 = 000: no prescaling
  55. // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
  56. TIMER_CONFIG_KHZ(khz);
  57. }
  58. //+=============================================================================
  59. // Custom delay function that circumvents Arduino's delayMicroseconds limit
  60. void IRsend::custom_delay_usec(unsigned long uSecs) {
  61. if (uSecs > 4) {
  62. unsigned long start = micros();
  63. unsigned long endMicros = start + uSecs - 4;
  64. if (endMicros < start) { // Check if overflow
  65. while ( micros() > start ) {} // wait until overflow
  66. }
  67. while ( micros() < endMicros ) {} // normal wait
  68. }
  69. //else {
  70. // __asm__("nop\n\t"); // must have or compiler optimizes out
  71. //}
  72. }