@@ -24,95 +24,87 @@ static bool reset;
2424
2525static void light_default_var_init (void )
2626{
27- gen_def_trans_time_srv_user_data . tt = 0x00 ;
27+ ctl -> tt = 0x00 ;
2828
29- gen_power_onoff_srv_user_data . onpowerup = STATE_DEFAULT ;
29+ ctl -> onpowerup = STATE_DEFAULT ;
3030
31- light_lightness_srv_user_data . light_range_min = LIGHTNESS_MIN ;
32- light_lightness_srv_user_data . light_range_max = LIGHTNESS_MAX ;
33- light_lightness_srv_user_data . last = LIGHTNESS_MAX ;
34- light_lightness_srv_user_data . def = LIGHTNESS_MAX ;
31+ ctl -> light -> range_min = LIGHTNESS_MIN ;
32+ ctl -> light -> range_max = LIGHTNESS_MAX ;
33+ ctl -> light -> last = LIGHTNESS_MAX ;
34+ ctl -> light -> def = LIGHTNESS_MAX ;
3535
36- light_ctl_srv_user_data .temp_range_min = TEMP_MIN ;
37- light_ctl_srv_user_data .temp_range_max = TEMP_MAX ;
38- light_ctl_srv_user_data .lightness_def = LIGHTNESS_MAX ;
39- light_ctl_srv_user_data .temp_def = TEMP_MIN ;
36+ ctl -> temp -> range_min = TEMP_MIN ;
37+ ctl -> temp -> range_max = TEMP_MAX ;
38+ ctl -> temp -> def = TEMP_MAX ;
4039
41- light_ctl_srv_user_data .lightness_temp_last_tgt =
42- (u32_t ) ((LIGHTNESS_MAX << 16 ) | TEMP_MIN );
40+ ctl -> duv -> def = DELTA_UV_DEF ;
41+
42+ ctl -> light_temp_def = (u32_t ) ((LIGHTNESS_MAX << 16 ) | TEMP_MAX );
43+ ctl -> light_temp_last_tgt = (u32_t ) ((LIGHTNESS_MAX << 16 ) | TEMP_MAX );
4344}
4445
46+ /* This function should only get call after reading persistent data storage */
4547static void light_default_status_init (void )
4648{
47- /* Retrieve Default Lightness & Temperature Values */
48-
49- if (light_ctl_srv_user_data .lightness_temp_def ) {
50- light_ctl_srv_user_data .lightness_def = (u16_t )
51- (light_ctl_srv_user_data .lightness_temp_def >> 16 );
49+ u16_t light_def ;
5250
53- light_ctl_srv_user_data .temp_def = (u16_t )
54- (light_ctl_srv_user_data .lightness_temp_def );
51+ /* Retrieve Range of Lightness */
52+ if (ctl -> light -> range ) {
53+ ctl -> light -> range_max = (u16_t ) (ctl -> light -> range >> 16 );
54+ ctl -> light -> range_min = (u16_t ) ctl -> light -> range ;
5555 }
5656
57- light_lightness_srv_user_data .def =
58- light_ctl_srv_user_data .lightness_def ;
59-
60- light_ctl_srv_user_data .temp = light_ctl_srv_user_data .temp_def ;
61-
62- /* Retrieve Range of Lightness & Temperature */
63-
64- if (light_lightness_srv_user_data .lightness_range ) {
65- light_lightness_srv_user_data .light_range_max = (u16_t )
66- (light_lightness_srv_user_data .lightness_range >> 16 );
67-
68- light_lightness_srv_user_data .light_range_min = (u16_t )
69- (light_lightness_srv_user_data .lightness_range );
57+ /* Retrieve Range of Temperature */
58+ if (ctl -> temp -> range ) {
59+ ctl -> temp -> range_max = (u16_t ) (ctl -> temp -> range >> 16 );
60+ ctl -> temp -> range_min = (u16_t ) ctl -> temp -> range ;
7061 }
7162
72- if ( light_ctl_srv_user_data . temperature_range ) {
73- light_ctl_srv_user_data . temp_range_max = (u16_t )
74- ( light_ctl_srv_user_data . temperature_range >> 16 );
63+ /* Retrieve Default Lightness Value */
64+ light_def = (u16_t ) ( ctl -> light_temp_def >> 16 );
65+ ctl -> light -> def = constrain_lightness ( light_def );
7566
76- light_ctl_srv_user_data .temp_range_min = (u16_t )
77- (light_ctl_srv_user_data .temperature_range );
78- }
67+ /* Retrieve Default Temperature Value */
68+ ctl -> temp -> def = (u16_t ) ctl -> light_temp_def ;
69+
70+ ctl -> temp -> current = ctl -> temp -> def ;
71+ ctl -> duv -> current = ctl -> duv -> def ;
7972
80- switch (gen_power_onoff_srv_user_data . onpowerup ) {
73+ switch (ctl -> onpowerup ) {
8174 case STATE_OFF :
82- gen_onoff_srv_root_user_data .onoff = STATE_OFF ;
83- state_binding (ONOFF , ONOFF_TEMP );
75+ ctl -> light -> current = 0U ;
8476 break ;
8577 case STATE_DEFAULT :
86- gen_onoff_srv_root_user_data .onoff = STATE_ON ;
87- state_binding (ONOFF , ONOFF_TEMP );
78+ if (ctl -> light -> def == 0U ) {
79+ ctl -> light -> current = ctl -> light -> last ;
80+ } else {
81+ ctl -> light -> current = ctl -> light -> def ;
82+ }
8883 break ;
8984 case STATE_RESTORE :
90- light_ctl_srv_user_data .lightness =
91- (u16_t ) (light_ctl_srv_user_data .lightness_temp_last_tgt >> 16 );
92-
93- light_ctl_srv_user_data .temp =
94- (u16_t ) (light_ctl_srv_user_data .lightness_temp_last_tgt );
95-
96- state_binding (ONPOWERUP , ONOFF_TEMP );
85+ ctl -> light -> current = (u16_t ) (ctl -> light_temp_last_tgt >> 16 );
86+ ctl -> temp -> current = (u16_t ) ctl -> light_temp_last_tgt ;
9787 break ;
9888 }
9989
100- default_tt = gen_def_trans_time_srv_user_data . tt ;
90+ default_tt = ctl -> tt ;
10191
102- init_lightness_target_values ();
103- init_temp_target_values ();
92+ ctl -> light -> target = ctl -> light -> current ;
93+ ctl -> temp -> target = ctl -> temp -> current ;
94+ ctl -> duv -> target = ctl -> duv -> current ;
10495}
10596
10697void update_led_gpio (void )
10798{
10899 u8_t power , color ;
109100
110- power = 100 * ((float ) lightness / 65535 );
111- color = 100 * ((float ) (temperature + 32768 ) / 65535 );
101+ power = 100 * ((float ) ctl -> light -> current / 65535 );
102+ color = 100 * ((float ) (ctl -> temp -> current - ctl -> temp -> range_min ) /
103+ (ctl -> temp -> range_max - ctl -> temp -> range_min ));
112104
113105 printk ("power-> %d, color-> %d\n" , power , color );
114106
115- if (lightness ) {
107+ if (ctl -> light -> current ) {
116108 /* LED1 On */
117109 gpio_pin_write (led_device [0 ], DT_ALIAS_LED0_GPIOS_PIN , 0 );
118110 } else {
0 commit comments