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; }
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_write_byte(mrb_state *mrb, mrb_value self){ mraa_i2c_context i2c; mrb_int wdata; mraa_result_t result; Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c); mrb_get_args(mrb, "i", &wdata); result = mraa_i2c_write_byte(i2c, wdata & 0xFF); return mrb_fixnum_value(result); }
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 upm_result_t expandWrite(const lcm1602_context dev, uint8_t value) { assert(dev != NULL); // invalid for gpio if (!dev->isI2C) return UPM_ERROR_NO_RESOURCES; uint8_t buffer = value | dev->backlight; if (mraa_i2c_write_byte(dev->i2c, buffer)) { printf("%s: mraa_i2c_write_byte() failed\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } return UPM_SUCCESS; }
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; }
int main() { uint8_t event_type; int exit_code; mraa_init(); //Declaring opaque pointer to the internal struct_i2c mraa_i2c_context i2c; i2c = mraa_i2c_init(0); //If no i2c connection is detected if (i2c == NULL) { system("echo $(date) No I2C connection detected. >> log.txt"); return 0; } //Setting the i2c context address mraa_i2c_address(i2c, I2C_ADDRESS); //Reading a single byte from the i2c context. event_type = mraa_i2c_read_byte(i2c); if (event_type != -1) { if (event_type == 'A') { system("echo Entered event A >> log.txt"); } else if (event_type == 'B') { system("echo Entered event B >> log.txt"); } else { system("echo $(date) Wrong wake-up character received. >> log.txt"); } //Waiting for one second to ensure program stability sleep(1); //Writing a single byte back to the i2c context mraa_i2c_write_byte(i2c, 1); //De-initializing the mraa_i2c_context device. mraa_i2c_stop(i2c); } if (event_type == 'A' || event_type == 'B') { //Shutdown intel edison if program ran successfully. system("shutdown -h now"); } else { system("echo $(date) No data received!!!!! >> log.txt"); //De-initialize the mraa_i2c_context device mraa_i2c_stop(i2c); } return 0; }
/** * Write a byte on the bus * * @param data The byte to send on the bus * @return Result of operation */ mraa_result_t writeByte(uint8_t data) { return mraa_i2c_write_byte(m_i2c, data); }
/** * Write a byte on the bus * * @param data The byte to send on the bus * @return Result of operation */ Result writeByte(uint8_t data) { return (Result) mraa_i2c_write_byte(m_i2c, data); }