boolean Adafruit_TSL2561_Unified::begin(void) 
{
  Wire.begin();

  /* Make sure we're actually connected */
  uint8_t x = read8(TSL2561_REGISTER_ID);
  if (!(x & 0x0A))
  {
    return false;
  }
  _tsl2561Initialised = true;

  /* Set default integration time and gain */
  setIntegrationTime(_tsl2561IntegrationTime);
  setGain(_tsl2561Gain);

  /* Note: by default, the device is in power down mode on bootup */
  disable();

  return true;
}
Exemplo n.º 2
0
bool Adafruit_LSM303_Mag_Unified::begin()
{
  // Enable I2C
  Wire.begin();
  
  // Enable the magnetometer
  write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_MR_REG_M, 0x00);

  // LSM303DLHC has no WHOAMI register so read CRA_REG_M to check
  // the default value (0b00010000/0x10)
  uint8_t reg1_a = read8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRA_REG_M);
  if (reg1_a != 0x10)
  {
    return false;
  }

  // Set the gain to a known level
  setMagGain(LSM303_MAGGAIN_1_3);

  return true;
}
void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
  //Serial.print("Attempting to set freq ");
  //Serial.println(freq);
  
  float prescaleval = 25000000;
  prescaleval /= 4096;
  prescaleval /= freq;
  prescaleval -= 1;
  Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
  uint8_t prescale = floor(prescaleval + 0.5);
  Serial.print("Final pre-scale: "); Serial.println(prescale);  
  
  uint8_t oldmode = read8(PCA9685_MODE1);
  uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
  write8(PCA9685_MODE1, newmode); // go to sleep
  write8(PCA9685_PRESCALE, prescale); // set the prescaler
  write8(PCA9685_MODE1, oldmode);
  delay(5);
  write8(PCA9685_MODE1, oldmode | 0x80);
  //  Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
}
Exemplo n.º 4
0
void Adafruit_LSM303_Accel_Unified::read()
{
  // Read the accelerometer
  
    uint8_t xlo = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_X_L_A) | 0x80; //s two compliment
    uint8_t xhi = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_X_H_A) | 0x80;
    uint8_t ylo = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_Y_L_A) | 0x80;
    uint8_t yhi = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_Y_H_A) | 0x80;
    uint8_t zlo = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_Z_L_A) | 0x80;
    uint8_t zhi = read8((byte)LSM303_ADDRESS_ACCEL,(byte)LSM303_REGISTER_ACCEL_OUT_Z_H_A) | 0x80; 
	 
  // Shift values to create properly formed integer (low byte first)
  _accelData.x = (int16_t)(xlo | (xhi << 8)) >> 4;
  _accelData.y = (int16_t)(ylo | (yhi << 8)) >> 4;
  _accelData.z = (int16_t)(zlo | (zhi << 8)) >> 4;
}
Exemplo n.º 5
0
bool QTranslatorPrivate::do_load(const uchar *data, int len)
{
    if (!data || len < MagicLength || memcmp(data, magic, MagicLength))
        return false;

    bool ok = true;
    const uchar *end = data + len;

    data += MagicLength;

    while (data < end - 4) {
        quint8 tag = read8(data++);
        quint32 blockLen = read32(data);
        data += 4;
        if (!tag || !blockLen)
            break;
        if (data + blockLen > end) {
            ok = false;
            break;
        }

        if (tag == QTranslatorPrivate::Contexts) {
            contextArray = data;
            contextLength = blockLen;
        } else if (tag == QTranslatorPrivate::Hashes) {
            offsetArray = data;
            offsetLength = blockLen;
        } else if (tag == QTranslatorPrivate::Messages) {
            messageArray = data;
            messageLength = blockLen;
        } else if (tag == QTranslatorPrivate::NumerusRules) {
            numerusRulesArray = data;
            numerusRulesLength = blockLen;
        }

        data += blockLen;
    }

    return ok;
}
Exemplo n.º 6
0
int
PCA9685::setPWMFreq(float freq)
{
	int ret  = OK;
	freq *= 0.9f;  /* Correct for overshoot in the frequency setting (see issue
		https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library/issues/11). */
	float prescaleval = 25000000;
	prescaleval /= 4096;
	prescaleval /= freq;
	prescaleval -= 1;
	uint8_t prescale = uint8_t(prescaleval + 0.5f); //implicit floor()
	uint8_t oldmode;
	ret = read8(PCA9685_MODE1, oldmode);
	if (ret != OK) {
		return ret;
	}
	uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep

	ret = write8(PCA9685_MODE1, newmode); // go to sleep
	if (ret != OK) {
		return ret;
	}
	ret = write8(PCA9685_PRESCALE, prescale); // set the prescaler
	if (ret != OK) {
		return ret;
	}
	ret = write8(PCA9685_MODE1, oldmode);
	if (ret != OK) {
		return ret;
	}

	usleep(5000); //5ms delay (from arduino driver)

	ret = write8(PCA9685_MODE1, oldmode | 0xa1);  //  This sets the MODE1 register to turn on auto increment.
	if (ret != OK) {
		return ret;
	}

	return ret;
}
Exemplo n.º 7
0
/**
 * \brief Read raw data from record
 */
