@@ -109,7 +109,9 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
109109 // Add Characteristic
110110 _data.setProperties (CHR_PROPS_WRITE);
111111 _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 */ );
113115 VERIFY_STATUS ( _data.begin () );
114116
115117 // Add Characteristic
@@ -124,28 +126,36 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
124126 return ERROR_NONE;
125127}
126128
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)
131130{
132131 (void ) conn_hdl;
133132
134133 if (len < 3 ) return ;
135134
136- BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService ();
137-
138135 uint16_t index;
139136 memcpy (&index, data, 2 );
140137 uint8_t flag = data[2 ];
141138
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);
143147
144- memcpy (buffer , data+3 , len- 3 );
148+ if (copied_count) memcpy (pixbuf , data+3 , copied_count );
145149
146150 // 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);
151161}
0 commit comments