/** * Read exactly one byte from the bus * * @throws std::invalid_argument in case of error * @return char read from the bus */ uint8_t readByte() { int x = mraa_i2c_read_byte(m_i2c); if (x == -1) { throw std::invalid_argument("Unknown error in I2c::readByte()"); } return (uint8_t) x; }
mrb_value mrb_mraa_i2c_read_byte(mrb_state *mrb, mrb_value self){ mraa_i2c_context i2c; uint8_t rdata; Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c); rdata = mraa_i2c_read_byte(i2c); return mrb_fixnum_value(rdata); }
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; }
/** * Read exactly one byte from the bus * * @return char read from the bus */ uint8_t readByte() { return (uint8_t) mraa_i2c_read_byte(m_i2c); }