void Storage::readRawData(uint16_t &length, uint8_t* data_record, uint16_t &offset)
{
	/* Read raw value */
	switch (length) {
	case 1:
		sprintf(buffer.data(), "%" PRIu16, static_cast<int>(read8(data_record + offset)));
		break;
	case 2:
		sprintf(buffer.data(), "%" PRIu16, ntohs(read16(data_record + offset)));
		break;
	case 4:
		sprintf(buffer.data(), "%" PRIu32, ntohl(read32(data_record + offset)));
		break;
	case 8:
		sprintf(buffer.data(), "%" PRIu64, be64toh(read64(data_record + offset)));
		break;
	default:
		length = this->realLength(length, data_record, offset);

		if (length == 0) {
			STR_APPEND(record, "null");
			return;
		}

		if (length * 2 > buffer.capacity()) {
			buffer.reserve(length * 2 + 1);
		}

		/* Start the string with 0x and print the rest in hexa */
		strncpy(buffer.data(), "0x", 3);
		for (int i = 0; i < length; i++) {
			sprintf(buffer.data() + i * 2 + 2, "%02x", (data_record + offset)[i]);
		}
	}

	record += '"';
	record += buffer.data();
	record += '"';
}
Exemplo n.º 8
0
void testNotLargePageAligned2MRW(result_t *result, char abort, char quiet){
	int fails = 0;
	int testNr = 0;

	for(testNr=0; testNr<(LARGE_PAGE_SIZE / MEDIUM_PAGE_SIZE); testNr++){
//		write64(L_SANDBOXW + testNr * MEDIUM_PAGE_SIZE, testNr, 1);
//		if(read8(L_SANDBOXR + testNr * MEDIUM_PAGE_SIZE, testNr, 0, 1)){
//			fails++;
			write8(L_SANDBOXW + testNr * MEDIUM_PAGE_SIZE, testNr, quiet);
			read8(L_SANDBOXR + testNr * MEDIUM_PAGE_SIZE, testNr, abort, quiet);
//		}
		if(fails >= 5){
			printString("Ending test prematurely due to too many failures\n", quiet);
			break;
		}
	}
	result_t localResult;
	localResult.nrFailed = fails;
	localResult.nrTests = testNr + 1;

	addResults(result, &localResult);
}
Exemplo n.º 9
0
int tu_file::read_string(char* dst, int max_length) 
{
	int i = 0;
	while (i < max_length)
	{
		dst[i] = read8();
		if (get_eof() == true || dst[i] == '\n' || dst[i] == 0)
		{
			// remove the last '\r'
			if (i > 0 && dst[i - 1] == '\r')
			{
				i--;
			}

			dst[i] = 0;
			return i + 1;
		}
		i++;
	}
	dst[i - 1] = 0;	// force termination.
	return i;
}
Exemplo n.º 10
0
float Adafruit_MPL3115A2::getTemperature() {
  uint16_t t;

  uint8_t sta = 0;
  while (! (sta & MPL3115A2_REGISTER_STATUS_TDR)) {
    sta = read8(MPL3115A2_REGISTER_STATUS);
    delay(10);
  }
  Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device 
  Wire.write(MPL3115A2_REGISTER_TEMP_MSB); 
  Wire.endTransmission(false); // end transmission
  
  Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)2);// send data n-bytes read
  t = Wire.read(); // receive DATA
  t <<= 8;
  t |= Wire.read(); // receive DATA
  t >>= 4;

  float temp = t;
  temp /= 16.0;
  return temp;
}
Exemplo n.º 11
0
boolean Adafruit_TCS34725::begin(void)
{
    _wire->begin();
    
    /* Make sure we're actually connected */
    uint8_t x = read8(TCS34725_ID);
    Serial.println(x, HEX);
    if (x != 0x44)
    {
        return false;
    }
    _tcs34725Initialised = true;
    
    /* Set default integration time and gain */
    setIntegrationTime(_tcs34725IntegrationTime);
    setGain(_tcs34725Gain);
    
    /* Note: by default, the device is in power down mode on bootup */
    enable();
    
    return true;
}
Exemplo n.º 12
0
/* Read from the 0x0XXX range of ports */
static uint8_t control_read(const uint16_t pio) {
    uint8_t index = pio & 0x7F;

    uint8_t value;

    switch (index) {
        case 0x01:
            value = control.cpuSpeed;
            break;
        case 0x02:
            /* Set bit 1 to set battery state */
            value = control.readBatteryStatus;
            break;
        case 0x03:
            value = get_device_type();
            break;
        case 0x0B:
            /* bit 2 set if charging */
            value = control.ports[index] | (control.batteryCharging == true)<<1;
            break;
        case 0x0F:
            value = control.ports[index];
            if(control.USBConnected)    { value |= 0x80; }
            if(control.noPlugAInserted) { value |= 0x40; }
            break;
        case 0x1D:
        case 0x1E:
        case 0x1F:
            value = read8(control.privileged, (index - 0x1D) << 3);
            break;
        case 0x28:
            value = control.ports[index] | 0x08;
            break;
        default:
            value = control.ports[index];
            break;
    }
    return value;
}
Exemplo n.º 13
0
/**
 * Probe for supported codecs
 */
