]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/lights.h
ambient.c: settings for my wife's bike
[bike-lights.git] / firmware / lights.h
1 #ifndef LIGHTS_H__
2 #define LIGHTS_H__ 1
3
4 #define TESTING_FW 1
5 #define PAVLINA 1       // settings for my wife's bike
6
7 #define N_LEDS 4
8 #define N_STATUS_LED 1
9 #define N_ILLUM_LED 2
10 #define N_LASER_LED 4
11
12 #define N_PWMLEDS 3
13 #define N_PWMLED_MODES 3
14
15 #define N_BUTTONS 2
16
17 /* logging.c */
18 #ifdef USE_LOGGING
19 void init_log(unsigned char mcusr);
20 void log_set_state(unsigned char val);
21 void log_flush();
22 void log_byte(unsigned char byte);
23 void log_word(uint16_t word);
24 #else
25 void inline init_log(unsigned char mcusr) { }
26 void inline log_set_state(unsigned char val) { }
27 void inline log_flush() { }
28 void inline log_byte(unsigned char byte) { }
29 void inline log_word(uint16_t word) { }
30 #endif
31
32 /* adc.c */
33 #define PWMLED_ADC_SHIFT 1 /* 1<<1 measurements per single callback */
34 void init_adc();
35 void susp_adc();
36 void timer_start_slow_adcs();
37 extern volatile unsigned char adc_is_on;
38
39 /* pwm.c */
40 /*
41  * The real Timer/Counter 1 frequency should not be too close to the
42  * A/D converter frequency (125 kHz). Note that this is not the Top
43  * value of T/C 1, it is shifted by PWM_STEP_SHIFT as described in pwm.c
44  */
45 #define PWM_MAX 0x780
46 extern volatile unsigned char channels_running;
47 #define PWM_IS_ON(n)    (channels_running & (1 << (n)))
48 #define TIMER1_IS_ON()  (channels_running)
49 void init_pwm();
50 void susp_pwm();
51 void pwm_off(unsigned char n);
52 void pwm_set(unsigned char n, uint16_t stride);
53 void pwm_timer();
54 void pwm_disable_if_not_needed();
55
56 /* tmr.c */
57 extern volatile uint16_t jiffies;
58 void init_tmr();
59 void susp_tmr();
60
61 /* pwmled.c */
62 void init_pwmled();
63 void pwmled_adc(unsigned char n, uint16_t adcval);
64 void pwmled_set_mode(unsigned char n, unsigned char mode);
65 void pwmled_set_brightness(uint16_t brightness);
66 #define PWMLED_BRIGHTNESS(l0_lo, l0_hi, l1, l2_lo, l2_hi) ( \
67            (uint16_t)(l0_lo) \
68         | ((uint16_t)(l0_hi) << 3) \
69         | ((uint16_t)(l1)    << 6) \
70         | ((uint16_t)(l2_lo) << 9) \
71         | ((uint16_t)(l2_hi) << 12) \
72         )
73
74 /* gpio.c */
75 void init_gpio();
76 void susp_gpio();
77 void gpio_set(unsigned char n, unsigned char on);
78
79 /* ambient.c */
80 #define AMBIENT_ADC_SHIFT 0     /* 1 measurement per callback */
81 #define N_AMBIENT_ZONES 4
82 void init_ambient();
83 void susp_ambient();
84 void ambient_log_min_max();
85 extern volatile unsigned char ambient_zone, ambient_shadow;
86 void ambient_adc(uint16_t adc_val);
87
88 /* pattern.c */
89 typedef struct {
90         unsigned char mode: 5;
91         unsigned char duration_fib: 3;
92 } pattern_t;
93
94 #define PATTERN_END { 0, 0 }
95 #define D_1 1
96 #define D_2 2
97 #define D_3 3
98 #define D_5 4
99 #define D_8 5
100 #define D_13 6
101 #define D_21 7
102
103 #define PWM_PAT(L2, L1, L0)     ( \
104         (((L2) & 3) << 3) | \
105         (((L1) & 1) << 2) | \
106         (((L0) & 3)) )
107
108 void init_pattern();
109 void patterns_next_tick();
110 void led_set_pattern(unsigned char led, pattern_t *pattern);
111 pattern_t *number_pattern(unsigned char num, unsigned char inv);
112 void pattern_reload();
113 void pwmleds_update_mode();
114
115 /* buttons.c */
116 #define N_USER_PARAMS 3
117 void init_buttons();
118 void susp_buttons();
119 void timer_check_buttons();
120 void button_adc(uint16_t adcval);
121 unsigned char get_user_param(unsigned char param);
122 unsigned char buttons_wait_for_release();
123 unsigned char buttons_setup_in_progress();
124 pattern_t *buttons_setup_status0_pattern_select();
125 pattern_t *buttons_setup_status1_pattern_select();
126
127 /* battery.c */
128 void battery_adc();
129 void init_battery();
130 unsigned char battery_gauge();
131
132 /* control.c */
133 extern pattern_t on_pattern[];
134 typedef union {
135         unsigned char any_flag;
136         struct {
137                 unsigned char booting : 1;
138                 unsigned char braking : 1;
139                 unsigned char err_battery : 1;
140                 unsigned char err_pwmled0 : 1;
141                 unsigned char err_pwmled1 : 1;
142                 unsigned char err_pwmled2 : 1;
143         };
144 } err_flags_t;
145 extern volatile err_flags_t err_flags;
146
147 void init_control();
148 void brake_on();
149 void brake_off();
150 void toggle_dim_mode();
151 void set_panic_mode();
152 pattern_t *pwmled_pattern_select();
153 pattern_t *status_led_pattern_select();
154 pattern_t *illumination_led_pattern_select();
155 pattern_t *laser_pattern_select();
156 void pwmled_select_brightness();
157
158 /* main.c */
159 void power_down(unsigned char err);
160
161 #endif /* !LIGHTS_H__ */
162