bool senselOpenConnection(char* com_port) { if(senselSerialOpen(&serial_data, com_port)) { frame_buffer = (uint8*)malloc(FRAME_BUFFER_INITIAL_CAPACITY*sizeof(uint8)); frame_buffer_capacity = FRAME_BUFFER_INITIAL_CAPACITY*sizeof(uint8); uint8 buf[2]; //Read in max X and max Y values _readReg(0x10, 2, buf); sensor_max_x = (256 * (buf[0] - 1)); sensor_max_y = (256 * (buf[1] - 1)); uint32 sensor_width_um; uint32 sensor_height_um; //Read in active area dimensions _readReg(SENSEL_REG_SENSOR_ACTIVE_AREA_WIDTH_UM, 4, (uint8 *)&sensor_width_um); _readReg(SENSEL_REG_SENSOR_ACTIVE_AREA_HEIGHT_UM, 4, (uint8 *)&sensor_height_um); sensor_width_mm = sensor_width_um / 1000.0; sensor_height_mm = sensor_height_um / 1000.0; sensor_x_to_mm_factor = sensor_width_mm / sensor_max_x; sensor_y_to_mm_factor = sensor_height_mm / sensor_max_y; return true; } return false; }
//note this has crc bytes embedded, per datasheet, so provide 12 byte buf int SI7021::getSerialBytes(byte * buf) { _writeReg(SERIAL1_READ, sizeof SERIAL1_READ); _readReg(buf, 6); _writeReg(SERIAL2_READ, sizeof SERIAL2_READ); _readReg(buf + 6, 6); // could verify crc here and return only the 8 bytes that matter return 1; }
//note this has crc bytes embedded, per datasheet, so provide 12 byte buf int SI7021::getUIDBytes(byte * buf) { _writeReg(UID1_READ, sizeof UID1_READ); _readReg(buf, 6); _writeReg(UID2_READ, sizeof UID2_READ); _readReg(buf + 6, 6); // could verify crc here and return only the 8 bytes that matter return 1; }
void SI7021::_command(byte cmd, byte * buf ) { _writeReg(&cmd, sizeof cmd); #if defined(ARDUINO_ARCH_ESP8266) delay(25); #endif _readReg(buf, 2); }
void cSI7021::_command(uint8_t cmd, uint8_t * buf ) { _writeReg(&cmd, 1); #if defined(ARDUINO_ARCH_ESP8266) delay(25); #endif _readReg(buf, 2); }
// Returns (x,y,z) acceleration in G's using the following coordinate system: // // --------------------------- // / Z /\ _ / // / | /| Y / // / | / / // / |/ / // / -----> X / // / / // ---------------------------- // // Assumes accelerometer is configured to the default +/- 2G range bool senselReadAccelerometerData(accel_data_t *accel_data) { accel_data_raw_t raw_data; // Read accelerometer data bytes for X, Y and Z if(!_readReg(SENSEL_REG_ACCEL_X, sizeof(accel_data_raw_t), (uint8 *)&raw_data)) return false; // Rescale to G's (at a range of +/- 2G, accelerometer returns 0x4000 for 1G acceleration) accel_data->x = ((float)raw_data.x) / 0x4000; accel_data->y = ((float)raw_data.y) / 0x4000; accel_data->z = ((float)raw_data.z) / 0x4000; return true; }
bool senselSerialOpen2(sensel_serial_data * data, char* file_name) { char magic[7]; magic[6] = '\0'; printf("Opening %s...", file_name); data->serial_fd = open(file_name, O_RDWR | O_NONBLOCK | O_NOCTTY); if(data->serial_fd == -1) { printf("unable to open!\n"); return false; } struct termios options; tcgetattr(data->serial_fd, &options); cfmakeraw(&options); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); tcsetattr(data->serial_fd, TCSANOW, &options); senselSerialFlushInput(data); if(_readReg(0x00, SENSEL_MAGIC_LEN, (uint8*)magic)) { printf("Magic: %s\n", magic); if(strcmp(magic, SENSEL_MAGIC) == 0) { printf("Found sensor!\n"); return true; } else { printf("Invalid magic!\n"); } } else { printf("Timeout on read!\n"); } close(data->serial_fd); data->serial_fd = -1; return false; }
int readReg(uint8_t address, uint8_t &val) { return _readReg(address, val); }
void SI7021::_command(byte cmd, byte * buf ) { _writeReg(&cmd, sizeof cmd); delay(25); //for ESP8266 _readReg(buf, 3); // 1 - data, 2 - data, 3 - CRC }