int hda_codec_detect(u8 *base)
{
	u8 reg8;

	/* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
	if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST) < 0)
		goto no_codec;

	/* Write back the value once reset bit is set. */
	write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));

	/* Clear the "State Change Status Register" STATESTS bits
	 * for each of the "SDIN Stat Change Status Flag"
	 */
	write8(base + HDA_STATESTS_REG, 0xf);

	/* Turn off the link and poll RESET# bit until it reads back as 0 */
	if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, ~HDA_GCTL_CRST) < 0)
		goto no_codec;

	/* Turn on the link and poll RESET# bit until it reads back as 1 */
	if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST) < 0)
		goto no_codec;

	/* Read in Codec location (BAR + 0xe)[2..0]*/
	reg8 = read8(base + HDA_STATESTS_REG);
	reg8 &= 0x0f;
	if (!reg8)
		goto no_codec;

	return reg8;

no_codec:
	/* Codec Not found */
	/* Put HDA back in reset (BAR + 0x8) [0] */
	set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, 0);
	printk(BIOS_DEBUG, "HDA: No codec!\n");
	return 0;
}
static void get_emic(struct xmp_context *ctx, int size, FILE *f)
{
    struct xmp_player_context *p = &ctx->p;
    struct xmp_mod_context *m = &p->m;
    int i, ver;

    ver = read16b(f);
    fread(m->name, 1, 20, f);
    fread(m->author, 1, 20, f);
    m->xxh->bpm = read8(f);
    m->xxh->ins = read8(f);
    m->xxh->smp = m->xxh->ins;

    m->xxh->flg |= XXM_FLG_MODRNG;

    snprintf(m->type, XMP_NAMESIZE, "EMOD v%d (Quadra Composer)", ver);
    MODULE_INFO();

    INSTRUMENT_INIT();

    reportv(ctx, 1, "     Instrument name      Len  LBeg LEnd L Vol Fin\n");

    for (i = 0; i < m->xxh->ins; i++) {
	m->xxi[i] = calloc(sizeof (struct xxm_instrument), 1);

	read8(f);		/* num */
	m->xxi[i][0].vol = read8(f);
	m->xxs[i].len = 2 * read16b(f);
	fread(m->xxih[i].name, 1, 20, f);
	m->xxs[i].flg = read8(f) & 1 ? WAVE_LOOPING : 0;
	m->xxi[i][0].fin = read8(f);
	m->xxs[i].lps = 2 * read16b(f);
	m->xxs[i].lpe = m->xxs[i].lps + 2 * read16b(f);
	read32b(f);		/* ptr */

	m->xxih[i].nsm = 1;
	m->xxi[i][0].pan = 0x80;
	m->xxi[i][0].sid = i;

	if (V(1) && (strlen((char *)m->xxih[i].name) || (m->xxs[i].len > 2))) {
	    report ("[%2X] %-20.20s %05x %05x %05x %c V%02x %+d\n",
			i, m->xxih[i].name, m->xxs[i].len, m->xxs[i].lps,
			m->xxs[i].lpe, m->xxs[i].flg & WAVE_LOOPING ? 'L' : ' ',
			m->xxi[i][0].vol, m->xxi[i][0].fin >> 4);
	}
    }
Exemplo n.º 15
0
void PX4Flow::update_integral()
{
  //send 0x16 to PX4FLOW module and receive back 25 Bytes data 
  Wire.beginTransmission(PX4FLOW_ADDRESS);
  Wire.write(0x16);  
  Wire.endTransmission();  
  
  // request 25 bytes from the module
  Wire.requestFrom(PX4FLOW_ADDRESS, 25);    

  // wait for all data to be available
  // TODO we could manage a timeout in order not to block
  // the loop when no component is connected
  while(Wire.available() < 25);
  
  iframe.frame_count_since_last_readout = read16();
  iframe.pixel_flow_x_integral  = read16();
  iframe.pixel_flow_y_integral  = read16();
  iframe.gyro_x_rate_integral   = read16();
  iframe.gyro_y_rate_integral   = read16();
  iframe.gyro_z_rate_integral   = read16();
  iframe.integration_timespan   = read32();
  iframe.sonar_timestamp        = read32();
  iframe.ground_distance        = read16();
  iframe.gyro_temperature       = read16();
  iframe.quality                = read8();
  
  // if too many bytes are available, we drain in order to be synched
  // on next read
  if(Wire.available()) {
    #if PX4FLOW_DEBUG == true
    {
      Serial.println("ERROR [PX4Flow] : Too many bytes available.");
    }
    #endif
    while(Wire.available()) {Wire.read();}
  }
}
bool TCS34725::begin(void)
{
  /* Make sure we're actually connected */
  uint8_t x = read8(TCS34725_ID);
  if (x != 0x44)
  {
    Serial.print("Error initializing sensor: ");
    Serial.println(x, HEX);
    _tcs34725Initialised = false;
    return false;
  }
  else {
    _tcs34725Initialised = true;

    /* Set default integration time and gain */
    setIntegrationTime(_tcs34725IntegrationTime);
    setGain(_tcs34725Gain);

    /* Note: by default, the device is in power down mode on bootup */
    enable();
    return true;
  }
}
Exemplo n.º 17
0
bool baro_begin(void)
{
	pmc_enable_periph_clk(BARO_TWI_ID);
	i2c_init(BARO_TWI);
	i2c_begin();

	memset(baro_buffer, 0, sizeof(baro_buffer));

	uint8_t whoami = read8(MPL3115A2_WHOAMI);

	write8(MPL3115A2_CTRL_REG1,
			MPL3115A2_CTRL_REG1_SBYB |
			MPL3115A2_CTRL_REG1_OS128 |
			MPL3115A2_CTRL_REG1_ALT);

	write8(MPL3115A2_PT_DATA_CFG,
		MPL3115A2_PT_DATA_CFG_TDEFE |
		MPL3115A2_PT_DATA_CFG_PDEFE |
		MPL3115A2_PT_DATA_CFG_DREM);

	return true;

}
Exemplo n.º 18
0
bool begin(uint8_t mode) {
	if (mode > BMP085_ULTRAHIGHRES) 
		mode = BMP085_ULTRAHIGHRES;
	oversampling = mode;

	if (read8(0xD0,1) != 0x55) return false;

	/* read calibration data */
	ac1 = read16(BMP085_CAL_AC1);
	ac2 = read16(BMP085_CAL_AC2);
	ac3 = read16(BMP085_CAL_AC3);
	ac4 = read16(BMP085_CAL_AC4);
	ac5 = read16(BMP085_CAL_AC5);
	ac6 = read16(BMP085_CAL_AC6);

	b1 = read16(BMP085_CAL_B1);
	b2 = read16(BMP085_CAL_B2);

	mb = read16(BMP085_CAL_MB);
	mc = read16(BMP085_CAL_MC);
	md = read16(BMP085_CAL_MD);
	return true;
}
Exemplo n.º 19
0
void Adafruit_LSM9DS0::setupMag ( lsm9ds0MagGain_t gain )
{
  uint8_t reg = read8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG6_XM);
  reg &= ~(0b01100000);
  reg |= gain;
  write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG6_XM, reg );

  switch(gain)
  {
    case LSM9DS0_MAGGAIN_2GAUSS:
      _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_2GAUSS;
      break;
    case LSM9DS0_MAGGAIN_4GAUSS:
      _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_4GAUSS;
      break;
    case LSM9DS0_MAGGAIN_8GAUSS:
      _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_8GAUSS;
      break;
    case LSM9DS0_MAGGAIN_12GAUSS:
      _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_12GAUSS;
      break;
  }
}
Exemplo n.º 20
0
static const unsigned char *
bmp_read_color_table(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char *end)
{
	int i, colors, readcolors;

	if (info->bitcount > 8)
		return p;

	if (info->colors == 0)
		colors = 1 << info->bitcount;
	else
		colors = info->colors;

	colors = fz_mini(colors, 1 << info->bitcount);

	if (info->palettetype == 0)
	{
		readcolors = fz_mini(colors, (end - p) / 3);
		for (i = 0; i < readcolors; i++)
		{
			info->palette[3 * i + 0] = read8(p + i * 3 + 2);
			info->palette[3 * i + 1] = read8(p + i * 3 + 1);
			info->palette[3 * i + 2] = read8(p + i * 3 + 0);
		}
		if (readcolors < colors)
			bmp_load_default_palette(ctx, info, readcolors);
		return p + readcolors * 3;
	}
	else
	{
		readcolors = fz_mini(colors, (end - p) / 4);
		for (i = 0; i < readcolors; i++)
		{
			/* ignore alpha channel */
			info->palette[3 * i + 0] = read8(p + i * 4 + 2);
			info->palette[3 * i + 1] = read8(p + i * 4 + 1);
			info->palette[3 * i + 2] = read8(p + i * 4 + 0);
		}
		if (readcolors < colors)
			bmp_load_default_palette(ctx, info, readcolors);
		return p + readcolors * 4;
	}

	return p;
}
Exemplo n.º 21
0
/*
 * Read PCR register. (This is internal function)
 * It returns PCR register and size in 1/2/4 bytes.
 * The offset should not exceed 0xFFFF and must be aligned with size
 */
