4545
4646static struct bt_gatt_ccc_cfg blvl_ccc_cfg [CONFIG_BLUETOOTH_MAX_PAIRED ] = {};
4747static uint8_t simulate_blvl ;
48- static uint8_t temperature = 100 ;
49- static uint8_t rgb [3 ] = {0xff , 0x01 , 0x02 }; // red
48+ static uint8_t temperature = 20 ;
49+ static uint8_t rgb [3 ] = {0xff , 0x00 , 0x00 }; // red
5050
5151struct device * pwm , * tmp36 ;
5252#define IO3_RED 0
@@ -55,82 +55,121 @@ struct device *pwm, *tmp36;
5555
5656#define ADC_DEVICE_NAME "ADC_0"
5757
58+ #define SLEEPTICKS SECONDS(1)
59+ /* about 2 ms, thus 500Hz */
60+ #define PERIOD 64000
61+ #define MAX (a ,b ) (((a)>(b))?(a):(b))
62+
63+ void pwm_write (void )
64+ {
65+ uint32_t r_on , g_on , b_on ;
66+ uint32_t r_off , g_off , b_off ;
67+
68+ r_on = (rgb [0 ] / 255.0 ) * PERIOD ;
69+ g_on = (rgb [1 ] / 255.0 ) * PERIOD ;
70+ b_on = (rgb [2 ] / 255.0 ) * PERIOD ;
71+
72+ r_on = MAX (1 , r_on );
73+ g_on = MAX (1 , g_on );
74+ b_on = MAX (1 , b_on );
75+
76+ r_off = PERIOD - r_on ;
77+ g_off = PERIOD - g_on ;
78+ b_off = PERIOD - b_on ;
79+
80+ r_off = MAX (1 , r_off );
81+ g_off = MAX (1 , g_off );
82+ b_off = MAX (1 , b_off );
83+
84+ printk ("rgb: %x %x %x\n" , rgb [0 ], rgb [1 ], rgb [2 ]);
85+ printk ("on : %x %x %x\n" , r_on , g_on , b_on );
86+ printk ("off: %x %x %x\n" , r_off , g_off , b_off );
87+
88+ pwm_pin_set_values (pwm , IO3_RED , r_on , r_off );
89+ pwm_pin_set_values (pwm , IO5_GREEN , g_on , g_off );
90+ pwm_pin_set_values (pwm , IO6_BLUE , b_on , b_off );
91+ }
92+
5893static void blvl_ccc_cfg_changed (uint16_t value )
5994{
60- simulate_blvl = (value == BT_GATT_CCC_NOTIFY ) ? 1 : 0 ;
95+ simulate_blvl = (value == BT_GATT_CCC_NOTIFY ) ? 1 : 0 ;
6196}
6297
6398static ssize_t read_temperature (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
64- void * buf , uint16_t len , uint16_t offset )
99+ void * buf , uint16_t len , uint16_t offset )
65100{
66- const char * value = attr -> user_data ;
101+ const char * value = attr -> user_data ;
67102
68- return bt_gatt_attr_read (conn , attr , buf , len , offset , value ,
69- sizeof (* value ));
103+ return bt_gatt_attr_read (conn , attr , buf , len , offset , value ,
104+ sizeof (* value ));
70105}
71106
72107static ssize_t read_rgb (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
73- void * buf , uint16_t len , uint16_t offset )
108+ void * buf , uint16_t len , uint16_t offset )
74109{
75- memcpy (rgb , attr -> user_data , sizeof (rgb ));
110+ memcpy (rgb , attr -> user_data , sizeof (rgb ));
76111
77- printk ("read_rgb: rgb=%x %x %x size=%d %d\n" ,
78- (uint8_t )rgb [0 ], rgb [1 ], rgb [2 ], sizeof (rgb ), len );
112+ printk ("read_rgb: %x %x %x\n" , rgb [0 ], rgb [1 ], rgb [2 ]);
79113
80- return bt_gatt_attr_read (conn , attr , buf , len , offset , rgb ,
81- sizeof (rgb ));
114+ return bt_gatt_attr_read (conn , attr , buf , len , offset , rgb ,
115+ sizeof (rgb ));
82116}
83117
84118static ssize_t write_rgb (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
85- const void * buf , uint16_t len , uint16_t offset )
119+ const void * buf , uint16_t len , uint16_t offset )
86120{
87- memcpy (rgb , buf , sizeof (rgb ));
121+ memcpy (rgb , buf , sizeof (rgb ));
122+
123+ printk ("write_rgb: %x %x %x\n" , rgb [0 ], rgb [1 ], rgb [2 ]);
88124
89- printk ("write_rgb: rgb=%x %x %x size=%d %d\n" ,
90- (uint8_t )rgb [0 ], rgb [1 ], rgb [2 ], sizeof (rgb ), len );
125+ pwm_write ();
91126
92- return sizeof (rgb );
127+ return sizeof (rgb );
93128}
94129
95130/* WebBT Service Declaration */
96131static struct bt_gatt_attr attrs [] = {
97- BT_GATT_PRIMARY_SERVICE (BT_UUID_WEBBT ),
98-
99- /* Temperature */
100- BT_GATT_CHARACTERISTIC (BT_UUID_TEMP ,
101- BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY ),
102- BT_GATT_DESCRIPTOR (BT_UUID_TEMP , BT_GATT_PERM_READ ,
103- read_temperature , NULL , & temperature ),
104- BT_GATT_CUD (SENSOR_1_NAME , BT_GATT_PERM_READ ),
105- BT_GATT_CCC (blvl_ccc_cfg , blvl_ccc_cfg_changed ),
106-
107- /* RGB Led */
108- BT_GATT_CHARACTERISTIC (BT_UUID_RGB ,
109- BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE ),
110- BT_GATT_DESCRIPTOR (BT_UUID_RGB , BT_GATT_PERM_READ | BT_GATT_PERM_WRITE ,
111- read_rgb , write_rgb , rgb ),
112- BT_GATT_CUD (SENSOR_2_NAME , BT_GATT_PERM_READ ),
132+ BT_GATT_PRIMARY_SERVICE (BT_UUID_WEBBT ),
133+
134+ /* Temperature */
135+ BT_GATT_CHARACTERISTIC (BT_UUID_TEMP ,
136+ BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY ),
137+ BT_GATT_DESCRIPTOR (BT_UUID_TEMP , BT_GATT_PERM_READ ,
138+ read_temperature , NULL , & temperature ),
139+ BT_GATT_CUD (SENSOR_1_NAME , BT_GATT_PERM_READ ),
140+ BT_GATT_CCC (blvl_ccc_cfg , blvl_ccc_cfg_changed ),
141+
142+ /* RGB Led */
143+ BT_GATT_CHARACTERISTIC (BT_UUID_RGB ,
144+ BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE ),
145+ BT_GATT_DESCRIPTOR (BT_UUID_RGB , BT_GATT_PERM_READ | BT_GATT_PERM_WRITE ,
146+ read_rgb , write_rgb , rgb ),
147+ BT_GATT_CUD (SENSOR_2_NAME , BT_GATT_PERM_READ ),
113148};
114149
115150void service_init (void )
116151{
117- if ((pwm = device_get_binding ("PWM" )) == NULL )
118- printk ("device_get_binding: failed for PWM\n" );
119-
120- bt_gatt_register (attrs , ARRAY_SIZE (attrs ));
152+ if ((pwm = device_get_binding ("PWM" )) == NULL )
153+ printk ("device_get_binding: failed for PWM\n" );
154+ else {
155+ pwm_write ();
156+ printk ("PWM init OK\n" );
157+ }
158+
159+ bt_gatt_register (attrs , ARRAY_SIZE (attrs ));
121160}
122161
123162void service_notify (void )
124163{
125- if (!simulate_blvl ) {
126- return ;
127- }
164+ if (!simulate_blvl ) {
165+ return ;
166+ }
128167
129- temperature -- ;
130- if (!temperature ) {
131- /* Software eco temperature charger */
132- temperature = 100 ;
133- }
168+ temperature -- ;
169+ if (!temperature ) {
170+ /* Software eco temperature charger */
171+ temperature = 20 ;
172+ }
134173
135- bt_gatt_notify (NULL , & attrs [2 ], & temperature , sizeof (temperature ));
174+ bt_gatt_notify (NULL , & attrs [2 ], & temperature , sizeof (temperature ));
136175}
0 commit comments