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_END
, /* Only a label */
56 uint8_t magic
; /* Magic value */
62 uint8_t initial_pulse
; /* Lenghth of initial pulse in 10ms steps */
63 uint8_t on_duty_cycle
; /* Duty cycle for on. 0-10 */
64 volatile enum learn_mode learn_mode
;
67 static struct EEMEM config ee_config
;
68 static volatile struct config config
;
69 static volatile uint8_t drive_on
= 0;
71 static void load_config(void)
73 eeprom_read_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
76 static void save_config(void)
78 #ifdef USE_EEPROM_UPDATE
79 eeprom_update_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
81 eeprom_write_block((uint8_t *)&config
, &ee_config
, sizeof(config
));
88 static uint8_t btn_last
= 0;
90 mm_pinchange_handler();
92 if (BTN_PRESSED
&& !btn_last
) {
94 config
.learn_mode
%= LM_END
;
96 btn_last
= BTN_PRESSED
;
99 static uint8_t get_speed(uint8_t command
)
101 uint8_t b1
, b3
, b5
, b7
;
103 b1
= ((command
& 0x80) != 0);
104 // b2 = ((command & 0x40) != 0);
105 b3
= ((command
& 0x20) != 0);
106 // b4 = ((command & 0x10) != 0);
107 b5
= ((command
& 0x8) != 0);
108 // b6 = ((command & 0x4) != 0);
109 b7
= ((command
& 0x2) != 0);
110 // b8 = ((command & 0x1) != 0);
112 //if ((b1 != b2) || (b2 != b3) || (b3 != b4) || (b5 != b6) || (b7 != b8))
115 return (b1
+ b3
*2 + b5
*4 +b7
*8);
118 #ifdef INTERPRET_DRIVE_COMMANDS
120 void mm_switch_drive(uint8_t decoder
, uint8_t function
, uint8_t command
)
122 static uint8_t seen_before
= 0;
126 if ((decoder
== 80) && (function
== 0) && (command
== 0xc0)) {
127 config
.learn_mode
= 1;
133 speed
= get_speed(command
);
134 static uint8_t itsme
= 0;
136 if (config
.learn_mode
) {
151 if (speed
>= 1) speed
-= 1;
153 config
.on_duty_cycle
= speed
;
155 config
.on_duty_cycle
= 14;
162 config
.initial_pulse
= speed
;
166 if (decoder
== 31 && command
== 0xc0)
172 #ifdef INTERPRET_DRIVE_SIMPLE
174 static volatile uint8_t current_loco
;
176 void mm_switch_drive(uint8_t decoder
, uint8_t function
, uint8_t command
)
179 speed
= get_speed(command
);
180 current_loco
= decoder
;
183 setpin(PIN_LED
, drive_on
= function
& 3);
184 config
.initial_pulse
= speed
;
185 } else if (decoder
== 51) {
186 if (speed
>= 1) speed
-= 1;
188 config
.on_duty_cycle
= speed
;
190 config
.on_duty_cycle
= 14;
191 } else if (decoder
== 52) {
192 static uint8_t last_speed
;
193 if (speed
== 1 && last_speed
!= 1)
203 static volatile uint8_t switch_on
= 0;
205 void mm_switch_command(uint8_t decoder
, uint8_t command
)
207 static uint8_t toggle_lock
= 0;
208 switch(config
.learn_mode
) {
212 if ((decoder
== config
.decoder_on
) &&
213 (command
== config
.key_on
)) { /* Primary key pressed */
214 switch(config
.op_mode
) {
217 if (!switch_on
&& current_loco
== 52)
223 drive_on
= !drive_on
;
232 if ((decoder
== config
.decoder_on
) &&
233 (command
== 0)) { /* Primary key released */
234 switch(config
.op_mode
) {
245 #ifdef HANDLE_OFF_KEY
247 if ((decoder
== config
.decoder_off
) &&
248 (command
== config
.key_off
)) { /* Secondary "off" key pressed */
249 switch(config
.op_mode
) {
262 case LM_LEARN_ON_KEY
:
264 config
.decoder_on
= decoder
;
265 config
.key_on
= command
;
266 if (config
.op_mode
== OM_DOUBLE
)
267 config
.learn_mode
= LM_LEARN_OFF_KEY
;
269 config
.learn_mode
= LM_OFF
;
273 #ifdef LEARN_ADVANCED
274 case LM_LEARN_OFF_KEY
:
276 config
.decoder_off
= decoder
;
277 config
.key_off
= command
;
278 config
.learn_mode
= LM_OFF
;
283 case LM_LEARN_INITIAL
:
291 if (config
.initial_pulse
>= 10)
292 config
.initial_pulse
-= 10;
294 config
.initial_pulse
= 0;
299 if (config
.initial_pulse
<= 245)
300 config
.initial_pulse
+= 10;
302 config
.initial_pulse
= 255;
319 if (config
.on_duty_cycle
> 0)
320 config
.on_duty_cycle
-= 1;
325 if (config
.on_duty_cycle
< 10)
326 config
.on_duty_cycle
+= 1;
336 case LM_LEARN_OP_MODE
:
339 config
.op_mode
= OM_MOMENTARY
;
341 config
.learn_mode
= LM_OFF
;
344 config
.op_mode
= OM_DOUBLE
;
346 config
.learn_mode
= LM_OFF
;
349 config
.op_mode
= OM_TOGGLE
;
351 config
.learn_mode
= LM_OFF
;
362 /******************************************************************************
364 * main() - The main routine
367 void shift(uint8_t mu
);
370 uint8_t learn_mode_off
;
371 uint8_t drive_last
= 0;
372 uint8_t drive_slope
= 0;
380 if ((config
.op_mode
== OM_ERASED
) || (config
.magic
!= EE_MAGIC
)) {
381 config
.magic
= EE_MAGIC
;
382 config
.op_mode
= OM_MOMENTARY
;
383 config
.decoder_on
= 1;
385 config
.decoder_off
= 1;
387 config
.initial_pulse
= 10;
388 config
.on_duty_cycle
= 5;
389 config
.learn_mode
= LM_LEARN_ON_KEY
;
395 //setpin(PIN_LED,1 );
397 //setpin(PIN_LED,0 );
402 output_on
= drive_on
|| switch_on
;
404 if (!drive_last
&& output_on
)
408 drive_last
= output_on
;
413 for (i
= 0; i
< config
.initial_pulse
; i
++) {
414 setpin(PIN_DRIVE
, 1);
419 for (i
= 0; i
< config
.on_duty_cycle
; i
++) {
420 setpin(PIN_DRIVE
, output_on
);
423 for (i
= 0; i
< (14 - config
.on_duty_cycle
); i
++) {
424 setpin(PIN_DRIVE
, 0);
429 setpin(PIN_DRIVE
, 0);
430 if (!config
.learn_mode
)
433 learn_mode_off
= !config
.learn_mode
;
435 if (output_on
|| (config
.learn_mode
&& learn_mode_off
))
438 for (i
= 0; i
< config
.learn_mode
; i
++) {
441 //if (output_on || (config.learn_mode && learn_mode_off))
445 //if (output_on || (config.learn_mode && learn_mode_off))
448 //if (output_on || (config.learn_mode && learn_mode_off))
451 for (i
= 0; i
< 15 - config
.learn_mode
; i
++) {
452 //if (output_on || (config.learn_mode && learn_mode_off))
456 //if (output_on || (config.learn_mode && learn_mode_off))
466 /******************************************************************************