Пример #1
0
upm_result_t
lis3dh_devinit(const lis3dh_context dev, LIS3DH_ODR_T odr, LIS3DH_FS_T fs, bool high_res)
{
    assert(dev != NULL);

    // Set high resolution mode, ODR and FS using passed values.
    // Also unconditionally enable X, Y and Z axes, temperature sensor (and ADC),
    // BDU mode as well as disable output high-pass filter.
    if (lis3dh_enable_lp_mode(dev, false) ||
        lis3dh_enable_hr_mode(dev, high_res) ||
        lis3dh_enable_axes(dev, true, true, true) ||
        lis3dh_enable_bdu_mode(dev, true) ||
        lis3dh_set_odr(dev, odr) ||
        lis3dh_set_full_scale(dev, fs) ||
        lis3dh_enable_hp_filtering(dev, false) ||
        lis3dh_enable_temperature(dev, true)) {

        printf("%s: failed to set configuration parameters\n", __FUNCTION__);
        return UPM_ERROR_OPERATION_FAILED;
    }

    // Settle
    upm_delay_ms(50);

    return UPM_SUCCESS;
}
Пример #2
0
upm_result_t rn2903_reset(const rn2903_context dev)
{
    assert(dev != NULL);

    rn2903_autobaud(dev, RN2903_AUTOBAUD_RETRIES);

    if (rn2903_command(dev, "sys reset"))
    {
        // this command will reset the baudrate back to the default if
        // we changed it previously.  We do not report an error here,
        // if we are not using the default baudrate, since we've now
        // switched to a different baudrate than we had, and cannot
        // read the response anyway.
        if (dev->baudrate == RN2903_DEFAULT_BAUDRATE)
            return UPM_ERROR_OPERATION_FAILED;
    }

    // to be safe, always set the baudrate after a reset
    if (rn2903_set_baudrate(dev, dev->baudrate))
        return UPM_ERROR_OPERATION_FAILED;

    upm_delay_ms(100);

    return UPM_SUCCESS;
}
Пример #3
0
bool rn2903_autobaud(const rn2903_context dev, int retries)
{
    assert(dev != NULL);

    do
    {
        // trigger rn2903 auto-baud detection

        // send a break signal, then a 0x55, then try a command
        mraa_result_t rv;
        if ((rv = mraa_uart_sendbreak(dev->uart, 0)))
        {
            // we don't want to fail here if break not implemented or
            // supported
            if (rv != MRAA_ERROR_FEATURE_NOT_IMPLEMENTED &&
                rv != MRAA_ERROR_FEATURE_NOT_SUPPORTED)
            {

                printf("%s: mraa_uart_sendbreak() failed.\n", __FUNCTION__);
                return UPM_ERROR_OPERATION_FAILED;
            }
        }

        upm_delay_ms(100);

        // The magic autobaud detection character
        char buf = 0x55;
        rn2903_write(dev, &buf, 1);

        upm_delay_ms(100);

        // try a command to verify speed
        if (!rn2903_command(dev, "sys get ver"))
            break;

        if (dev->debug)
            printf("%s: RETRIES %d: FAIL!\n", __FUNCTION__, retries);
    } while (retries-- > 0);

    if (retries <= 0)
        return false;

    if (dev->debug)
        printf("%s: RETRIES %d: success!\n", __FUNCTION__, retries);

    return true;
}
Пример #4
0
int main(int argc, char **argv)
{
    signal(SIGINT, sig_handler);
//! [Interesting]

#if defined(CONFIG_BOARD_ARDUINO_101_SSS)
    // ARDUINO_101_SSS (ARC core) must use I2C
    // Instantiate a BMA250E instance using default i2c bus and address
    bma250e_context sensor = bma250e_init(BMA250E_DEFAULT_I2C_BUS,
                                          BMA250E_DEFAULT_ADDR, -1);
#elif defined(CONFIG_BOARD_ARDUINO_101)
    // ARDUINO_101 (Quark core) where you must use SPI
    // Instantiate a BMA250E instance using default SPI bus and pin 10 as CS
    bma250e_context sensor = bma250e_init(BMA250E_DEFAULT_SPI_BUS,
                                          -1, 10);
#else
    // everything else use I2C by default
    // Instantiate a BMA250E instance using default i2c bus and address
    bma250e_context sensor = bma250e_init(BMA250E_DEFAULT_I2C_BUS,
                                          BMA250E_DEFAULT_ADDR, -1);
#endif

    if (!sensor)
    {
        printf("bma250e_init() failed.\n");
        return 1;
    }

    // now output data every 250 milliseconds
    while (shouldRun)
    {
        float x, y, z;

        if (bma250e_update(sensor))
        {
            printf("bma250e_update() failed\n");
            return 1;
        }

        bma250e_get_accelerometer(sensor, &x, &y, &z);
        printf("Acceleration x: %f y: %f z: %f g\n",
               x, y, z);

        printf("Compensation Temperature: %f C\n\n",
               bma250e_get_temperature(sensor));

        upm_delay_ms(250);
    }

    printf("Exiting...\n");

    bma250e_close(sensor);

//! [Interesting]

    return 0;
}
Пример #5
0
int main()
{
    if (mraa_init() != MRAA_SUCCESS)
    {
        perror("Failed to initialize mraa\n");
        return -1;
    }

    signal(SIGINT, sig_handler);

    //! [Interesting]

    // Instantiate a slide sensor on analog pin A0
    slide_context sensor = slide_init(0);

    if (!sensor)
    {
        printf("slide_init() failed.\n");
        return -1;
    }

    // Set the aref, scale, and offset
    slide_set_aref(sensor, 5.0);
    slide_set_scale(sensor, 1.0);
    slide_set_offset(sensor, -.1);
    printf("aRef: %0.03f scale: %0.03f offset: %0.03f\n\n",
            slide_get_aref(sensor),
            slide_get_scale(sensor),
            slide_get_offset(sensor));

    // Every half a second, sample the sensor output
    while (shouldRun)
    {
        float normalized = 0.0;
        float raw_volts = 0.0;
        float volts = 0.0;

        slide_get_normalized(sensor, &normalized);
        slide_get_raw_volts(sensor, &raw_volts);
        slide_get_volts(sensor, &volts);

        printf("Normalized output: %0.03f, raw slide sensor output: %0.03f v "
                "adjusted output: %0.03f v\n", normalized, raw_volts, volts);

        upm_delay_ms(500);
    }

    //! [Interesting]

    printf("Exiting\n");

    slide_close(sensor);

    return 0;
}
Пример #6
0
int main()
{
    if (mraa_init() != MRAA_SUCCESS)
    {
        perror("Failed to initialize mraa\n");
        return -1;
    }

    signal(SIGINT, sig_handler);

    //! [Interesting]

    // Instantiate a NMEA_GPS UBLOX based i2c sensor on i2c bus 0 at
    // address 0x42
    nmea_gps_context sensor = nmea_gps_init_ublox_i2c(0, 0x42);

    if (!sensor)
    {
        printf("nmea_gps_init_ublox_i2c() failed.\n");
        return 1;
    }

    char buffer[bufferLength];
    int rv = 0;

    // loop, dumping NMEA data out as fast as it comes in
    while (shouldRun)
    {
        if (!nmea_gps_data_available(sensor, 0))
            upm_delay_ms(500);
        else
        {
            if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0)
            {
                int i;
                for (i=0; i<rv; i++)
                    printf("%c", buffer[i]);
            }
        }
    }

    //! [Interesting]

    printf("Exiting\n");

    nmea_gps_close(sensor);

    return 0;
}
Пример #7
0
int ecezo_read(const ecezo_context dev, char *buffer, size_t len)
{
    assert(dev != NULL);

    upm_delay_ms(CMD_DELAY); // delay CMD_DELAY ms to make sure cmd completed

    // i2c
    if (dev->i2c)
    {
        return readBytes(dev, (uint8_t *)buffer, len);
    }
    else
    {
        // UART
        size_t bytesRead = 0;

        while(bytesRead < len)
        {
            // we read one byte at a time, exiting when either len is
            // reached, or a '\r' is found indicating the end of a
            // sentence. Most commands (except 'R') require a minimum
            // of 300ms to execute, so we wait up to CMD_DELAY ms after all
            // data (if any) is read.
            if (ecezo_data_available(dev, CMD_DELAY))
            {
                int br = mraa_uart_read(dev->uart, &buffer[bytesRead], 1);

                if (br <= 0)
                    return br;

                if (buffer[bytesRead] == '\r')
                {
                    // if we found a CR, replace it with a 0 byte
                    buffer[bytesRead++] = 0;
                    return bytesRead;
                }
                bytesRead++;
            }
            else
            {
                // timed out - ok with responses disabled
                return 0;
            }
        }
    }

    // anything else is an error
    return -1;
}
Пример #8
0
upm_result_t mma7361_sleep(const mma7361_context dev, bool sleep)
{
  assert(dev != NULL);

  if (!dev->gpio_sleep)
    return UPM_ERROR_NO_RESOURCES;

  if (sleep)
    mraa_gpio_write(dev->gpio_sleep, 0);
  else
    mraa_gpio_write(dev->gpio_sleep, 1);

  upm_delay_ms(2);

  return UPM_SUCCESS;
}
Пример #9
0
int main()
{
    if (mraa_init() != MRAA_SUCCESS)
    {
        perror("Failed to initialize mraa\n");
        return -1;
    }

    signal(SIGINT, sig_handler);

    //! [Interesting]

    // Instantiate a URM37 sensor on UART 0, with the reset pin on D2
    urm37_context sensor = urm37_init(0, 2, 0, 0, 0, false);

    if (!sensor)
    {
        printf("urm37_init() failed.\n");
        return(1);
    }

    // Every half a second, sample the URM37 and output the measured
    // distance in cm.

    while (shouldRun)
    {
        float distance, temperature;

        urm37_get_distance(sensor, &distance, 0);
        printf("Detected distance (cm): %f\n", distance);

        urm37_get_temperature(sensor, &temperature);
        printf("Temperature (C): %f\n\n", temperature);
        upm_delay_ms(500);
    }

    //! [Interesting]

    printf("Exiting\n");

    urm37_close(sensor);

    return 0;
}
Пример #10
0
int main()
{
    signal(SIGINT, sig_handler);

    //! [Interesting]

    // Instantiate a sensor on analog pin A0
    vdiv_context sensor = vdiv_init(0, 5);

    if (!sensor)
    {
        printf("vdiv_init() failed.\n");
        return(1);
    }

    // Every half a second, sample the sensor output
    while (shouldRun)
    {
        float raw_volts = 0.0, computed_volts = 0.0;

        vdiv_get_raw_volts(sensor, &raw_volts);
        vdiv_get_computed_volts(sensor, &computed_volts);

        printf("Divide SW: %d ADC voltage: %0.03f Sensor voltage: %0.03f\n",
                vdiv_get_divsw(sensor), raw_volts, computed_volts);

        upm_delay_ms(500);
    }

    //! [Interesting]

    printf("Exiting\n");

    vdiv_close(sensor);

    return 0;
}
Пример #11
0
// 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;
}