void CompassCalibrator::thin_samples() {
    if(_sample_buffer == nullptr) {
        return;
    }

    _samples_thinned = 0;
    // shuffle the samples http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
    // this is so that adjacent samples don't get sequentially eliminated
    for(uint16_t i=_samples_collected-1; i>=1; i--) {
        uint16_t j = get_random16() % (i+1);
        CompassSample temp = _sample_buffer[i];
        _sample_buffer[i] = _sample_buffer[j];
        _sample_buffer[j] = temp;
    }

    for(uint16_t i=0; i < _samples_collected; i++) {
        if(!accept_sample(_sample_buffer[i])) {
            _sample_buffer[i] = _sample_buffer[_samples_collected-1];
            _samples_collected --;
            _samples_thinned ++;
        }
    }

    update_completion_mask();
}
Beispiel #2
0
int http_handshake(int fd, const char *host, const char *service,
                   const char *path, std::string& body)
{
  char buf[4096];
  std::string client_key = base64(get_random16());
  snprintf(buf, sizeof(buf),
           "GET %s HTTP/1.1\r\n"
           "Host: %s:%s\r\n"
           "Upgrade: websocket\r\n"
           "Connection: Upgrade\r\n"
           "Sec-WebSocket-Key: %s\r\n"
           "Sec-WebSocket-Version: 13\r\n"
           "\r\n",
           path, host, service, client_key.c_str());
  std::string reqheader = buf;
  if(send_http_handshake(fd, reqheader) == -1) {
    return -1;
  }
  std::string resheader;
  if(recv_http_handshake(fd, resheader) == -1) {
    return -1;
  }
  std::string::size_type keyhdstart;
  if((keyhdstart = resheader.find("Sec-WebSocket-Accept: ")) ==
     std::string::npos) {
    std::cerr << "http_upgrade: missing required headers" << std::endl;
    return -1;
  }
  keyhdstart += 22;
  std::string::size_type keyhdend = resheader.find("\r\n", keyhdstart);
  std::string accept_key = resheader.substr(keyhdstart, keyhdend-keyhdstart);
  if(accept_key == create_acceptkey(client_key)) {
    body = resheader.substr(resheader.find("\r\n\r\n")+4);
    return 0;
  } else {
    return -1;
  }
}
Beispiel #3
0
// generate a random float between -1 and 1
static float rand_num(void)
{
    return ((2.0f * get_random16()) / 0xFFFF) - 1.0f;
}