DIGILIGHT
setup.c
См. документацию.
1 
9 #include <avr/io.h>
10 #include "avr_helper.h"
11 #include <avr/eeprom.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <avr/pgmspace.h>
15 #include <avr/wdt.h>
16 #include <avr/interrupt.h>
17 #include <util/delay.h>
18 #include "power_control.h"
19 #include "global.h"
20 #include "lcd.h"
21 #include "lcd_show.h"
22 #include "hardware.h"
23 #include "setup.h"
24 #include "control.h"
25 #include "ir_remote.h"
26 #include "menu.h"
27 
46 __flash const char VOL_NAME[] = "SENSITIVITY";
47 static __flash const char str2[] = "A.G.C.";
48 static __flash const char str3[] = "SLEEP TIME";
49 static __flash const char str4[] = "WS2812 IN GROUP";
50 static __flash const char str5[] = "GROUPS OF WS2812";
51 static __flash const char str6[] = "RESET TO DEFAULT";
52 static __flash const char str7[] = "IR REMOTE MENU";
53 static __flash const char str8[] = "DC OFFSET";
54 
55 static __flash const char ir0[] = "POWER OFF";
56 static __flash const char ir1[] = "VOLUME+";
57 static __flash const char ir2[] = "VOLUME-";
58 static __flash const char ir3[] = "EFFECT NEXT";
59 static __flash const char ir4[] = "EFFECT PREV";
60 static __flash const char ir5[] = "PRESET NEXT";
61 static __flash const char ir6[] = "PRESET PREV";
62 static __flash const char ir7[] = "DONE UP LEARNING";
63 
64 static void default_setup(void){
65  cfg.pixels_in_group = 1;
66  cfg.group_of_pixels = 96;
67  cfg.time_to_sleep = 0;
68  cfg.sensitivity = 50;
69  cfg.agc_enabled = 0;
70 }
71 
72 static void default_ir_setup(void){
73 #include "ir_car_mp3.h"
74  init_ir_cmd_code(0, CMD_VOLUME_PLUS, IR_VOL_PLUS);
75  init_ir_cmd_code(1, CMD_VOLUME_MINUS, IR_VOL_MINUS);
76  init_ir_cmd_code(2, CMD_EFFECT_PLUS, IR_CH_PLUS);
77  init_ir_cmd_code(3, CMD_EFFECT_MINUS, IR_CH_MINUS);
78  init_ir_cmd_code(4, CMD_PRESET_PLUS, IR_NEXT);
79  init_ir_cmd_code(5, CMD_PRESET_MINUS, IR_PREV);
80  init_ir_cmd_code(6, CMD_POWER, IR_NO_CMD);
81 }
82 
83 static EEMEM config_t e_cfg = {
84  .clear = 0xFF
85 };
86 
87 void update_config(void){
88  eeprom_update_block(&cfg, &e_cfg, sizeof(config_t));
89 }
90 
91 INIT(6){
92  // этот модуль обязан инициализировать раньше, чем lcd_show !
93  eeprom_read_block(&cfg, &e_cfg, sizeof(config_t));
94  if(cfg.clear){
95  memset(&cfg, 0, sizeof(config_t));
96  default_setup();
97  cfg.band_mask[0] = BAND_LF;
98  cfg.band_mask[1] = BAND_MF;
99  cfg.band_mask[0] = BAND_HF;
100  cfg.dc_offset = 0x7FE0;
101  eeprom_update_block(&cfg, &e_cfg, sizeof(config_t));
102  default_ir_setup();
103  }
104 }
105 
106 static menu_result_t reset_to_default(uint16_t d){
107  default_setup();
108  lcd_clrscr();
109  lcd_puts_P("ALL SETTINGS\nRESET TO DEFAULT");
110  _delay_ms(1000);
111  return MENU_DONE;
112 }
113 
114 #define DC_OFS_MIN 29696UL
115 #define DC_OFS_MAX 35200UL
116 
117 static void edit_dc(int8_t d, uint16_t data){
118  uint16_t tmp = cfg.dc_offset;
119  tmp += d*0x40;
120  if(tmp < DC_OFS_MIN) tmp = DC_OFS_MIN;
121  else if(tmp > DC_OFS_MAX) tmp = DC_OFS_MAX;
122  cfg.dc_offset = tmp;
123 }
124 
125 static void paint_dc(int32_t d){
126  char tmp[10];
127  signal_t *ptr = (void*)(int)d;
128 
129  lcd_gotoxy(0,1);
130  lcd_puts_P("DC=");
131  itoa(ptr->average_vol, tmp, 10);
132  lcd_puts(tmp);
133  lcd_puts_P("/");
134  utoa(cfg.dc_offset, tmp, 10);
135  lcd_puts(tmp);
136  lcd_puts_P(" ");
137 }
138 
139 #define ir_wait lock_current_menu_item
140 static uint32_t ir_code;
141 
142 static void ir_paint(int32_t d){
143  static uint8_t div;
144 
145  uint32_t tmp;
146  tmp = ir_wait ? get_ir_code() : get_ir_cmd_code(d);
147  if(tmp) ir_code = tmp;
148 
149  lcd_gotoxy(0,1);
150  lcd_puts_P("CODE: ");
151 
152  if((++div & 0x20) && ir_wait)
153  lcd_puts_P(" ");
154  else
155  if((ir_code == 0) || (ir_code == IR_NO_CMD)){
156  lcd_puts_P("-------- ");
157  } else {
158  show_hex_long(ir_code);
159  }
160 
161  if(ir_wait) reset_menu_timeout();
162 }
163 
164 static menu_result_t ir_action(uint16_t d){
165  if(ir_wait){
166  set_ir_cmd_code(d, ir_code);
167  ir_wait = 0;
168  } else {
169  ir_wait = 1;
170  ir_code = 0;
172  }
173  return MENU_CONTINUE;
174 }
175 
176 static menu_result_t ir_action_reset(uint16_t d){
177  default_ir_setup();
178  lcd_clrscr();
179  lcd_puts_P("IR-REMOTE RESET\nTO DEFAULT");
180  return MENU_DONE_MSG;
181 }
182 
183 static void edit_pix(int8_t d, uint16_t data){
184  // data - указатель на поле в cfg
185  int16_t tmp = *(uint8_t *)data;
186  uint8_t gop = cfg.group_of_pixels;
187  uint8_t pig = cfg.pixels_in_group;
188 
189  if((uint8_t *)data == &cfg.group_of_pixels){
190  // кол-во групп
191  tmp += d*STEP_GROUP_OF_PIX;
192  if(tmp < MIN_GROUP_OF_PIX) tmp = MIN_GROUP_OF_PIX;
193  if(tmp > MAX_GROUP_OF_PIX) tmp = MAX_GROUP_OF_PIX;
194  } else {
195  // пикселов в группе
196  tmp += d*STEP_PIX_IN_GROUP;
197  if(tmp < MIN_PIX_IN_GROUP) tmp = MIN_PIX_IN_GROUP;
198  if(tmp > MAX_PIX_IN_GROUP) tmp = MAX_PIX_IN_GROUP;
199  }
200  *(uint8_t *)data = tmp;
201  if((cfg.group_of_pixels * cfg.pixels_in_group) > MAX_TOTAL_PIX){
202  cfg.group_of_pixels = gop;
203  cfg.pixels_in_group = pig;
204  }
205 }
206 
207 static void paint_pix(int32_t d){
208  show_number(*(uint8_t*)(uint16_t)d);
209 }
210 
211 static menu_result_t update_and_reboot(uint16_t d){
212  update_config();
213  lcd_clrscr();
214  lcd_puts_P("ALL SETTINGS\nSAVED");
215  _delay_ms(1000);
216  return MENU_DONE;
217 }
218 
219 static __flash const menu_item_t __flash const ir_menu_items[] = {
220  _MI_USER(ir0, NULL, ir_paint, ir_action, CMD_POWER),
221  _MI_USER(ir1, NULL, ir_paint, ir_action, CMD_VOLUME_PLUS),
222  _MI_USER(ir2, NULL, ir_paint, ir_action, CMD_VOLUME_MINUS),
223  _MI_USER(ir3, NULL, ir_paint, ir_action, CMD_EFFECT_PLUS),
224  _MI_USER(ir4, NULL, ir_paint, ir_action, CMD_EFFECT_MINUS),
225  _MI_USER(ir5, NULL, ir_paint, ir_action, CMD_PRESET_PLUS),
226  _MI_USER(ir6, NULL, ir_paint, ir_action, CMD_PRESET_MINUS),
227  _MI_USER(ir7, NULL, show_press_encoder_msg, NULL, 0),
228  _MI_USER(str6, NULL, show_press_encoder_msg, ir_action_reset, 0)
229 };
230 
231 static __flash const menu_t ir_menu = _MENU(ir_menu_items);
232 
233 static __flash const menu_item_t __flash const main_menu_items[] = {
234  _MI_SCALE(VOL_NAME, cfg.sensitivity, 0, MAX_LEVEL, 1),
235  _MI_U8(str3, cfg.time_to_sleep, 0, 60, 5),
236  _MI_ONOFF(str2, cfg.agc_enabled),
237  _MI_USER(str5, edit_pix, paint_pix, update_and_reboot, &cfg.group_of_pixels),
238  _MI_USER(str4, edit_pix, paint_pix, update_and_reboot, &cfg.pixels_in_group),
239  _MI_USER(str8, edit_dc, paint_dc, update_and_reboot, &music),
240  _MI_SUBMENU(str7, ir_menu),
241  _MI_USER(str6, NULL, show_press_encoder_msg, reset_to_default,0)
242 };
243 
244 __flash const menu_t main_menu = _MENU(main_menu_items);
245 
#define lcd_puts_P(__s)
macros for automatically storing string constant in program memory
Definition: lcd.h:243
void lcd_gotoxy(uint8_t x, uint8_t y)
Set cursor to specified position.
Definition: lcd.c:380
#define BAND_LF
битовые маски частот спектра, объединенные в НЧ-полосу
Definition: global.h:58
#define BAND_HF
битовые маски частот спектра, объединенные в ВЧ-полосу
Definition: global.h:62
#define MAX_LEVEL
фиксированное количество уровней регулирования чувствительности
Definition: global.h:81
вариативный тип пункта меню
Definition: menu.h:41
Константы команд пульта управления INCAR MP3.
signal_t music
Параметры мелодии
Definition: main.c:172
volume_t average_vol
средняя громкость
Definition: global.h:74
void lcd_clrscr(void)
Clear display and set cursor to home position.
Definition: lcd.c:416
void set_ir_cmd_code(control_cmd_t cmd, uint32_t code)
назначение команде IR-кода
Definition: ir_remote.c:170
Вспомогательные эффекты ЖКИ
структура конфигурации устройства
Definition: global.h:84
Общие описания
uint8_t sensitivity
уровень регулировки чувствительности
Definition: global.h:89
структура параметров звукового сигнала
Definition: global.h:72
void show_hex_long(uint32_t num)
вывод 4 байтового числа в HEX формате с лидирующими нулями
Definition: lcd_show.c:216
uint8_t time_to_sleep
время автоотключения
Definition: global.h:88
#define BAND_MF
битовые маски частот спектра, объединенные в СЧ-полосу
Definition: global.h:60
Управление питанием
Аппаратно-зависимые определения
uint32_t get_ir_code(void)
получение кода ДУ
Definition: ir_remote.c:114
uint16_t band_mask[BAND_CNT]
битовые маски полос
Definition: global.h:93
uint8_t agc_enabled
АРУ включено
Definition: global.h:90
uint8_t pixels_in_group
количество пикселов в группе
Definition: global.h:87
Настройки цветомузыки
void lcd_puts(const char *s)
Display string without auto linefeed.
Definition: lcd.c:483
#define IR_NO_CMD
Заглушка "нет команды".
Definition: ir_remote.h:22
uint8_t group_of_pixels
количество групп пикселов
Definition: global.h:86
описания модуля дистанционного управления
описания урганов управления
uint32_t get_ir_cmd_code(control_cmd_t cmd)
получение IR-кода команды
Definition: ir_remote.c:156
меню
Definition: menu.h:102
void update_config(void)
обновление EEPROM с параметрами настройки
Definition: setup.c:87
void show_number(int32_t num)
вывод десятичного числа
Definition: lcd_show.c:100
uint8_t clear
признак чистых настроек
Definition: global.h:85