c61741e6b22632ba2ac997c58a2a33952dffb54f
1 /******************************************************************************
3 * Trennfix firmware - main.c
5 * Copyright (C) 2017 Philipp Hachtmann
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *****************************************************************************/
27 #include <avr/eeprom.h>
28 #include <avr/interrupt.h>
29 #include <avr/pgmspace.h>
31 #include <util/delay.h>
33 #include <mm/mm_switch.h>
34 #include <config/hardware.h>
39 OM_MOMENTARY
, /* on as long as "key on" pressed */
40 OM_DOUBLE
, /* On off with "on" and "off" keys */
41 OM_TOGGLE
, /* toggle on "key on" pressed */
42 OM_ERASED
= 0xff /* EEPROM erased, need setup */
47 LM_LEARN_ON_KEY
, /* Learn primary key */
48 LM_LEARN_OFF_KEY
, /* Learn secondary key, relevant for OM_DOUBLE */
49 LM_LEARN_INITIAL
, /* Learn initial pulse length, 10ms steps */
50 LM_LEARN_DUTY
, /* Learn duty cycle 0-10 */
51 LM_LEARN_OP_MODE
, /* Learn operation mode */
52 LM_EASY_WAIT_PRESS
, /* Wait for key press to enter easy mode */
53 LM_EASY_WAIT_UP
, /* Wait for end of key press */
54 LM_EASY_WAIT_TURN
, /* Wait for loco 80 turn end */
55 LM_EASY_MODE
, /* Easy config mode for PWM and initial kick */
56 LM_END
, /* Only a label */
60 uint8_t magic
; /* Magic value */
66 uint8_t initial_pulse
; /* Lenghth of initial pulse in 10ms steps */
67 uint8_t on_duty_cycle
; /* Duty cycle for on. 0-10 */
68 volatile enum learn_mode learn_mode
;
71 static struct EEMEM config ee_config
;
72 static volatile struct config config
;
74 static volatile uint8_t easy_mode
= 0;
75 static volatile uint8_t easy_mode_possible
= 0;
77 #ifndef USE_REGISTER_VARS
78 static volatile uint8_t drive_on
= 0;
81 /******************************************************************************
83 * Some nice sounds to play on your coil
88 #define sekunde(g)(quinte((quinte(g)))*2)
89 #define terz(g) ((g) * 4 / 5)
90 #define kleine_terz(g) ((g) * 5 / 6)
91 #define quarte(g) ((g) * 3 / 4)
92 #define quinte(g) ((g) * 2 / 3)
93 #define tt(g) ((g) * 32 / 45)
94 #define septime(g) ((g) * 15 / 8)
96 #if defined(WITH_SOUND) && defined(WITH_PWM)
97 void play_tone(uint8_t divisor
, uint8_t duration
, uint8_t pause
)
103 for (c
= 0; c
< duration
- pause
; c
++)
107 for (c
= 0; c
< pause
; c
++)
114 static void tone_enter(void)
116 play_tone((G
), 70, 20);
117 play_tone(terz(G
), 70, 20);
118 play_tone(quinte(G
), 70, 20);
119 play_tone(G
/2, 100, 0);
122 static void tone_good(void)
124 play_tone(G
, 150, 120);
125 play_tone(terz(G
), 100, 70);
126 play_tone(tt(G
), 100, 50);
127 play_tone(quarte(terz((G
))), 50, 0);
128 play_tone(quinte(G
), 150, 0);
129 play_tone(terz(G
), 100, 50);
132 static void snd_on(void)
139 static void snd_off(void)
147 #define play_tone(...)
148 #define tone_enter(...)
149 #define tone_good(...)
154 static void load_config(void)
156 eeprom_read_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
159 static void save_config(void)
161 #ifdef WITH_EEPROM_UPDATE
162 eeprom_update_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
164 eeprom_write_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
169 static uint8_t btn_last
= 0;
171 mm_pinchange_handler();
173 if (BTN_PRESSED
&& !btn_last
) {
175 config
.learn_mode
%= LM_END
;
177 btn_last
= BTN_PRESSED
;
180 static uint8_t get_speed(uint8_t command
)
182 uint8_t b1
, b3
, b5
, b7
;
184 b1
= ((command
& 0x80) != 0);
185 b3
= ((command
& 0x20) != 0);
186 b5
= ((command
& 0x8) != 0);
187 b7
= ((command
& 0x2) != 0);
189 //if ((b1 != b2) || (b2 != b3) || (b3 != b4) || (b5 != b6) || (b7 != b8))
192 return (b1
+ b3
*2 + b5
*4 +b7
*8);
196 void mm_switch_drive(uint8_t loco
, uint8_t function
, uint8_t command
)
199 speed
= get_speed(command
);
200 static uint8_t alert_last
= 0;
204 switch (config
.learn_mode
) {
207 config
.learn_mode
= LM_EASY_WAIT_PRESS
;
210 if ((speed
== 1) && (alert_last
== 0)) {
211 config
.learn_mode
= LM_OFF
;
217 case LM_EASY_WAIT_PRESS
:
219 config
.learn_mode
= LM_OFF
;
225 alert_last
= (speed
== 1);
231 if (config
.learn_mode
== LM_EASY_MODE
) {
232 config
.initial_pulse
= speed
;
236 if (config
.learn_mode
== LM_EASY_MODE
)
237 config
.on_duty_cycle
= speed
;
243 void mm_switch_command(uint8_t decoder
, uint8_t command
)
245 static uint8_t toggle_lock
= 0;
247 switch(config
.learn_mode
) {
251 if ((decoder
== config
.decoder_on
) &&
252 (command
== config
.key_on
)) { /* Primary key pressed */
253 switch(config
.op_mode
) {
260 drive_on
= ~drive_on
;
268 if ((decoder
== config
.decoder_on
) &&
269 (command
== 0)) { /* Primary key released */
270 switch(config
.op_mode
) {
283 case LM_EASY_WAIT_PRESS
:
284 if ((decoder
== config
.decoder_on
) &&
285 (command
== config
.key_on
))
286 config
.learn_mode
= LM_EASY_WAIT_UP
;
289 case LM_EASY_WAIT_UP
:
290 if ((decoder
== config
.decoder_on
) &&
292 config
.learn_mode
= LM_EASY_MODE
;
297 #ifdef HANDLE_OFF_KEY
299 if ((decoder
== config
.decoder_off
) &&
300 (command
== config
.key_off
)) { /* Secondary "off" key pressed */
301 switch(config
.op_mode
) {
314 case LM_LEARN_ON_KEY
:
316 config
.decoder_on
= decoder
;
317 config
.key_on
= command
;
318 if (config
.op_mode
== OM_DOUBLE
)
319 config
.learn_mode
= LM_LEARN_OFF_KEY
;
321 config
.learn_mode
= LM_OFF
;
325 #ifdef LEARN_ADVANCED
326 case LM_LEARN_OFF_KEY
:
328 config
.decoder_off
= decoder
;
329 config
.key_off
= command
;
330 config
.learn_mode
= LM_OFF
;
335 case LM_LEARN_INITIAL
:
343 if (config
.initial_pulse
>= 10)
344 config
.initial_pulse
-= 10;
346 config
.initial_pulse
= 0;
351 if (config
.initial_pulse
<= 245)
352 config
.initial_pulse
+= 10;
354 config
.initial_pulse
= 255;
371 if (config
.on_duty_cycle
> 0)
372 config
.on_duty_cycle
-= 1;
377 if (config
.on_duty_cycle
< 10)
378 config
.on_duty_cycle
+= 1;
388 case LM_LEARN_OP_MODE
:
391 config
.op_mode
= OM_MOMENTARY
;
393 config
.learn_mode
= LM_OFF
;
396 config
.op_mode
= OM_DOUBLE
;
398 config
.learn_mode
= LM_OFF
;
401 config
.op_mode
= OM_TOGGLE
;
403 config
.learn_mode
= LM_OFF
;
414 /******************************************************************************
416 * main() - The main routine
419 void shift(uint8_t mu
);
422 #define DRIVE_OFF {OCR1B = 14;}
423 #define DRIVE_ON {OCR1B = 14 - config.on_duty_cycle;}
424 #define DRIVE_FULL {OCR1B = 0;}
426 #define DRIVE_OFF {setpin(PIN_DRIVE, 0);}
427 #define DRIVE_ON {setpin(PIN_DRIVE, 1); setpin(PIN_LED, 1);}
428 #define DRIVE_FULL {setpin(PIN_DRIVE, 1); setpin(PIN_LED, 0);}
434 #ifdef WITH_INITIAL_PULSE
435 uint8_t drive_last
= 0;
436 uint8_t drive_slope
= 0;
440 #ifdef USE_REGISTER_VARS
447 if (config
.magic
!= EE_MAGIC
) {
448 config
.magic
= EE_MAGIC
;
449 config
.op_mode
= OM_MOMENTARY
;
450 config
.decoder_on
= 1;
452 config
.decoder_off
= 1;
454 config
.initial_pulse
= 10;
455 config
.on_duty_cycle
= 5;
456 config
.learn_mode
= LM_LEARN_ON_KEY
;
463 #ifdef WITH_INITIAL_PULSE
465 if (drive_on
&& !drive_last
)
469 drive_last
= drive_on
;
474 #ifdef WITH_INITIAL_PULSE
477 for (i
= 0; i
< config
.initial_pulse
; i
++) {
487 if (!config
.learn_mode
||
488 config
.learn_mode
> LM_LEARN_OP_MODE
)
492 for (i
= 0; i
< config
.learn_mode
; i
++) {
498 if (drive_on
) goto drive_start
;
500 if (drive_on
) goto drive_start
;
502 for (i
= 0; i
< 15 - config
.learn_mode
; i
++) {
503 if (drive_on
) goto drive_start
;
511 /******************************************************************************