static int mraa_grovepi_read_internal(int function, int pin) { uint8_t data[5]; uint8_t result[3]; data[0] = GROVEPI_REGISTER; data[1] = function; data[2] = pin; data[3] = 0; data[4] = 0; if (mraa_i2c_write(grovepi_bus, data, 5) != MRAA_SUCCESS) { syslog(LOG_WARNING, "grovepi: failed to write command to i2c bus /dev/i2c-%d", grovepi_bus->busnum); return -1; } if (mraa_i2c_write_byte(grovepi_bus, 1) != MRAA_SUCCESS) { syslog(LOG_WARNING, "grovepi: failed to write to i2c bus /dev/i2c-%d", grovepi_bus->busnum); return -1; } if (function == GROVEPI_GPIO_READ) { if (mraa_i2c_read(grovepi_bus, result, 1) != 1) { syslog(LOG_WARNING, "grovepi: failed to read result from i2c bus /dev/i2c-%d", grovepi_bus->busnum); return -1; } return result[0]; } if (function == GROVEPI_AIO_READ) { if (mraa_i2c_read(grovepi_bus, result, 3) != 3) { syslog(LOG_WARNING, "grovepi: failed to read result from i2c bus /dev/i2c-%d", grovepi_bus->busnum); return -1; } return (result[1] << 8) | result [2]; } return -1; }
atlsci_status_ret_t atlsci_status(int sensor_type) { int ret = -1; uint8_t rx_buffer[STRING_SIZE] = {0}; char * status_msg = "STATUS"; int success = 0; ret = set_sensor_address(i2c_context, sensor_type); if (ret == MRAA_SUCCESS) { ret = send_string(i2c_context, status_msg); if (ret == MRAA_SUCCESS) { ret = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); success = ATLSCI_SUCCESS_BYTE_INDICATOR; } else { log_mraa_response("atlsci_status()", ret); } } else { log_mraa_response("atlsci_status()", ret); } return parse_atlsci_status(rx_buffer, success); }
/* * Run the read command 'r' to read the data obtained from the sensor * * Return message format: * For ORP, pH, D.O.: * 1 DATA null * where DATA represents many bytes. * For Conductivity: * 1 EC , TDS, SAL , SG null * where EC, TDS, SAL, SG represent many bytes */ atlsci_read_ret_t atlsci_read(int sensor_type) { int ret = -1; uint8_t rx_buffer[STRING_SIZE] = {0}; ret = set_sensor_address(i2c_context, sensor_type); if (ret == MRAA_SUCCESS) { ret = send_string(i2c_context, "r"); if (ret == MRAA_SUCCESS) { ret = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); return parse_atlsci_read(sensor_type, rx_buffer); } else { log_mraa_response("atlsci_read()", ret); } } else { log_mraa_response("atlsci_read()", ret); } return parse_atlsci_read_fail(); }
atlsci_led_ret_t atlsci_led(int sensor_type, int action) { char * led_on = "L,1"; //Turn on LEDs char * led_off = "L,0"; //Turn off LEDs char * led_query = "L,?"; //Query LEDs uint8_t rx_buffer[STRING_SIZE] = {0}; int ret = set_sensor_address(i2c_context, sensor_type); switch (action) { case ATLSCI_LED_ON: ret = send_string(i2c_context, led_on); break; case ATLSCI_LED_OFF: ret = send_string(i2c_context, led_off); break; case ATLSCI_LED_QUERY: ret = send_string(i2c_context, led_query); break; default: ret = -1; break; } if (ret == MRAA_SUCCESS) { ret = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(action == ATLSCI_LED_QUERY) printf("%s\n", rx_buffer); return parse_atlsci_led(rx_buffer, action); } log_mraa_response("atlsci_led()", ret); return parse_atlsci_led(rx_buffer, action); }
/* * Temperature compensation is only enable for pH, DO, and Conductivity sensor. * ORP does not have this feature. The value that it takes is expressed in float value nn.n */ atlsci_temp_comp_ret_t atlsci_temp_comp(int sensor_type, atlsci_temp_comp_param param, float value) { atlsci_temp_comp_ret_t ret; uint8_t rx_buffer[STRING_SIZE] = {0}; char cal_temp[TEMP_COMP_SIZE] = "T,"; int success = set_sensor_address(i2c_context, sensor_type); char *pt_cal = cal_temp; switch(param) { case SET_TEMPERATURE: sprintf(pt_cal + TEMP_COMP_INDEX, "%.1f", value); success = send_string(i2c_context, cal_temp); break; case QUERY_TEMPERATURE: success = send_string (i2c_context, cal_query); break; default: return atlsci_temp_comp_fail(param); } if (success == MRAA_SUCCESS) { success = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(param == QUERY_TEMPERATURE) printf("%s\n", rx_buffer); //First bit of rx_buffer will be '1' if write is successful ret.success = (int)rx_buffer[0]; } else { log_mraa_response("atlsci_temp_comp()", success); ret.success = -1; } return ret; }
mraa_result_t mag_update() { mraa_result_t result; int bytesRead; result = mraa_i2c_write_byte(mag_context, HMC5883L_DATA_REG); if (result != MRAA_SUCCESS) { printError("unable to write to compass (9)"); return result; } bytesRead = mraa_i2c_read(mag_context, mag_rx_tx_buf, DATA_REG_SIZE); if (bytesRead != DATA_REG_SIZE) { printError("unable to read from compass (10)"); return result; } // x mag_coor[0] = (mag_rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | mag_rx_tx_buf[HMC5883L_X_LSB_REG]; // z mag_coor[2] = (mag_rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | mag_rx_tx_buf[HMC5883L_Z_LSB_REG]; // y mag_coor[1] = (mag_rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | mag_rx_tx_buf[HMC5883L_Y_LSB_REG]; save_log_value(RAW_COMPASS_X, mag_coor[0], 1); save_log_value(RAW_COMPASS_Y, mag_coor[1], 1); save_log_value(RAW_COMPASS_Z, mag_coor[2], 1); save_log_value(HEADING, mag_heading(), 1); save_log_value(DIRECTION, mag_direction(), 1); return MRAA_SUCCESS; }
mrb_value mrb_mraa_i2c_read(mrb_state *mrb, mrb_value self){ mraa_i2c_context i2c; mrb_int length; uint8_t *rbuf; mrb_int num_of_read; int ai, i; mrb_value mrv_rbuf; Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c); mrb_get_args(mrb, "i", &length); rbuf = (uint8_t*)mrb_malloc(mrb, sizeof(uint8_t) * length); memset(rbuf, 0, sizeof(uint8_t) * length); num_of_read = mraa_i2c_read(i2c, rbuf, length); mrv_rbuf = mrb_ary_new_capa(mrb, num_of_read); ai = mrb_gc_arena_save(mrb); for (i = 0; i < num_of_read; i++){ mrb_ary_push(mrb, mrv_rbuf, mrb_fixnum_value(rbuf[i])); mrb_gc_arena_restore(mrb, ai); } mrb_free(mrb, rbuf); return mrb_assoc_new(mrb, mrv_rbuf, mrb_fixnum_value(num_of_read)); }
static atlsci_cal_ret_t atlsci_cal_conductivity(atlsci_cal_param_t param, int value) { atlsci_cal_ret_t ret; int success = set_sensor_address(i2c_context, ATLSCI_CONDUCTIVITY); uint8_t rx_buffer[STRING_SIZE] = {0}; char cal_dry[CONDUCTIVITY_CAL_DRY_SIZE] = "Cal,dry"; char cal_one[CONDUCTIVITY_CAL_ONE_SIZE] = "Cal,one,"; char cal_low[CONDUCTIVITY_CAL_LOW_SIZE] = "Cal,low,"; char cal_high[CONDUCTIVITY_CAL_HIGH_SIZE] = "Cal,high,"; char *pt_cal = NULL; switch(param.cal_conductivity) { case CONDUCTIVITY_CALIBRATION_CLEAR: success = send_string(i2c_context, cal_clear); break; case CONDUCTIVITY_CALIBRATION_QUERY: success = send_string(i2c_context, cal_query); break; case CONDUCTIVITY_DRY_CALIBRATION: success = send_string(i2c_context, cal_dry); break; case CONDUCTIVITY_SINGLE_CALIBRATION_POINT: pt_cal = cal_one; sprintf((pt_cal + CONDUCTIVITY_INDEX_ONE), "%d", value); //set value to end of string success = send_string(i2c_context, cal_one); break; case CONDUCTIVITY_LOW_DUAL_CALIBRATION_POINT: pt_cal = cal_low; sprintf((pt_cal + CONDUCTIVITY_INDEX_LOW), "%d", value); success = send_string(i2c_context, cal_low); break; case CONDUCTIVITY_HIGH_DUAL_CALIBRATION_POINT: pt_cal = cal_high; sprintf((pt_cal + CONDUCTIVITY_INDEX_HIGH), "%d", value); success = send_string(i2c_context, cal_high); break; default: return atlsci_cal_fail(param); } if (success == MRAA_SUCCESS) { success = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(param.cal_conductivity == CONDUCTIVITY_CALIBRATION_QUERY) { ret.query = malloc(QUERY_SIZE); uint8_t * rx_ptr = rx_buffer; rx_ptr++; sprintf(ret.query, "%s", rx_ptr); //ret.query = rx_buffer; } //printf("%s\n", rx_buffer); //First bit of rx_buffer will be '1' if write is successful ret.success = (int)rx_buffer[0]; } else { log_mraa_response("atlsci_cal_conductivity()", success); ret.success = -1; } return ret; }
static atlsci_cal_ret_t atlsci_cal_ph(atlsci_cal_param_t param, float value) { atlsci_cal_ret_t ret; int success = set_sensor_address(i2c_context, ATLSCI_PH); uint8_t rx_buffer[STRING_SIZE] = {0}; char cal_low[PH_CAL_LOW_SIZE] = "Cal,low,"; char cal_mid[PH_CAL_MID_SIZE] = "Cal,mid,"; char cal_high[PH_CAL_HIGH_SIZE] = "Cal,high"; char *pt_cal = NULL; switch(param.cal_ph) { case PH_CALIBRATION_CLEAR: success = send_string(i2c_context, cal_clear); break; case PH_CALIBRATION_QUERY: success = send_string(i2c_context, cal_query); break; case PH_LOW_CALIBRATION_POINT: pt_cal = cal_low; sprintf(pt_cal + PH_INDEX_LOW, "%.2f", value); success = send_string(i2c_context, cal_low); break; case PH_MID_CALIBRATION_POINT: pt_cal = cal_mid; sprintf((pt_cal + PH_INDEX_MID), "%.2f", value); success = send_string(i2c_context, cal_mid); break; case PH_HIGH_CALIBRATION_POINT: pt_cal = cal_high; sprintf((pt_cal + PH_INDEX_HIGH), "%.2f", value); success = send_string(i2c_context, cal_high); break; default: return atlsci_cal_fail(param); } if (success == MRAA_SUCCESS) { success = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(param.cal_ph == PH_CALIBRATION_QUERY) { ret.query = malloc(QUERY_SIZE); //ret.query = rx_buffer; uint8_t * rx_ptr = rx_buffer; rx_ptr++; sprintf(ret.query, "%s", rx_ptr); } //First bit of rx_buffer will be '1' if write is successful ret.success = (int)rx_buffer[0]; } else { log_mraa_response("atlsci_cal_ph()", success); ret.success = -1; } return ret; }
int main(int argc, char **argv) { mraa_init(); float direction = 0; int16_t x = 0, y = 0, z = 0; char rx_tx_buf[MAX_BUFFER_LENGTH]; //! [Interesting] mraa_i2c_context i2c; i2c = mraa_i2c_init(0); mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); rx_tx_buf[0] = HMC5883L_CONF_REG_B; rx_tx_buf[1] = GA_1_3_REG; mraa_i2c_write(i2c, rx_tx_buf, 2); //! [Interesting] mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); rx_tx_buf[0] = HMC5883L_MODE_REG; rx_tx_buf[1] = HMC5883L_CONT_MODE; mraa_i2c_write(i2c, rx_tx_buf, 2); for(;;) { mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG); mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE); x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ; z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ; y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Y_LSB_REG] ; //scale and calculate direction direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG); //check if the signs are reversed if (direction < 0) direction += 2 * M_PI; printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG, z * SCALE_0_92_MG) ; printf("Heading : %f\n", direction * 180/M_PI) ; } }
static atlsci_cal_ret_t atlsci_cal_orp(atlsci_cal_param_t param, int value) { atlsci_cal_ret_t ret; uint8_t rx_buffer[STRING_SIZE] = {0}; char cal_single[ORP_CAL_SIZE] = "Cal,"; int success = set_sensor_address(i2c_context, ATLSCI_ORP); char *pt_cal = cal_single; switch(param.cal_orp) { case ORP_CALIBRATION_CLEAR: success = send_string(i2c_context, cal_clear); break; case ORP_CALIBRATION_QUERY: success = send_string(i2c_context, cal_query); break; case ORP_SINGLE_CALIBRATION_POINT: sprintf((pt_cal + ORP_INDEX), "%d", value); success = send_string(i2c_context, cal_single); break; default: return atlsci_cal_fail(param); } if (success == MRAA_SUCCESS) { success = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(param.cal_orp == ORP_CALIBRATION_QUERY) { ret.query = malloc(QUERY_SIZE); uint8_t * rx_ptr = rx_buffer; rx_ptr++; sprintf(ret.query, "%s", rx_ptr); //ret.query = rx_buffer; } //printf("%s\n", rx_buffer); //First bit of rx_buffer will be '1' if write is successful ret.success = (int)rx_buffer[0]; } else { log_mraa_response("atlsci_cal_orp()", success); ret.success = -1; } return ret; }
static atlsci_cal_ret_t atlsci_cal_dissolved_oxygen(atlsci_cal_param_t param) { atlsci_cal_ret_t ret; int success = set_sensor_address(i2c_context, ATLSCI_DO); uint8_t rx_buffer[STRING_SIZE] = {0}; char * cal_atmospheric_oxygen_level = "Cal"; char * cal_0 = "Cal,0"; switch(param.cal_do) { case DO_CALIBRATION_CLEAR: success = send_string(i2c_context, cal_clear); break; case DO_CALIBRATION_QUERY: success = send_string(i2c_context, cal_query); break; case DO_ATMOSPHERIC_CALIBRATION: success = send_string(i2c_context, cal_atmospheric_oxygen_level); break; case DO_ZERO_LEVEL_CALIBRATION: success = send_string(i2c_context, cal_0); break; default: return atlsci_cal_fail(param); } if (success == MRAA_SUCCESS) { success = mraa_i2c_read(i2c_context, rx_buffer, sizeof(rx_buffer)); if(param.cal_do == DO_CALIBRATION_QUERY) { ret.query = malloc(QUERY_SIZE); uint8_t * rx_ptr = rx_buffer; rx_ptr++; sprintf(ret.query, "%s", rx_ptr); //ret.query = (char *)rx_buffer; //ret.query = rx_buffer; } //printf("%s\n", rx_buffer); //First bit of rx_buffer will be '1' if write is successful ret.success = (int)rx_buffer[0]; } else { log_mraa_response("atlsci_cal_do()", success); ret.success = -1; } return ret; }
mraa_result_t gyro_update() { mraa_result_t result; result = mraa_i2c_write_byte(gyro_context, ITG3200_TEMP_H); if (result != MRAA_SUCCESS) { printError("unable to write to gyro (5)"); return result; } result = mraa_i2c_read(gyro_context, gyro_buffer, DATA_REG_SIZE); if (result != MRAA_SUCCESS) { printError("unable to read from gyro (6)"); return result; } //temp // gyro_temperature = (gyro_buffer[0] << 8 ) | gyro_buffer[1]; // x gyro_rotation[0] = ((gyro_buffer[2] << 8 ) | gyro_buffer[3]) + gyro_offsets[0]; // y gyro_rotation[1] = ((gyro_buffer[4] << 8 ) | gyro_buffer[5]) + gyro_offsets[1]; // z gyro_rotation[2] = ((gyro_buffer[6] << 8 ) | gyro_buffer[7]) + gyro_offsets[2]; save_log_value(RAW_TEMP, gyro_temperature, 1); save_log_value(TEMP, gyro_getTemperature(), 1); save_log_value(RAW_ANG_X, gyro_rotation[0], 1); save_log_value(RAW_ANG_Y, gyro_rotation[1], 1); save_log_value(RAW_ANG_Z, gyro_rotation[2], 1); float* ang = gyro_getRotation(); save_log_value(ANG_X, ang[0], 1); save_log_value(ANG_Y, ang[1], 1); save_log_value(ANG_Z, ang[2], 1); return MRAA_SUCCESS; }
mraa_result_t i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data) { mraa_result_t status = MRAA_SUCCESS; mraa_i2c_context i2c = mraa_i2c_init(bus); if (i2c == NULL) { return MRAA_ERROR_NO_RESOURCES; } status = mraa_i2c_address(i2c, device_address); if (status != MRAA_SUCCESS) { goto i2c_get_exit; } status = mraa_i2c_write_byte(i2c, register_address); if (status != MRAA_SUCCESS) { goto i2c_get_exit; } status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED; if (status != MRAA_SUCCESS) { goto i2c_get_exit; } i2c_get_exit: mraa_i2c_stop(i2c); return status; }
// I2C read helper static int readBytes(const ecezo_context dev, uint8_t *buffer, int len) { assert(dev != NULL); assert(dev->i2c != NULL); bool done = false; int rv; int retries = 10; while (!done && (retries-- > 0)) { if ((rv = mraa_i2c_read(dev->i2c, buffer, len)) < 0) { printf("%s: mraa_i2c_read(code) failed.\n", __FUNCTION__); return rv; } #if defined(ECEZO_DEBUG) printf("CODE: %02x\n", buffer[0]); #endif if (buffer[0] == 0xff || buffer[0] == 0x02) { // no data available, or error return -1; } else if (buffer[0] == 0x01) { // data is ready done = true; // now we need to move the data one byte down so the rest // of this driver can work as-is. memmove(buffer, (buffer + 1), len - 1); } else { // buffer[0] 0xfe - data is pending. wait and loop again. upm_delay_ms(CMD_DELAY); } } if (retries <= 0) { printf("%s: timed out waiting for correct response.\n", __FUNCTION__); return -1; } #if defined(ECEZO_DEBUG) printf("%s: Got %d bytes\n", __FUNCTION__, rv); for (int i=0; i<rv; i++) { printf("%02x (%c) ", buffer[i], isprint(buffer[i]) ? buffer[i] : '@'); } printf("\n"); #endif // ECEZO_DEBUG return rv; }
/** * Read length bytes from the bus into *data pointer * * @param data Data to read into * @param length Size of read in bytes to make * @return pointer to std::string */ int read(uint8_t *data, int length) { return mraa_i2c_read(m_i2c, data, length); }