static int pch_pcr_read(u8 pid, u16 offset, u32 size, void *data)
{
	if ((offset & (size - 1)) != 0) {
		printk(BIOS_DEBUG,
			"PchPcrRead error. Invalid Offset: %x Size: %x",
			offset, size);
		return -1;
	}
	switch (size) {
	case 4:
		*(u32 *) data = read32(pcr_reg_address(pid, offset));
		break;
	case 2:
		*(u16 *) data = read16(pcr_reg_address(pid, offset));
		break;
	case 1:
		*(u8 *) data = read8(pcr_reg_address(pid, offset));
		break;
	default:
		return -1;
	}
	return 0;
}
Exemplo n.º 22
0
uint32_t ZUNO_BMP180::readRawPressure(void) {
  uint32_t raw;

  write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));

  if (oversampling == BMP085_ULTRALOWPOWER) 
    _delay_ms(5);
  else if (oversampling == BMP085_STANDARD) 
    _delay_ms(8);
  else if (oversampling == BMP085_HIGHRES) 
    _delay_ms(14);
  else 
    _delay_ms(26);

  raw = read16(BMP085_PRESSUREDATA);

  raw <<= 8;
  raw |= read8(BMP085_PRESSUREDATA+2);
  raw >>= (8 - oversampling);

 
  return raw;
}
Exemplo n.º 23
0
void Adafruit_LSM303_Mag_Unified::read()
{
  // Read the magnetometer  
    uint8_t xlo = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_X_L_M) | 0x80; //s two compliment
    uint8_t xhi = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_X_H_M) | 0x80;
    uint8_t ylo = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_Y_L_M) | 0x80;
    uint8_t yhi = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_Y_H_M) | 0x80;
    uint8_t zlo = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_Z_L_M) | 0x80;
    uint8_t zhi = read8((byte)LSM303_ADDRESS_MAG,(byte)LSM303_REGISTER_MAG_OUT_Z_H_M) | 0x80; 
	  
  // Shift values to create properly formed integer (low byte first)
  _magData.x = (int16_t)(xlo | ((int16_t)xhi << 8));
  _magData.y = (int16_t)(ylo | ((int16_t)yhi << 8));
  _magData.z = (int16_t)(zlo | ((int16_t)zhi << 8));
  
  // ToDo: Calculate orientation
  _magData.orientation = 0.0;
}
float Adafruit_MPL3115A2::getAltitude() {
  int32_t alt;

  write8(MPL3115A2_CTRL_REG1, 
	 MPL3115A2_CTRL_REG1_SBYB |
	 MPL3115A2_CTRL_REG1_OS128 |
	 MPL3115A2_CTRL_REG1_ALT);

  uint8_t sta = 0;
  while (! (sta & MPL3115A2_REGISTER_STATUS_PDR)) {
    sta = read8(MPL3115A2_REGISTER_STATUS);
    delay(10);
  }
  Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device 
  Wire.write(MPL3115A2_REGISTER_PRESSURE_MSB); 
#ifdef __AVR_ATtiny85__
  Wire.endTransmission(); // end transmission
#else
  Wire.endTransmission(false); // end transmission
#endif  
  
  Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read
  alt = Wire.read(); // receive DATA
  alt <<= 8;
  alt |= Wire.read(); // receive DATA
  alt <<= 8;
  alt |= Wire.read(); // receive DATA
  alt >>= 4;

  if (alt & 0x800000) {
    alt |= 0xFF000000;
  }

  float altitude = alt;
  altitude /= 16.0;
  return altitude;
}
Exemplo n.º 25
0
static void readRawPressure(int32_t *pressure)
{
  #if BMP085_USE_DATASHEET_VALS
    *pressure = 23843;
  #else
    uint8_t  p8;
    uint16_t p16;
    int32_t  p32;

    writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READPRESSURECMD + (_bmp085Mode << 6));
    switch(_bmp085Mode)
    {
      case BMP085_MODE_ULTRALOWPOWER:
        delay(5);
        break;
      case BMP085_MODE_STANDARD:
        delay(8);
        break;
      case BMP085_MODE_HIGHRES:
        delay(14);
        break;
      case BMP085_MODE_ULTRAHIGHRES:
      default:
        delay(26);
        break;
    }

    read16(BMP085_REGISTER_PRESSUREDATA, &p16);
    p32 = (uint32_t)p16 << 8;
    read8(BMP085_REGISTER_PRESSUREDATA+2, &p8);
    p32 += p8;
    p32 >>= (8 - _bmp085Mode);

    *pressure = p32;
  #endif
}
Exemplo n.º 26
0
static int polly_test(FILE *f, char *t, const int start)
{
	int i;
	uint8 *buf;

	if (read8(f) != 0xae)
		return -1;

	if ((buf = malloc(0x10000)) == NULL)
		return -1;

	decode_rle(buf, f, 0x10000);

	for (i = 0; i < 128; i++) {
		if (buf[ORD_OFS + i] != 0 && buf[ORD_OFS] < 0xe0) {
			free(buf);
			return -1;
		}
	}

	if (t) {
		memcpy(t, buf + ORD_OFS + 160, 16);
		t[16] = 0;
		for (i = 15; i >=0; i--) {
			if (t[i] == ' ') {
				t[i] = 0;
			} else {
				break;
			}
		}
	}

	free(buf);

	return 0;
}
Exemplo n.º 27
0
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
{
  uint8_t a, b;

  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));

  /* Check the accelerometer revision */
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);

  /* Check the magnetometer revision */
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);

  /* Check the gyroscope revision */
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);

  /* Check the SW revision */
  info->bl_rev    = read8(BNO055_BL_REV_ID_ADDR);
  
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
}
Exemplo n.º 28
0
int8_t Adafruit_BNO055::getTemp(void)
{
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
  return temp;
}
Exemplo n.º 29
0
int MpegDemux::readPesHeader(PesHeader &pesHeader, int length, int startCode) {
	int c = 0;
	while (length > 0) {
		c = read8();
		length--;
		if (c != 0xFF) {
			break;
		}
	}
	if ((c & 0xC0) == 0x40) {
		read8();
		c = read8();
		length -= 2;
	}
	pesHeader.pts = 0;
	pesHeader.dts = 0;
	if ((c & 0xE0) == 0x20) {
		pesHeader.dts = pesHeader.pts = readPts(c);
		length -= 4;
		if ((c & 0x10) != 0) {
			pesHeader.dts = readPts();
			length -= 5;
		}
	} else if ((c & 0xC0) == 0x80) {
		int flags = read8();
		int headerLength = read8();
		length -= 2;
		length -= headerLength;
		if ((flags & 0x80) != 0) {
			pesHeader.dts = pesHeader.pts = readPts();
			headerLength -= 5;
			if ((flags & 0x40) != 0) {
				pesHeader.dts = readPts();
				headerLength -= 5;
			}
		}
		if ((flags & 0x3F) != 0 && headerLength == 0) {
			flags &= 0xC0;
		}
		if ((flags & 0x01) != 0) {
			int pesExt = read8();
			headerLength--;
			int skip = (pesExt >> 4) & 0x0B;
			skip += skip & 0x09;
			if ((pesExt & 0x40) != 0 || skip > headerLength) {
				pesExt = skip = 0;
			}
			this->skip(skip);
			headerLength -= skip;
			if ((pesExt & 0x01) != 0) {
				int ext2Length = read8();
				headerLength--;
				 if ((ext2Length & 0x7F) != 0) {
					 int idExt = read8();
					 headerLength--;
					 if ((idExt & 0x80) == 0) {
						 startCode = ((startCode & 0xFF) << 8) | idExt;
					 }
				 }
			}
		}
Exemplo n.º 30
0
static int ptdt_load(struct module_data *m, FILE *f, const int start)
{
	struct xmp_module *mod = &m->mod;
	int i, j;
	struct xmp_event *event;
	struct mod_header mh;
	uint8 mod_event[4];

	fread(&mh.name, 20, 1, f);
	for (i = 0; i < 31; i++) {
		fread(&mh.ins[i].name, 22, 1, f);
		mh.ins[i].size = read16b(f);
		mh.ins[i].finetune = read8(f);
		mh.ins[i].volume = read8(f);
		mh.ins[i].loop_start = read16b(f);
		mh.ins[i].loop_size = read16b(f);
	}
	mh.len = read8(f);
	mh.restart = read8(f);
	fread(&mh.order, 128, 1, f);
	fread(&mh.magic, 4, 1, f);

	mod->ins = 31;
	mod->smp = mod->ins;
	mod->chn = 4;
	mod->len = mh.len;
	mod->rst = mh.restart;
	memcpy(mod->xxo, mh.order, 128);

	for (i = 0; i < 128; i++) {
		if (mod->xxo[i] > mod->pat)
			mod->pat = mod->xxo[i];
	}

	mod->pat++;
	mod->trk = mod->chn * mod->pat;

	INSTRUMENT_INIT();

	for (i = 0; i < mod->ins; i++) {
		mod->xxi[i].sub = calloc(sizeof (struct xmp_subinstrument), 1);
		mod->xxs[i].len = 2 * mh.ins[i].size;
		mod->xxs[i].lps = 2 * mh.ins[i].loop_start;
		mod->xxs[i].lpe = mod->xxs[i].lps + 2 * mh.ins[i].loop_size;
		mod->xxs[i].flg = mh.ins[i].loop_size > 1 ? XMP_SAMPLE_LOOP : 0;
		mod->xxi[i].sub[0].fin = (int8)(mh.ins[i].finetune << 4);
		mod->xxi[i].sub[0].vol = mh.ins[i].volume;
		mod->xxi[i].sub[0].pan = 0x80;
		mod->xxi[i].sub[0].sid = i;
		mod->xxi[i].nsm = !!(mod->xxs[i].len);
		mod->xxi[i].rls = 0xfff;

		copy_adjust(mod->xxi[i].name, mh.ins[i].name, 22);

		D_(D_INFO "[%2X] %-22.22s %04x %04x %04x %c V%02x %+d",
				i, mod->xxi[i].name,
				mod->xxs[i].len, mod->xxs[i].lps,
				mod->xxs[i].lpe,
				mh.ins[i].loop_size > 1 ? 'L' : ' ',
				mod->xxi[i].sub[0].vol,
				mod->xxi[i].sub[0].fin >> 4);
	}

	PATTERN_INIT();

	/* Load and convert patterns */
	D_(D_INFO "Stored patterns: %d", mod->pat);

	for (i = 0; i < mod->pat; i++) {
		PATTERN_ALLOC(i);
		mod->xxp[i]->rows = 64;
		TRACK_ALLOC(i);
		for (j = 0; j < (64 * 4); j++) {
			event = &EVENT(i, j % 4, j / 4);
			fread(mod_event, 1, 4, f);
			cvt_pt_event(event, mod_event);
		}
	}

	m->quirk |= QUIRK_MODRNG;

	/* Load samples */
	D_(D_INFO "Stored samples: %d", mod->smp);

	for (i = 0; i < mod->smp; i++) {
		if (!mod->xxs[i].len)
			continue;
		load_sample(f, 0, &mod->xxs[mod->xxi[i].sub[0].sid], NULL);
	}

	return 0;
}