Ejemplo n.º 1
0
void *send_msg()
{
  fprintf(stderr, "Bluegiga comms thread started\n");
  uint8_t device = 0, bt_msg_len = 0;
  uint16_t diff = 0;
  while (state != state_finish) {
    if (action == action_broadcast) {
      diff = (insert_idx[0] - extract_idx[0] + BUF_SIZE) % BUF_SIZE;
      if (diff) {
        ble_cmd_gap_end_procedure();
        bt_msg_len = diff < 31 ? diff : 31;
        //ble_cmd_attclient_attribute_write(device, drone_handle_measurement, bt_msg_len[device], &data_buf[device][extract_idx[device]]);

        ble_cmd_gap_set_adv_data(0, bt_msg_len, &data_buf[0][extract_idx[0]]);
        ble_cmd_gap_set_mode(gap_user_data, gap_non_connectable);    //gap_set_mode($84, gap_scannable_non_connectable)
        extract_idx[device] = (extract_idx[device] + bt_msg_len) % BUF_SIZE;

        usleep(10000); // advertisement interval set at 320ms so pause for shorter before turning off
        ble_cmd_gap_set_mode(0, 0);   // stop advertising

        ble_cmd_gap_discover(gap_discover_observation);   // return to listening
        usleep(5000);
      }
    } else {
      device = 0;
      while (ac_id[device] != -1 && device < 8) {
        diff = (insert_idx[device] - extract_idx[device] + BUF_SIZE) % BUF_SIZE;
        if (diff) {
          bt_msg_len = diff < 19 ? diff : 19; // msg length in max 20 but one header byte added on bluegiga to lisa
          //if (bt_msg_len > 18)
          //  fprintf(stderr,"Long msg: %d, buff size: %d\n", bt_msg_len, diff);
          //ble_cmd_attclient_attribute_write(device, drone_handle_measurement, bt_msg_len[device], &data_buf[device][extract_idx[device]]);

          uint16_t i = 0;
          unsigned char buf[19];
          for (i = 0; i < bt_msg_len; i++)
          {
            buf[i] = data_buf[device][extract_idx[device]];
            extract_idx[device] = (extract_idx[device] + 1) % BUF_SIZE;
          }

          ble_cmd_attclient_write_command(device, drone_handle_measurement, bt_msg_len, buf);
        }
        device++;
      } // next device
      usleep(connection_interval * 1000 * 1.5); // send messages at max intervals of the connection interval, 2 safety factor
    } // repeat
  }
  pthread_exit(NULL);
}
Ejemplo n.º 2
0
void *send_msg()
{
  fprintf(stderr,"Bluegiga comms thread started\n");
  uint8_t device = 0, bt_msg_len = 0;
  uint16_t diff = 0;
  while (state != state_finish) {
    if (action == action_broadcast) {
      diff = (insert_idx[0] - extract_idx[0] + 1024) % 1024;
      if (diff) {
        ble_cmd_gap_end_procedure();
        bt_msg_len = diff < 31 ? diff : 31;
        //ble_cmd_attclient_attribute_write(device, drone_handle_measurement, bt_msg_len[device], &data_buf[device][extract_idx[device]]);

        ble_cmd_gap_set_adv_data(0, bt_msg_len, &data_buf[0][extract_idx[0]]);
        ble_cmd_gap_set_mode(gap_user_data, gap_non_connectable);    //gap_set_mode($84, gap_scannable_non_connectable)
        extract_idx[device] = (extract_idx[device] + bt_msg_len) % 1024;

        usleep(1000); // advertisement interval set at 320ms so pause for shorter before turning off
        ble_cmd_gap_set_mode(0, 0);   // stop advertising

        ble_cmd_gap_discover(gap_discover_observation);   // return to listening
        usleep(5000);
      }
    } else {
      device = 0;
      while (ac_id[device] != -1 && device < 8) {
        diff = (insert_idx[device] - extract_idx[device] + 1024) % 1024;
        if (diff) {
          bt_msg_len = diff < 27 ? diff : 27;
          //if (bt_msg_len > 18)
          //  fprintf(stderr,"Long msg: %d, buff size: %d\n", bt_msg_len, diff);
          //ble_cmd_attclient_attribute_write(device, drone_handle_measurement, bt_msg_len[device], &data_buf[device][extract_idx[device]]);

          ble_cmd_attclient_write_command(device, drone_handle_measurement, bt_msg_len, &data_buf[device][extract_idx[device]]);
          extract_idx[device] = (extract_idx[device] + bt_msg_len) % 1024;
        }
        device++;
        usleep(10000);  // ~100Hz, max spi speed on lisa
      }
    }
  }
  pthread_exit(NULL);
}