trennfix/sw: Was not as beautiful as expected. Now it's better!
[eisenbahn.git] / trennfix / sw / src / main.c
index c61741e6b22632ba2ac997c58a2a33952dffb54f..04935fc0651e535c37a6b9da9e6896a3bba5d709 100644 (file)
@@ -99,16 +99,18 @@ void play_tone(uint8_t divisor, uint8_t duration, uint8_t pause)
        uint16_t c;
        TCCR1 = 0x8;
        OCR1C = divisor;
-       OCR1B = divisor / 2;
+       c = (divisor * 2) / 3;
+       OCR1B = c;
        for (c = 0; c < duration - pause; c++)
                _delay_ms(2);
-       OCR1B = 255;
-       OCR1C = 14;
+       TCCR1 = 0x6;
+       OCR1C = PWM_CYCLE;
+       OCR1B = 12;
        for (c = 0; c < pause; c++)
                _delay_ms(2);
        TCCR1 = 0x6;
-       OCR1C = 14;
-       OCR1B = 14;
+       OCR1C = PWM_CYCLE;
+       OCR1B = PWM_CYCLE;
 }
 
 static void tone_enter(void)
@@ -139,8 +141,8 @@ static void snd_on(void)
 static void snd_off(void)
 {
        TCCR1 = 0x6;
-       OCR1C = 14;
-       OCR1B = 14;
+       OCR1C = PWM_CYCLE;
+       OCR1B = PWM_CYCLE;
 }
 
 #else
@@ -165,11 +167,10 @@ static void save_config(void)
 #endif
 }
 
-ISR(PCINT0_vect){
+void mm_switch_pinchange_callback(void)
+{
        static uint8_t btn_last = 0;
 
-       mm_pinchange_handler();
-
        if (BTN_PRESSED && !btn_last) {
                config.learn_mode++;
                config.learn_mode %= LM_END;
@@ -179,7 +180,7 @@ ISR(PCINT0_vect){
 
 static uint8_t get_speed(uint8_t command)
 {
-       uint8_t b1,  b3, b5, b7;
+       uint8_t b1, b3, b5, b7;
                
        b1 = ((command & 0x80) != 0);
        b3 = ((command & 0x20) != 0);
@@ -188,8 +189,7 @@ static uint8_t get_speed(uint8_t command)
        
        //if ((b1 != b2) || (b2 != b3) || (b3 != b4) || (b5 != b6) || (b7 != b8))
        //      return 0xff;
-       
-       return (b1 + b3*2 + b5*4 +b7*8);
+       return (b1 + b3 * 2 + b5 * 4 +b7 * 8);
 }
 
 
@@ -224,15 +224,19 @@ void mm_switch_drive(uint8_t loco, uint8_t function, uint8_t command)
                }
                alert_last = (speed == 1);
                break;
+
        case 50:
                if (speed)
-                       speed -= 1;
-               
+                       speed = speed - 1;
+
                if (config.learn_mode == LM_EASY_MODE) {
                        config.initial_pulse = speed;
                }
                break;
        case 51:
+               if (speed)
+                       speed = speed - 1;
+
                if (config.learn_mode == LM_EASY_MODE)
                        config.on_duty_cycle = speed;
                break;
@@ -419,8 +423,8 @@ void mm_switch_command(uint8_t decoder, uint8_t command)
 void shift(uint8_t mu);
 
 #ifdef WITH_PWM
-#define DRIVE_OFF  {OCR1B = 14;}
-#define DRIVE_ON   {OCR1B = 14 - config.on_duty_cycle;}
+#define DRIVE_OFF  {OCR1B = PWM_CYCLE;}
+#define DRIVE_ON   {OCR1B = PWM_CYCLE - config.on_duty_cycle;}
 #define DRIVE_FULL {OCR1B = 0;}
 #else
 #define DRIVE_OFF  {setpin(PIN_DRIVE, 0);}
@@ -436,7 +440,6 @@ int main(void) {
        uint8_t drive_slope = 0;
 #endif
 
-
 #ifdef USE_REGISTER_VARS
        drive_on = 0;
 #endif
@@ -456,7 +459,6 @@ int main(void) {
                config.learn_mode = LM_LEARN_ON_KEY;
        }
        sei();
-
        while (1) {
        drive_start:
 
@@ -488,7 +490,6 @@ int main(void) {
                            config.learn_mode > LM_LEARN_OP_MODE)
                                continue;
 
-
                        for (i = 0; i < config.learn_mode; i++) {
                                setpin(PIN_LED, 1);
                                snd_on();