/*Function: The clock timing will start */ void rtc_ds1307_start(void) // set the ClockHalt bit low to start the rtc { uint8 dta[2] = {0x00}; suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1); suli_i2c_read(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1); dta[1] = dta[0] & 0x7f; dta[0] = 0x00; suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 2); }
/*Function: The clock timing will stop */ void rtc_ds1307_stop(void) // set the ClockHalt bit high to stop the rtc { uint8 dta[2] = {0x00}; suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1); suli_i2c_read(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1); dta[1] = dta[0] | 0x80; dta[0] = 0x00; suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 2); }
bool GroveI2CMotorDriver::write_dcmotor_speed(uint8_t speed_m1, uint8_t speed_m2) { cmd[0] = MotorSpeedSet; cmd[1] = speed_m1; cmd[2] = speed_m2; suli_i2c_write(i2c, i2c_addr, cmd, 3); cmd[0] = DirectionSet; cmd[1] = dir; cmd[2] = Nothing; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
static void setMeasurementMode(I2C_T *i2c, uint8_t mode) { //write(MODE_REGISTER, mode); cmdbuf[0] = MODE_REGISTER; cmdbuf[1] = mode; suli_i2c_write(i2c, HMC5883L_ADDRESS, cmdbuf, 2); }
// Writes val to address register on device void writeTo(uint8 address, uint8 val) { uint8 dta_send[] = {address, val}; suli_i2c_write(__I2C_Device, ADXL345_DEVICE, dta_send, 2); // suli_i2c_write(void * i2c_device, uint8 dev_addr, uint8 *data, uint8 len); }
bool GroveI2CMotorDriver::write_enable_stepper_mode(uint8_t direction, uint8_t speed) { cmd[0] = EnableStepper; cmd[1] = direction; cmd[2] = speed; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
bool GroveI2CMotorDriver::write_stepper_steps(uint8_t steps) { cmd[0] = Stepernu; cmd[1] = steps; cmd[2] = Nothing; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
bool GroveI2CMotorDriver::write_disable_stepper_mode() { cmd[0] = UnenableStepper; cmd[1] = Nothing; cmd[2] = Nothing; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
static void grove_compass_getxyz_raw(I2C_T *i2c, int16_t *x, int16_t *y, int16_t *z) { cmdbuf[0] = DATA_REGISTER_BEGIN; suli_i2c_write(i2c, HMC5883L_ADDRESS, &cmdbuf[0], 1); suli_i2c_read(i2c, HMC5883L_ADDRESS, databuf, 6); *x = (databuf[0] << 8) | databuf[1]; *y = (databuf[2] << 8) | databuf[3]; *z = (databuf[4] << 8) | databuf[5]; }
bool GroveI2CMotorDriver::write_dcmotor1_change_direction() { dir = dir ^ 0x3; cmd[0] = DirectionSet; cmd[1] = dir; cmd[2] = Nothing; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
// Reads num bytes starting from address register on device in to _buff array void readFrom(uint8 address, uint8 num, uint8 buff[]) { //grove_hal_i2c_read(ADXL345_DEVICE, address, buff, num); uint8 dta_send[] = {address}; suli_i2c_write(__I2C_Device, ADXL345_DEVICE, dta_send, 1); suli_i2c_read(__I2C_Device, ADXL345_DEVICE, buff, num); }
static int setScale(I2C_T *i2c, float gauss) { uint8_t regValue = 0x00; if(gauss == 0.88) { regValue = 0x00; m_Scale = 0.73; } else if(gauss == 1.3) { regValue = 0x01; m_Scale = 0.92; } else if(gauss == 1.9) { regValue = 0x02; m_Scale = 1.22; } else if(gauss == 2.5) { regValue = 0x03; m_Scale = 1.52; } else if(gauss == 4.0) { regValue = 0x04; m_Scale = 2.27; } else if(gauss == 4.7) { regValue = 0x05; m_Scale = 2.56; } else if(gauss == 5.6) { regValue = 0x06; m_Scale = 3.03; } else if(gauss == 8.1) { regValue = 0x07; m_Scale = 4.35; } else return -1; // Setting is in the top 3 bits of the register. regValue = regValue << 5; //write(CONFIGURATION_REGISTERB, regValue); cmdbuf[0] = CONFIGURATION_REGISTERB; cmdbuf[1] = regValue; suli_i2c_write(i2c, HMC5883L_ADDRESS, &cmdbuf[0], 2); return true; }
void rtc_ds1307_set_time(struct __time t) { uint8 dta[8] = {0x00, t.second, t.minute, t.hour, t.week, t.day, t.month, t.year}; for(int i=1; i<8; i++) { dta[i] = decToBcd(dta[i]); } suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 8); }
bool GroveI2CMotorDriver::write_dcmotor1_break() { motor1_dir = dir & 0x3; dir |= 0x3; cmd[0] = DirectionSet; cmd[1] = dir; cmd[2] = Nothing; suli_i2c_write(i2c, i2c_addr, cmd, 3); return true; }
/*Function: Read time and date from RTC */ void rtc_ds1307_get_time(struct __time * time_get) { uint8 dta[1] = {0x00}; suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1); suli_i2c_read(__I2C_Device_RTC, DS1307_I2C_ADDRESS, time_get->data, 7); time_get->data[0] &= 0x7f; time_get->data[2] &= 0x3f; for(int i=0; i<7; i++) { time_get->data[i] = bcdToDec(time_get->data[i]); } }
GroveCompass::GroveCompass(int pinsda, int pinscl) { this->i2c = (I2C_T *)malloc(sizeof(I2C_T)); suli_i2c_init(i2c, pinsda, pinscl); suli_delay_ms(5); //uint8_t config; //config = (0x01 << 5); //suli_i2c_write_reg(i2c, HMC5883L_ADDRESS, CONFIGURATION_REGISTERB, &config, 1); mode = MEASUREMENT_CONTINUOUS; suli_i2c_write_reg(i2c, HMC5883L_ADDRESS, MODE_REGISTER, &mode, 1); cmdbuf[0] = DATA_REGISTER_BEGIN; suli_i2c_write(i2c, HMC5883L_ADDRESS, cmdbuf, 1); }
bool grove_compass_write_setup(I2C_T *i2c) { uint8_t regValue = 0x00; //setScale(i2c, (float)1.3); // Set the scale of the compass. regValue = 0x01; m_Scale = 0.92; // Setting is in the top 3 bits of the register. regValue = regValue << 5; //write(CONFIGURATION_REGISTERB, regValue); cmdbuf[0] = CONFIGURATION_REGISTERB; cmdbuf[1] = regValue; suli_i2c_write(i2c, HMC5883L_ADDRESS, cmdbuf, 2); setMeasurementMode(i2c, MEASUREMENT_CONTINUOUS); // Set the measurement mode to Continuous return true; }
bool GroveCompass::read_compass_heading(float *heading_deg) { int16_t x,y,z; float cx, cy, cz; suli_i2c_read(i2c, HMC5883L_ADDRESS, databuf, 6); x = (databuf[0] << 8) | databuf[1]; z = (databuf[2] << 8) | databuf[3]; y = (databuf[4] << 8) | databuf[5]; //Serial1.println(x); //Serial1.println(y); //Serial1.println(z); cx = static_cast<float>(x); cy = static_cast<float>(y); cz = static_cast<float>(z); float head = atan2f(cy, cx) - 0.0457; // Correct for when signs are reversed. if(head < 0) head += 2*PI; // Check for wrap due to addition of declination. if(head > 2*PI) head -= 2*PI; // Convert radians to degrees for readability. *heading_deg = head * 180 / PI; cmdbuf[0] = DATA_REGISTER_BEGIN; suli_i2c_write(i2c, HMC5883L_ADDRESS, cmdbuf, 1); return true; }
// send data void oled_128x64_dta(unsigned char Data) { uint8 dta[2] = {SeeedOLED_Data_Mode, Data}; suli_i2c_write(__I2C_Device, SeeedOLED_Address, dta, 2); }
// send command void oled_128x64_cmd(unsigned char command) { uint8 dta[2] = {SeeedOLED_Command_Mode, command}; suli_i2c_write(__I2C_Device, SeeedOLED_Address, dta, 2); }