@@ -109,7 +109,9 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
109
109
// Add Characteristic
110
110
_data.setProperties (CHR_PROPS_WRITE);
111
111
_data.setPermission (SECMODE_NO_ACCESS, SECMODE_OPEN);
112
- _data.setMaxLen (3 + bufsize); // start (2 byte) + flags (1 byte)
112
+ // start (2 byte) + flags (1 byte)
113
+ // TODO: for backward compatible with current CPB app, force to at least 10 pixels
114
+ _data.setMaxLen (3 + 30 /* bufsize */ );
113
115
VERIFY_STATUS ( _data.begin () );
114
116
115
117
// Add Characteristic
@@ -124,28 +126,36 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
124
126
return ERROR_NONE;
125
127
}
126
128
127
- // --------------------------------------------------------------------+
128
- // Static callbacks
129
- // --------------------------------------------------------------------+
130
- void BLEAdafruitAddressablePixel::pixel_data_write_cb (uint16_t conn_hdl, BLECharacteristic* chr, uint8_t * data, uint16_t len)
129
+ void BLEAdafruitAddressablePixel::_pixel_write_handler (uint16_t conn_hdl, uint8_t * data, uint16_t len)
131
130
{
132
131
(void ) conn_hdl;
133
132
134
133
if (len < 3 ) return ;
135
134
136
- BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService ();
137
-
138
135
uint16_t index;
139
136
memcpy (&index, data, 2 );
140
137
uint8_t flag = data[2 ];
141
138
142
- uint8_t * buffer = svc._neo ->getPixels () + index;
139
+ // pixel staring buffer
140
+ uint8_t * pixbuf = _neo->getPixels () + index;
141
+
142
+ // limit copied bytes up to strip's boundary
143
+ uint16_t copied_count = max16 (_bufsize.read16 (), index) - index;
144
+ copied_count = min16 (len-3 , copied_count);
145
+
146
+ PRINT_INT (copied_count);
143
147
144
- memcpy (buffer , data+3 , len- 3 );
148
+ if (copied_count) memcpy (pixbuf , data+3 , copied_count );
145
149
146
150
// show flag
147
- if ( flag & 0x01 )
148
- {
149
- svc._neo ->show ();
150
- }
151
+ if ( flag & 0x01 ) _neo->show ();
152
+ }
153
+
154
+ // --------------------------------------------------------------------+
155
+ // Static callbacks
156
+ // --------------------------------------------------------------------+
157
+ void BLEAdafruitAddressablePixel::pixel_data_write_cb (uint16_t conn_hdl, BLECharacteristic* chr, uint8_t * data, uint16_t len)
158
+ {
159
+ BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService ();
160
+ svc._pixel_write_handler (conn_hdl, data, len);
151
161
}
0 commit comments