fixed_it

patch to STAR_momentary firmware for PD68 TripleDown

Mar 16th, 2016
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/STAR_momentary.c b/STAR_momentary.c
  2. index 1cbed87..4502b36 100644
  3. --- a/STAR_momentary.c
  4. +++ b/STAR_momentary.c
  5. @@ -72,11 +72,12 @@
  6.   */
  7.  
  8.  #define VOLTAGE_MON            // Comment out to disable - ramp down and eventual shutoff when battery is low
  9. -#define MODES          0,3,14,39,125,255       // Must be low to high, and must start with 0
  10. -#define ALT_MODES      0,3,14,39,125,255       // Must be low to high, and must start with 0, the defines the level for the secondary output. Comment out if no secondary output
  11. -#define MODE_PWM       0,PHASE,FAST,FAST,FAST,PHASE        // Define one per mode above. 0 tells the light to go to sleep
  12. -#define TURBO              // Comment out to disable - full output with a step down after n number of seconds
  13. -                           // If turbo is enabled, it will be where 255 is listed in the modes above
  14. +#define MODES          0,3,14,39,125,255,TURBO     // Must be low to high, and must start with 0
  15. +#define ALT_MODES      0,3,14,39,125,255,0         // Must be low to high, and must start with 0, the defines the level for the secondary output. Comment out if no secondary output
  16. +#define MODE_PWM       0,PHASE,FAST,FAST,FAST,PHASE,PHASE      // Define one per mode above. 0 tells the light to go to sleep
  17. +#define TURBO          254 // Convenient define for the turbo level in MODES above.
  18. +                           // full output with a step down after n number of seconds if TURBO_TIMEOUT is defined.
  19. +                           // This is not defined to 255 so that value remains a valid PWM choice in MODES.
  20.  #define TURBO_TIMEOUT  5625 // How many WTD ticks before before dropping down (.016 sec each)
  21.                             // 90  = 5625
  22.                             // 120 = 7500
  23. @@ -103,7 +104,8 @@
  24.  #include <avr/sleep.h>
  25.  //#include <avr/power.h>
  26.  
  27. -#define STAR3_PIN   PB4     // If not connected, will cycle L-H.  Connected, H-L
  28. +#define LOW_TO_HIGH 1       // If 1, will cycle L-H. If 0, H-L
  29. +#define FET_PIN     PB4
  30.  #define SWITCH_PIN  PB3        // what pin the switch is connected to, which is Star 4
  31.  #define PWM_PIN     PB1
  32.  #define ALT_PWM_PIN PB0
  33. @@ -140,7 +142,6 @@ const uint8_t alt_modes[] = { ALT_MODES };
  34.  const uint8_t mode_pwm[] = { MODE_PWM };
  35.  volatile uint8_t mode_idx = 0;
  36.  volatile uint8_t press_duration = 0;
  37. -volatile uint8_t low_to_high = 0;
  38.  volatile uint8_t in_momentary = 0;
  39.  
  40.  // Debounce switch press value
  41. @@ -288,7 +289,7 @@ ISR(WDT_vect) {
  42.  
  43.         if (press_duration == LONG_PRESS_DUR) {
  44.             // Long press
  45. -           if (low_to_high) {
  46. +           if (LOW_TO_HIGH) {
  47.                 prev_mode();
  48.             } else {
  49.                 next_mode();
  50. @@ -318,15 +319,15 @@ ISR(WDT_vect) {
  51.         // Not pressed
  52.         if (press_duration > 0 && press_duration < LONG_PRESS_DUR) {
  53.             // Short press
  54. -           if (low_to_high) {
  55. +           if (LOW_TO_HIGH) {
  56.                 next_mode();
  57.             } else {
  58.                 prev_mode();
  59.             }  
  60.         } else {
  61.             // Only do turbo check when switch isn't pressed
  62. -       #ifdef TURBO
  63. -           if (modes[mode_idx] == 255) {
  64. +       #ifdef TURBO_TIMEOUT
  65. +           if (modes[mode_idx] == TURBO) {
  66.                 turbo_ticks++;
  67.                 if (turbo_ticks > TURBO_TIMEOUT) {
  68.                     // Go to the previous mode
  69. @@ -372,16 +373,16 @@ int main(void)
  70.  { 
  71.     // Set all ports to input, and turn pull-up resistors on for the inputs we are using
  72.     DDRB = 0x00;
  73. -   PORTB = (1 << SWITCH_PIN) | (1 << STAR3_PIN);
  74. +   PORTB = (1 << SWITCH_PIN);
  75.  
  76.     // Set the switch as an interrupt for when we turn pin change interrupts on
  77.     PCMSK = (1 << SWITCH_PIN);
  78.    
  79. -    // Set PWM pin to output
  80. +    // Set FET and PWM pin to output
  81.     #ifdef ALT_MODES
  82. -    DDRB = (1 << PWM_PIN) | (1 << ALT_PWM_PIN);
  83. +    DDRB = (1 << FET_PIN) | (1 << PWM_PIN) | (1 << ALT_PWM_PIN);
  84.     #else
  85. -   DDRB = (1 << PWM_PIN);
  86. +   DDRB = (1 << FET_PIN) | (1 << PWM_PIN);
  87.     #endif
  88.  
  89.      // Set timer to do PWM for correct output pin and set prescaler timing
  90. @@ -396,14 +397,6 @@ int main(void)
  91.     #endif
  92.     ACSR   |=  (1<<7); //AC off
  93.    
  94. -   // Determine if we are going L-H, or H-L based on Star 3
  95. -   if ((PINB & (1 << STAR3_PIN)) == 0) {
  96. -       // High to Low
  97. -       low_to_high = 0;
  98. -   } else {
  99. -       low_to_high = 1;
  100. -   }
  101. -  
  102.     // Enable sleep mode set to Power Down that will be triggered by the sleep_mode() command.
  103.     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  104.     sleep_until_switch_press();
  105. @@ -427,7 +420,18 @@ int main(void)
  106.                     #endif
  107.                 }
  108.             }
  109. -           PWM_LVL     = modes[mode_idx];
  110. +           if (modes[mode_idx] == TURBO)
  111. +           {
  112. +               // Turn on FET instead of main PWM.
  113. +               PORTB |= (1 << FET_PIN);
  114. +               PWM_LVL = 0;
  115. +           }
  116. +           else
  117. +           {
  118. +               // Turn off FET, use requested main PWM.
  119. +               PORTB &= ~(1 << FET_PIN);
  120. +               PWM_LVL     = modes[mode_idx];
  121. +           }
  122.             #ifdef ALT_MODES
  123.             ALT_PWM_LVL = alt_modes[mode_idx];
  124.             #endif
  125. @@ -441,4 +445,4 @@ int main(void)
  126.     }
  127.  
  128.      return 0; // Standard Return Code
  129. -}
  130. \ No newline at end of file
  131. +}
RAW Paste Data