/** * Streams a message instead of getting the entire msg as a byte array * and then sending the bytes accross. */ void stream_msg(ScripterMsg* msg) { init_packet(); // send header put_slip_char(get_msg_id_most_repr(msg->id)); put_slip_char(get_msg_id_least_repr(msg->id)); put_slip_char(msg->param_count); // send parameters MsgParam* param = msg->parameters; while(param != NULL) { // size is 3 bytes, so we first get // rid of most representative byte if (param->type == PT_BYTE_STREAM) { int size = (*(param->data.data_stream.fp_get_size))(¶m->data.data_stream); put_slip_char(get_size_most_repr(size)); put_slip_char(get_size_middle_repr(size)); put_slip_char(get_size_least_repr(size)); char *c = NULL; while( (c =(*(param->data.data_stream.fp_get_char))(¶m->data.data_stream)) != NULL ) { put_slip_char(*c); } } else { put_slip_char(get_size_most_repr(param->data.data_blob.size)); put_slip_char(get_size_middle_repr(param->data.data_blob.size)); put_slip_char(get_size_least_repr(param->data.data_blob.size)); int i; for(i=0; i<(int)param->data.data_blob.size; i++) { put_slip_char(*(param->data.data_blob.data+i)); } } param = param->next_param; } finish_packet(); }
/** * Sends the picture currently in the pixbuf over serial */ int send_picture_msg(lua_State *_l) { uint32_t x, y; uint32_t size_x, size_y; // only send if we're in debug mode if (debug_on) { init_packet(); // First the header // first the ID we have to put it in two bytes // big endian put_slip_char(get_msg_id_most_repr(PPM_MSG_ID)); put_slip_char(get_msg_id_least_repr(PPM_MSG_ID)); // now the param count. 1 bytes put_slip_char(1); cc3_pixbuf_rewind(); // just in case uint8_t *row = cc3_malloc_rows(1); // now parameters size_x = cc3_g_pixbuf_frame.width; size_y = cc3_g_pixbuf_frame.height; char* head = malloc(20); sprintf(head,"P6\n%d %d\n255\n",size_x,size_y ); uint32_t size = strlen(head)+size_x*size_y*3; // size is 3 bytes, so we first get // rid of most representative byte put_slip_char(get_size_most_repr(size)); put_slip_char(get_size_middle_repr(size)); put_slip_char(get_size_least_repr(size)); put_slip_string(head); free(head); bool led_on = false; for (y = 0; y < size_y; y++) { cc3_pixbuf_read_rows(row, 1); cc3_led_set_state(0, led_on=!led_on); for (x = 0; x < size_x * 3U; x++) { /* Sleep to prevent byte-loss. Seems a bit extreme, but I saw problems with even 1000 and 500 as intervals. */ if (x % 100 == 0) { cc3_timer_wait_ms (5); } put_slip_char(row[x]); } } cc3_led_set_state(0, false); free(row); finish_packet(); } return 0; }
int message_to_bytes( ScripterMsg msg, uint_least8_t** msg_bytes ) { // First the header uint_least8_t *header = malloc(3); // first the ID we have to put it in two bytes // big endian header[0] = get_msg_id_most_repr(msg.id); header[1] = get_msg_id_least_repr(msg.id); // now the param count. 1 bytes header[2] = msg.param_count; // now parameters int param_bytes_count = 0; MsgParam* param = msg.parameters; while(param != NULL) { param_bytes_count += 3+param->data.data_blob.size; param = param->next_param; } // Now, we put everything together in on place: // header + each of the params + each of the param sizes uint_least8_t* final_bytes = malloc( 3 + param_bytes_count); // Let's get the 3 bytes for teh header in now final_bytes[0] = header[0]; final_bytes[1] = header[1]; final_bytes[2] = header[2]; free(header); int p_idx = 3; // parameters start at position 4 param = msg.parameters; while(param != NULL) { // size is 3 bytes, so we first get // rid of most representative byte final_bytes[p_idx++] = get_size_most_repr(param->data.data_blob.size); final_bytes[p_idx++] = get_size_middle_repr(param->data.data_blob.size); final_bytes[p_idx++] = get_size_least_repr(param->data.data_blob.size); int i; for(i=0; i<(int)param->data.data_blob.size; i++) { final_bytes[p_idx++] = *(param->data.data_blob.data+i); } param = param->next_param; } *msg_bytes = final_bytes; return 3+param_bytes_count; }