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
)
102 c
= (divisor
* 2) / 3;
104 for (c
= 0; c
< duration
- pause
; c
++)
109 for (c
= 0; c
< pause
; c
++)
116 static void tone_enter(void)
118 play_tone((G
), 70, 20);
119 play_tone(terz(G
), 70, 20);
120 play_tone(quinte(G
), 70, 20);
121 play_tone(G
/2, 100, 0);
124 static void tone_good(void)
126 play_tone(G
, 150, 120);
127 play_tone(terz(G
), 100, 70);
128 play_tone(tt(G
), 100, 50);
129 play_tone(quarte(terz((G
))), 50, 0);
130 play_tone(quinte(G
), 150, 0);
131 play_tone(terz(G
), 100, 50);
134 static void snd_on(void)
141 static void snd_off(void)
149 #define play_tone(...)
150 #define tone_enter(...)
151 #define tone_good(...)
156 static void load_config(void)
158 eeprom_read_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
161 static void save_config(void)
163 #ifdef WITH_EEPROM_UPDATE
164 eeprom_update_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
166 eeprom_write_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
170 void mm_switch_pinchange_callback(void)
172 static uint8_t btn_last
= 0;
174 if (BTN_PRESSED
&& !btn_last
) {
176 config
.learn_mode
%= LM_END
;
178 btn_last
= BTN_PRESSED
;
181 static uint8_t get_speed(uint8_t command
)
183 uint8_t b1
, b3
, b5
, b7
;
185 b1
= ((command
& 0x80) != 0);
186 b3
= ((command
& 0x20) != 0);
187 b5
= ((command
& 0x8) != 0);
188 b7
= ((command
& 0x2) != 0);
190 //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);
232 if (config
.learn_mode
== LM_EASY_MODE
) {
233 config
.initial_pulse
= speed
;
240 if (config
.learn_mode
== LM_EASY_MODE
)
241 config
.on_duty_cycle
= speed
;
247 void mm_switch_command(uint8_t decoder
, uint8_t command
)
249 static uint8_t toggle_lock
= 0;
251 switch(config
.learn_mode
) {
255 if ((decoder
== config
.decoder_on
) &&
256 (command
== config
.key_on
)) { /* Primary key pressed */
257 switch(config
.op_mode
) {
264 drive_on
= ~drive_on
;
272 if ((decoder
== config
.decoder_on
) &&
273 (command
== 0)) { /* Primary key released */
274 switch(config
.op_mode
) {
287 case LM_EASY_WAIT_PRESS
:
288 if ((decoder
== config
.decoder_on
) &&
289 (command
== config
.key_on
))
290 config
.learn_mode
= LM_EASY_WAIT_UP
;
293 case LM_EASY_WAIT_UP
:
294 if ((decoder
== config
.decoder_on
) &&
296 config
.learn_mode
= LM_EASY_MODE
;
301 #ifdef HANDLE_OFF_KEY
303 if ((decoder
== config
.decoder_off
) &&
304 (command
== config
.key_off
)) { /* Secondary "off" key pressed */
305 switch(config
.op_mode
) {
318 case LM_LEARN_ON_KEY
:
320 config
.decoder_on
= decoder
;
321 config
.key_on
= command
;
322 if (config
.op_mode
== OM_DOUBLE
)
323 config
.learn_mode
= LM_LEARN_OFF_KEY
;
325 config
.learn_mode
= LM_OFF
;
329 #ifdef LEARN_ADVANCED
330 case LM_LEARN_OFF_KEY
:
332 config
.decoder_off
= decoder
;
333 config
.key_off
= command
;
334 config
.learn_mode
= LM_OFF
;
339 case LM_LEARN_INITIAL
:
347 if (config
.initial_pulse
>= 10)
348 config
.initial_pulse
-= 10;
350 config
.initial_pulse
= 0;
355 if (config
.initial_pulse
<= 245)
356 config
.initial_pulse
+= 10;
358 config
.initial_pulse
= 255;
375 if (config
.on_duty_cycle
> 0)
376 config
.on_duty_cycle
-= 1;
381 if (config
.on_duty_cycle
< 10)
382 config
.on_duty_cycle
+= 1;
392 case LM_LEARN_OP_MODE
:
395 config
.op_mode
= OM_MOMENTARY
;
397 config
.learn_mode
= LM_OFF
;
400 config
.op_mode
= OM_DOUBLE
;
402 config
.learn_mode
= LM_OFF
;
405 config
.op_mode
= OM_TOGGLE
;
407 config
.learn_mode
= LM_OFF
;
418 /******************************************************************************
420 * main() - The main routine
423 void shift(uint8_t mu
);
426 #define DRIVE_OFF {OCR1B = PWM_CYCLE;}
427 #define DRIVE_ON {OCR1B = PWM_CYCLE - config.on_duty_cycle;}
428 #define DRIVE_FULL {OCR1B = 0;}
430 #define DRIVE_OFF {setpin(PIN_DRIVE, 0); setpin(PIN_LED, 0);}
431 #define DRIVE_ON {setpin(PIN_DRIVE, 1); setpin(PIN_LED, 1);}
432 #define DRIVE_FULL {setpin(PIN_DRIVE, 1); setpin(PIN_LED, 1);}
438 #ifdef WITH_INITIAL_PULSE
439 uint8_t drive_last
= 0;
440 uint8_t drive_slope
= 0;
443 #ifdef USE_REGISTER_VARS
450 if (config
.magic
!= EE_MAGIC
) {
451 config
.magic
= EE_MAGIC
;
452 config
.op_mode
= OM_MOMENTARY
;
453 config
.decoder_on
= 1;
455 config
.decoder_off
= 1;
457 config
.initial_pulse
= 10;
458 config
.on_duty_cycle
= 5;
459 config
.learn_mode
= LM_LEARN_ON_KEY
;
465 #ifdef WITH_INITIAL_PULSE
467 if (drive_on
&& !drive_last
)
471 drive_last
= drive_on
;
476 #ifdef WITH_INITIAL_PULSE
479 for (i
= 0; i
< config
.initial_pulse
; i
++) {
489 if (!config
.learn_mode
||
490 config
.learn_mode
> LM_LEARN_OP_MODE
)
493 for (i
= 0; i
< config
.learn_mode
; i
++) {
499 if (drive_on
) goto drive_start
;
501 if (drive_on
) goto drive_start
;
503 for (i
= 0; i
< 15 - config
.learn_mode
; i
++) {
504 if (drive_on
) goto drive_start
;
512 /******************************************************************************