Esempio n. 1
0
int main(void)
{
    hdc1000_t dev;
    uint16_t rawtemp, rawhum;
    int temp, hum;

    puts("HDC1000 Temperature and Humidity Sensor driver test application\n");
    printf("Initializing HDC1000 sensor at I2C_%i... ", TEST_HDC1000_I2C);
    if (hdc1000_init(&dev, TEST_HDC1000_I2C, TEST_HDC1000_ADDR) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return -1;
    }

    while (1) {
        if (hdc1000_startmeasure(&dev)) {
            puts("Start measure failed.");
            return -1;
        }
        vtimer_usleep(HDC1000_CONVERSION_TIME);

        hdc1000_read(&dev, &rawtemp, &rawhum);
        printf("Raw data T: %5i   RH: %5i\n", rawtemp, rawhum);

        hdc1000_convert(rawtemp, rawhum,  &temp, &hum);
        printf("Data T: %d   RH: %d\n", temp, hum);

        vtimer_usleep(SLEEP);

    }

    return 0;
}
Esempio n. 2
0
void blink(void) {
    struct timeval tiv;
    uint32_t wait;

    /* everyone needs to wait for microseconds=0 before starting to blink */
    rtc_time(&tiv);
    vtimer_usleep( (uint32_t)(( 1000*1000 - tiv.tv_usec ) * VTIMER_FACTOR ));
    //hwtimer_spin( HWTIMER_TICKS( ( 1000*1000 - tiv.tv_usec ) * VTIMER_FACTOR ) );
    printf("blink thread synced to zero\n");

    while(1){
        //rtc_time(&tiv);
        //printf("blink time usec: %lu\n", tiv.tv_usec);
        /* do the blink */
        LED_RED_ON;
        //hwtimer_spin( HWTIMER_TICKS( BLINK_DURATION ));
        vtimer_usleep( BLINK_DURATION );
        LED_RED_OFF;

        wait = timesync_get_master_offset();
        if ( wait && ! timesync_get_synced() ){
            /* be careful with debug output here, as it affects delay as well! */
            //rtc_time(&tiv);
            //printf("(pseudo) adjusting clock from %i\n", (int) tiv.tv_usec);
            vtimer_usleep( (uint32_t)((wait+PROC_DELAY) * VTIMER_FACTOR) );
            printf("D: waited %lu us.\n", (uint32_t)((wait+PROC_DELAY) * VTIMER_FACTOR));
            timesync_set_synced(1);
            //rtc_time(&tiv);
            //printf("(pseudo) adjusting clock to %i\n", (int)tiv.tv_usec);
        }
        vtimer_usleep( BLINK_PAUSE-BLINK_DURATION);
    }
}
Esempio n. 3
0
int main(void)
{
    puts("PIR motion sensor test application\n");
    printf("Initializing PIR sensor at GPIO_%ld... ", (long)PIR_GPIO);
    if (pir_init(&dev, PIR_GPIO) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return 1;
    }

#if TEST_PIR_POLLING
    puts("Printing sensor state every second.");
    while (1) {
        printf("Status: %s\n", pir_get_status(&dev) == PIR_STATUS_LO ? "lo" : "hi");
        vtimer_usleep(1000 * 1000);
    }
#else
   thread_create(
           pir_handler_stack, sizeof(pir_handler_stack), THREAD_PRIORITY_MAIN - 1,
           CREATE_WOUT_YIELD | CREATE_STACKTEST,
           pir_handler, NULL, "pir_handler");
#endif
    return 0;
}
Esempio n. 4
0
int main(void)
{
    int res;
    int pos = (STEP_LOWER_BOUND + STEP_UPPER_BOUND) / 2;
    int step = STEP;

    puts("\nRIOT RC servo test");
    puts("Connect an RC servo or scope to PWM_0 channel 0 to see anything");

    res = servo_init(&servo, DEV, CHANNEL, SERVO_MIN, SERVO_MAX);
    if (res < 0) {
        puts("Errors while initializing servo");
        return -1;
    }
    puts("Servo initialized.");

    while (1) {
        servo_set(&servo, pos);

        pos += step;
        if (pos <= STEP_LOWER_BOUND || pos >= STEP_UPPER_BOUND) {
            step = -step;
        }

        vtimer_usleep(WAIT);
    }

    return 0;
}
Esempio n. 5
0
File: ping.c Progetto: AnonMall/RIOT
void l2_ping(radio_address_t addr, uint16_t count, uint32_t interval,
             const char *payload, uint8_t payload_len, uint8_t probe_only)
{
    l2_ping_type_t pt;

    probe_only ? (pt = L2_PROBE) : (pt = L2_PING);

    if (!interval) {
        interval = L2_PING_DEFAULT_INTERVAL;
    }

    mutex_lock(&ping_sender_mutex);
    l2_ping_stats.dst = addr;
    l2_ping_stats.ping_count = 0;
    l2_ping_stats.pong_count = 0;
    l2_ping_stats.last_rtt = timex_set(0, 0);
    l2_ping_stats.avg_rtt = timex_set(0, 0);
    l2_ping_stats.max_rtt = timex_set(0, 0);
    l2_ping_stats.min_rtt = timex_set(UINT32_MAX, UINT32_MAX);

    for (unsigned i = 1; (count == 0) || (i <= count); i++) {
        vtimer_now(&start);

        if (send_l2_packet(addr, pt, i, payload, payload_len)) {
            ping_sent = 1;
            l2_ping_stats.ping_count++;
        }
        if ((!count) || (i <= count)) {
            vtimer_usleep(interval);
        }
    }
    mutex_unlock(&ping_sender_mutex);
}
Esempio n. 6
0
void etx_clock(void) {
    /*
     * Manages the etx_beacon thread to wake up every full second +- jitter
     */

    /*
     * The jittercorrection and jitter variables keep usecond values divided
     * through 1000 to fit into uint8 variables.
     *
     * That is why they are multiplied by 1000 when used for hwtimer_wait.
     */
    uint8_t jittercorrection = ETX_DEF_JIT_CORRECT;
    uint8_t jitter = (uint8_t) (rand() % ETX_JITTER_MOD);

    while (true) {
        thread_wakeup(etx_beacon_pid);

        /*
         * Vtimer is buggy, but I seem to have no hwtimers left, so using this
         * for now.
         */
        vtimer_usleep(
                ((ETX_INTERVAL - ETX_MAX_JITTER)*MS)+ jittercorrection*MS + jitter*MS - ETX_CLOCK_ADJUST);

        //hwtimer_wait(
        //        HWTIMER_TICKS(((ETX_INTERVAL - ETX_MAX_JITTER)*MS) + jittercorrection*MS + jitter*MS - ETX_CLOCK_ADJUST));

        jittercorrection = (ETX_MAX_JITTER) - jitter;
        jitter = (uint8_t) (rand() % ETX_JITTER_MOD);
    }
}
Esempio n. 7
0
void at86rf231_switch_to_rx(void)
{
    at86rf231_disable_interrupts();
    // Send a FORCE TRX OFF command
    at86rf231_reg_write(AT86RF231_REG__TRX_STATE, AT86RF231_TRX_STATE__FORCE_TRX_OFF);

    // Reset IRQ to TRX END only
    at86rf231_reg_write(AT86RF231_REG__IRQ_MASK, AT86RF231_IRQ_STATUS_MASK__TRX_END);

    // Read IRQ to clear it
    at86rf231_reg_read(AT86RF231_REG__IRQ_STATUS);

    // Enable IRQ interrupt
    at86rf231_enable_interrupts();

    // Start RX
    at86rf231_reg_write(AT86RF231_REG__TRX_STATE, AT86RF231_TRX_STATE__RX_ON);

    // wait until it is on RX_ON state
    uint8_t status;
    uint8_t max_wait = 100;   // TODO : move elsewhere, this is in 10us

    do {
        status = at86rf231_get_status();

        vtimer_usleep(10);

        if (!--max_wait) {
            printf("at86rf231 : ERROR : could not enter RX_ON mode");
            break;
        }
    }
    while ((status & AT86RF231_TRX_STATUS_MASK__TRX_STATUS) != AT86RF231_TRX_STATUS__RX_ON);
}
Esempio n. 8
0
void acc_thread(void)
{
    int16_t acc_data[6];
    int16_t norm;
    int state = ACC_POS_0;
    int count = 0;

    /* initialize the acceleration sensor */
    puts("acc: will initialize SMB380 driver now");
    SMB380_init_simple(100, SMB380_BAND_WIDTH_375HZ, SMB380_RANGE_2G);
    puts("acc: SMB380 initialized");

    /* detect data */
    while (1) {
        /* wait according to the sampling interval */
        vtimer_usleep(SAMPLING_INTERVAL * 1000);

        /* get acceleration values */
        SMB380_getAcceleration(SMB380_X_AXIS, &acc_data[SMB380_X_AXIS], &acc_data[SMB380_X_AXIS + 3]);
        SMB380_getAcceleration(SMB380_Y_AXIS, &acc_data[SMB380_Y_AXIS], &acc_data[SMB380_Y_AXIS + 3]);
        SMB380_getAcceleration(SMB380_Z_AXIS, &acc_data[SMB380_Z_AXIS], &acc_data[SMB380_Z_AXIS + 3]);

        /* evaluate the new data */
        norm = math_modulus(acc_data, 3);
        if (norm > LOWER_TH && norm < UPPER_TH) {

            /* sensor is stable, evaluate all three axes */
            for (int i = 0; i < 3; i++) {
                if (acc_data[i] > AXIS_TH) {
                    if (state == i) {
                        if (count <= REP_COUNT) {
                            count++;
                        }
                    }
                    else {
                        state = i;
                        count = 0;
                    }
                }
                else if (acc_data[i] < -AXIS_TH) {
                    if (state == (i + 3)) {
                        if (count <= REP_COUNT) {
                            count++;
                        }
                    }
                    else {
                        state = i + 3;
                        count = 0;
                    }
                }
            }
        }
        if (count == REP_COUNT && acc_current_pos != state) {
            printf("acc: new state: %i\n", state);
            acc_current_pos = state;
        }
    }
}
Esempio n. 9
0
int main(void)
{
    puts("PIR motion sensor test application\n");

    printf("Initializing red LED at GPIO_%i...        ", GPIO_LED_RED);
    led_red = GPIO_LED_RED;
    if (gpio_init_out(led_red, GPIO_NOPULL) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return 1;
    }
    gpio_clear(led_red);

    printf("Initializing green LED at GPIO_%i...        ", GPIO_LED_GREEN);
    led_green = GPIO_LED_GREEN;
    if (gpio_init_out(led_green, GPIO_NOPULL) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return 1;
    }
    gpio_clear(led_green);

    printf("Initializing PIR sensor at GPIO_%i... ", PIR_GPIO);
    if (pir_init(&pir, PIR_GPIO) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return 1;
    }

   kernel_pid_t pir_handler_pid = thread_create(
           pir_handler_stack, sizeof(pir_handler_stack), PRIORITY_MAIN - 1,
           CREATE_WOUT_YIELD | CREATE_STACKTEST,
           pir_handler, NULL, "pir_handler");

#if TEST_PIR_POLLING
    puts("Checking sensor state every half second.");
    pir_event_t status_old;
    while (1) {
        pir_event_t status = pir_get_status(&pir);
        if (status != status_old) {
            msg_t m = { .type = status, .content.ptr = (void*)&pir, }; 
            msg_send(&m, pir_handler_pid, 0);
        }
        status_old = status;
        vtimer_usleep(1000 * 500);
    }
#endif
    return 0;
}
Esempio n. 10
0
int main(void)
{
    lsm303dlhc_t dev;
    int16_t temp_value;
    lsm303dlhc_3d_data_t mag_value;
    lsm303dlhc_3d_data_t acc_value;

    puts("LSM303DLHC temperature test application\n");
    printf("Initializing LSM303DLHC sensor at I2C_%i... ", TEST_LSM303DLHC_I2C);

    if (lsm303dlhc_init(&dev, TEST_LSM303DLHC_I2C, TEST_LSM303DLHC_ACC_PIN, TEST_LSM303DLHC_MAG_PIN,
                        TEST_LSM303DLHC_ACC_ADDR, ACC_S_RATE, ACC_SCALE,
                        TEST_LSM303DLHC_MAG_ADDR, MAG_S_RATE, MAG_GAIN) == 0) {
        puts("[OK]\n");
    }
    else {
        puts("[Failed]");
        return 1;
    }

    while (1) {
        if (lsm303dlhc_read_acc(&dev, &acc_value) == 0) {
            printf("Accelerometer x: %i y: %i z: %i\n", acc_value.x_axis,
                                                        acc_value.y_axis,
                                                        acc_value.z_axis);
        }
        else {
            puts("\nFailed reading accelerometer values\n");
        }
        if (lsm303dlhc_read_temp(&dev, &temp_value) == 0) {
            printf("Temperature value: %i degrees\n", temp_value);
        }
        else {
            puts("\nFailed reading value\n");
        }

        if (lsm303dlhc_read_mag(&dev, &mag_value) == 0) {
            printf("Magnetometer x: %i y: %i z: %i\n", mag_value.x_axis,
                                                       mag_value.y_axis,
                                                       mag_value.z_axis);
        }
        else {
            puts("\nFailed reading magnetometer values\n");
        }

        vtimer_usleep(SLEEP);
    }

    return 0;
}
Esempio n. 11
0
void *ccnl_riot_relay_helper_start(void *arg)
{
    (void) arg;
    unsigned long us = CCNL_CHECK_RETRANSMIT_USEC;
    mutex_lock(&theRelay->stop_lock);
    while (!theRelay->halt_flag) {
        mutex_lock(&theRelay->global_lock);
        ccnl_run_events();
        mutex_unlock(&theRelay->global_lock);

        vtimer_usleep(us);
    }

    mutex_unlock(&theRelay->stop_lock);
    return NULL;
}
Esempio n. 12
0
int main(void)
{
    hih6130_t dev;

    puts("HIH6130 sensor driver test application\n");
    printf("Initializing I2C_%i... ", TEST_HIH6130_I2C);
    if (i2c_init_master(TEST_HIH6130_I2C, I2C_SPEED_FAST) < 0) {
        puts("[Failed]");
        return -1;
    }
    puts("[OK]");

    printf("Initializing HIH6130 sensor at I2C_%i, address 0x%02x... ",
        TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
    hih6130_init(&dev, TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
    puts("[OK]");

    while (1) {
        float hum = 0.f;
        float temp = 0.f;
        int status;
        float integral = 0.f;
        float fractional;

        vtimer_usleep(SLEEP);

        status = hih6130_get_humidity_temperature_float(&dev, &hum, &temp);
        if (status < 0) {
            printf("Communication error: %d\n", status);
            continue;
        } else if (status == 1) {
            puts("Stale values");
        }
        /* Several platforms usually build with nano.specs, (without float printf) */
        /* Split value into two integer parts for printing. */
        fractional = modff(hum, &integral);
        printf("humidity: %4d.%04u %%",
            (int)integral, (unsigned int)abs(fractional * 10000.f));
        fractional = modff(temp, &integral);
        printf("  temperature: %4d.%04u C\n",
            (int)integral, (unsigned int)abs(fractional * 10000.f));
    }

    return 0;
}
Esempio n. 13
0
void sender(char *count) {
    unsigned int c, i;

    mesg.type = SND_PKT;
    mesg.content.ptr = (char*) &tcmd;

    tcmd.transceivers = TRANSCEIVER_CC1100;
    tcmd.data = &p;

    p.length = CC1100_MAX_DATA_LENGTH;
    p.dst = 0;

    c = atoi(count + strlen("snd "));
    for (i = 0; i < c; i++) {
        puts(".");
        p.data = snd_buffer[i % SND_BUFFER_SIZE];
        msg_send(&mesg, transceiver_pid, 1);
        vtimer_usleep(sending_delay);
    }
}
Esempio n. 14
0
static void *rt_timer_over(void *arg)
{
    (void) arg;

    rpl_routing_entry_t *rt;

    while (1) {
        rpl_dodag_t *my_dodag = rpl_get_my_dodag();

        if (my_dodag != NULL) {
            rt = rpl_get_routing_table();

            for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
                if (rt[i].used) {
                    if (rt[i].lifetime <= 1) {
                        memset(&rt[i], 0, sizeof(rt[i]));
                    }
                    else {
                        rt[i].lifetime--;
                    }
                }
            }

            /* Parent is NULL for root too */
            if (my_dodag->my_preferred_parent != NULL) {
                if (my_dodag->my_preferred_parent->lifetime <= 1) {
                    puts("parent lifetime timeout");
                    rpl_parent_update(NULL);
                }
                else {
                    my_dodag->my_preferred_parent->lifetime--;
                }
            }
        }

        /* Wake up every second */
        vtimer_usleep(1000000);
    }

    return NULL;
}
Esempio n. 15
0
void leader_elect(){
    char msg_buffer[strlen(PREAMBLE) + strlen(MSG) + strlen(LE) + UID_LEN + ROUND_LEN];
    gossip_node_t* node;

    for(int i=0;i<ROUNDS;i++){
        sprintf(msg_buffer, "%s%s%s%0" ROUND_LEN_STR "i%0" UID_LEN_STR "i",
                PREAMBLE, MSG, LE, election_round, leader);
        node = gossip_get_neighbour(RANDOM);
        if(!node){
            WARN("W: no neighbours, election failed.\n");
            return;
        }
        gossip_send(node, msg_buffer, strlen(msg_buffer));
        vtimer_usleep(1000*1000*10);
    }
    if( leader_get_leader() == gossip_id ){
        timesync_set_trusted(1);
        timesync_set_synced(1);
    }
    leader_set_active(0);
}
Esempio n. 16
0
File: main.c Progetto: AnonMall/RIOT
int main(void)
{
    int res;
    puts("SRF02 ultrasonic ranger test application\n");
    printf("Initializing SRF02 sensor at I2C_%i... ", TEST_SRF02_I2C);
    res = srf02_init(&srf02_0, TEST_SRF02_I2C, SRF02_DEFAULT_ADDR, TEST_SRF02_SPEED);

    if (res < 0) {
        printf("[Failed]");
        return 1;
    }
    else {
        puts("[Ok]\n");

        while(1) {
            uint16_t distance = srf02_get_distance(&srf02_0, TEST_MODE);
            printf("distance = %i cm\n", distance);
            vtimer_usleep(SLEEP);
        }
    }
}
Esempio n. 17
0
int main(void) {
	
	LED_RED_ON;
#ifdef LED_GREEN_OFF
    LED_GREEN_OFF;
#endif

	timer_over_pid = thread_create(timer_over_buf,
	                               KERNEL_CONF_STACKSIZE_MAIN,
	                               PRIORITY_MAIN-1,
	                               CREATE_WOUT_YIELD | CREATE_STACKTEST,
	                               blinker,
	                               "blinker");

	while(1) {
#ifdef LED_GREEN_TOGGLE
	  LED_GREEN_TOGGLE;
#endif
	  vtimer_usleep(SEC);
	}

	return 0;
}
Esempio n. 18
0
/**
 * @print register
 */
void print_register(char reg, int num_bytes)
{

    vtimer_init();

    char buf_return[num_bytes];
    int ret;


    gpio_clear(CS_PIN);
    vtimer_usleep(1);
    ret = spi_transfer_regs(SPI_PORT, (CMD_R_REGISTER | (REGISTER_MASK & reg)), 0, buf_return, num_bytes);
    gpio_set(CS_PIN);

    if (ret < 0) {
        printf("Error in read access\n");
    }
    else {
        if (num_bytes < 2) {
            printf("0x%x returned: ", reg);

            for (int i = 0; i < num_bytes; i++) {
                prtbin(buf_return[i]);
            }
        }
        else {
            printf("0x%x returned: ", reg);

            for (int i = 0; i < num_bytes; i++) {
                printf("%x ", buf_return[i]);
            }

            printf("\n\n");
        }
    }
}
Esempio n. 19
0
void sleep_now(char *str)
	{
	int time;
	sscanf(str, "sleep %i", &time);
	vtimer_usleep(time*1000*1000);
	}
Esempio n. 20
0
File: main.c Progetto: A-L-E-X/RIOT
static void do_sleep(int factor)
{
    uint32_t timeout_us = (genrand_uint32() % 100000) * factor;
    /* PRINTF("sleep for % 8i µs.", timeout_us); */
    vtimer_usleep(timeout_us);
}
Esempio n. 21
0
void boot_server(char *str)
	{
	bootstrapping(NULL);
	vtimer_usleep(1000*1000*2);
	init_tcp_server_thread(NULL);
	}
Esempio n. 22
0
void boot_client(char *str)
	{
	init_tcp_cht(NULL);
	vtimer_usleep(1000*1000*2);
	connect_tcp("connect_tcp 2");
	}
Esempio n. 23
0
int handle_new_tcp_connection(socket_internal_t *current_queued_int_socket,
                              socket_internal_t *server_socket, uint8_t pid)
{
    msg_t msg_recv_client_ack, msg_send_client_ack;
    socket_t *current_queued_socket = &current_queued_int_socket->socket_values;
    uint8_t send_buffer[BUFFER_SIZE];
    ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer));
    tcp_hdr_t *syn_ack_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN]));

    current_queued_int_socket->recv_pid = thread_getpid();
#ifdef TCP_HC
    current_queued_int_socket->socket_values.tcp_control.tcp_context.hc_type =
        FULL_HEADER;
    memcpy(&current_queued_int_socket->socket_values.tcp_control.tcp_context.context_id,
           &server_socket->socket_values.tcp_control.tcp_context.context_id,
           sizeof(server_socket->socket_values.tcp_control.tcp_context.context_id));
#endif
    /* Remember current time */
    timex_t now;
    vtimer_now(&now);
    current_queued_int_socket->socket_values.tcp_control.last_packet_time = now;

    current_queued_int_socket->socket_values.tcp_control.no_of_retries = 0;

    /* Set message type to Retry for while loop */
    msg_recv_client_ack.type = TCP_RETRY;

    while (msg_recv_client_ack.type == TCP_RETRY) {
        /* Send packet */
        send_tcp(current_queued_int_socket, syn_ack_packet, temp_ipv6_header,
                 TCP_SYN_ACK, 0);

        /* wait for ACK from Client */
        msg_receive(&msg_recv_client_ack);

        if (msg_recv_client_ack.type == TCP_TIMEOUT) {
            /* Set status of internal socket back to LISTEN */
            server_socket->socket_values.tcp_control.state = LISTEN;

            close_socket(current_queued_int_socket);
            return -1;
        }
    }

    tcp_hdr_t *tcp_header;

    tcp_header = ((tcp_hdr_t *)(msg_recv_client_ack.content.ptr));

    /* Check for consistency */
    if (tcp_header->ack_nr != current_queued_socket->tcp_control.send_nxt + 1) {
        printf("TCP packets not consistent!\n");
    }

    /* Got ack, connection established, refresh local and foreign tcp socket
     * status */
    set_tcp_cb(&current_queued_socket->tcp_control, tcp_header->seq_nr + 1,
               current_queued_socket->tcp_control.rcv_wnd, tcp_header->ack_nr,
               tcp_header->ack_nr, tcp_header->window);

#ifdef TCP_HC
    /* Copy TCP context information into new socket */
    memset(&server_socket->socket_values.tcp_control.tcp_context, 0,
           sizeof(tcp_hc_context_t));
#endif

    /* Update connection status information */
    current_queued_socket->tcp_control.state = ESTABLISHED;

    /* Set status of internal socket back to LISTEN */
    server_socket->socket_values.tcp_control.state = LISTEN;

    /* send a reply to the TCP handler after processing every information from
     * the TCP ACK packet */
    msg_reply(&msg_recv_client_ack, &msg_send_client_ack);

    /* Reset PID to an unlikely value */
    current_queued_int_socket->recv_pid = 255;

    /* Waiting for Clients ACK waiting period to time out */
    vtimer_usleep(TCP_SYN_INITIAL_TIMEOUT / 2);

    destiny_socket_print_sockets();

    return current_queued_int_socket->socket_id;
}
Esempio n. 24
0
int main(void)
{
    lis3dh_t dev;
    lis3dh_data_t acc_data;
    int16_t temperature;

    puts("LIS3DH accelerometer driver test application\n");
    printf("Initializing SPI_%i... ", TEST_LIS3DH_SPI);
    if (spi_init_master(TEST_LIS3DH_SPI, SPI_CONF_SECOND_FALLING, SPI_SPEED_10MHZ) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Initializing LIS3DH sensor... ");
    if (lis3dh_init(&dev, TEST_LIS3DH_SPI, TEST_LIS3DH_CS,
                      TEST_LIS3DH_INT1, TEST_LIS3DH_INT2, SCALE) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Set ODR... ");
    if (lis3dh_set_odr(&dev, ODR) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Set scale... ");
    if (lis3dh_set_scale(&dev, SCALE) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Set axes XYZ... ");
    if (lis3dh_set_axes(&dev, LIS3DH_AXES_XYZ) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Disable FIFO mode... ");
    if (lis3dh_set_fifo(&dev, 0) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Enable temperature reading... ");
    if (lis3dh_set_aux_adc(&dev, 1, 1) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("LIS3DH init done.\n");

    while (1) {
        lis3dh_read_xyz(&dev, &acc_data);
        if (lis3dh_read_xyz(&dev, &acc_data) != 0) {
            puts("Reading acceleration data... ");
            puts("[Failed]\n");
            return 1;
        }
        if (lis3dh_read_aux_adc3(&dev, &temperature) != 0) {
            puts("Reading temperature data... ");
            puts("[Failed]\n");
            return 1;
        }

        printf("Sensor data - X: %6i   Y: %6i   Z: %6i   Temp: %6i\n",
               acc_data.acc_x, acc_data.acc_y, acc_data.acc_z, temperature);

        vtimer_usleep(SLEEP);
    }

    return 0;
}
Esempio n. 25
0
void led_thread(void)
{
    msg_t msg;
    int ret;

    color_hsv_t hsv_0;
    color_hsv_t hsv_1;
    color_rgb_t rgb;

    float step_h = 1.0f;
    float step_s = 0.02f;
    float step_v = 0.02f;
    int step = 0;

    int state = 1;
    int limit = 1;
    color_rgb_t *sequ = cd_sequ;

    rgbled_t led;

    /* initialize RGB-LED */
    rgbled_init(&led, PWM_0, 0, 1, 2);

    while (1) {
        /* see if something has come up */
        if (state == limit) {
            notify_done();
            ++state;
        }
        if (state >= limit) {
            ret = msg_receive(&msg);
        } else {
            ret = msg_try_receive(&msg);
        }

        /* if message was receive, act on it */
        if (ret == 1) {
            printf("led: got message %i\n", msg.type);
            if (msg.type != last_cmd) {
                switch (msg.type) {
                    case MSG_GAME_START:
                        puts("led: starting game");
                        sequ = cd_sequ;
                        state = 1;
                        limit = GAME_LED_COUNTDOWN_LEN;
                        last_cmd = msg.type;
                        break;
                    case MSG_GAME_SCORE:
                        printf("led: displaying score: %i\n", (unsigned int)msg.content.value);
                        if (PLAYER == msg.content.value) {
                            puts("led: WIN sequence");
                            sequ = win_sequ;
                            limit = GAME_LED_WIN_LEN;
                        }
                        else if (msg.content.value == GAME_STATE_DRAW) {
                            puts("led: DRAW sequence");
                            sequ = draw_sequ;
                            limit = GAME_LED_DRAW_LEN;
                        }
                        else {
                            puts("led: LOSE sequence");
                            sequ = lose_sequ;
                            limit = GAME_LED_LOSE_LEN;
                        }
                        state = 1;
                        last_cmd = msg.type;
                        break;
                }
            }
        }

        if (state < limit) {
            /* update color */
            if (step == 0) {
                printf("led: setting color fr 0x%02x 0x%02x 0x%02x\n",
                       sequ[state - 1].r, sequ[state - 1].g, sequ[state - 1].b);
                printf("led: setting color to 0x%02x 0x%02x 0x%02x\n",
                       sequ[state].r, sequ[state].g, sequ[state].b);

                color_rgb2hsv(&sequ[state - 1], &hsv_0);
                color_rgb2hsv(&sequ[state ], &hsv_1);

                step_h = (hsv_0.h - hsv_1.h) / STEPS;
                step_s = (hsv_0.s - hsv_1.s) / STEPS;
                step_v = (hsv_0.v - hsv_1.v) / STEPS;
                printf("led: steps are h+= %i s+= %i v+= %i\n", (int)(step_h * 100),(int)(step_s * 100),
                                                                (int)(step_v * 100));
            }

            /* set color */
            color_hsv2rgb(&hsv_0, &rgb);
            rgbled_set(&led, &rgb);
            ++step;
            hsv_0.h += step_h;
            hsv_0.s += step_s;
            hsv_0.v += step_v;

            /* check if transition is finished */
            if (step == STEPS) {
                step = 0;
                ++state;
            }
        }

        /* wait for next step */
        vtimer_usleep(PAUSE);
    }
}
Esempio n. 26
0
int main(void)
{
    adt7310_t dev;

    puts("ADT7310 temperature driver test application\n");
    printf("Initializing SPI_%i... ", TEST_ADT7310_SPI);
    if (spi_init_master(TEST_ADT7310_SPI, SPI_CONF, SPI_SPEED) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("Initializing ADT7310 sensor... ");
    if (adt7310_init(&dev, TEST_ADT7310_SPI, TEST_ADT7310_CS) == 0) {
        puts("[OK]");
    }
    else {
        puts("[Failed]\n");
        return 1;
    }

    puts("ADT7310 init done.\n");

    while (1) {
        puts("Set mode to continuous, 16 bit... ");
        if (adt7310_set_config(&dev, ADT7310_MODE_CONTINUOUS | ADT7310_CONF_RESOLUTION(1)) == 0) {
            puts("[OK]");
        }
        else {
            puts("[Failed]\n");
            return 1;
        }

        for (int i = 0; i < READINGS_CONT; ++i)
        {
            printf("%4d: ", i);
            if (test_adt7310_sample_print(&dev) != 0)
            {
                return 1;
            }
            vtimer_usleep(SLEEP_CONT);
        }
        puts("Set mode to 1SPS, 13 bit... ");
        if (adt7310_set_config(&dev, ADT7310_MODE_1SPS) == 0) {
            puts("[OK]");
        }
        else {
            puts("[Failed]\n");
            return 1;
        }

        for (int i = 0; i < READINGS_1SPS; ++i)
        {
            printf("%4d: ", i);
            if (test_adt7310_sample_print(&dev) != 0)
            {
                return 1;
            }
            vtimer_usleep(SLEEP_1SPS);
        }
    }

    return 0;
}
Esempio n. 27
0
void blinker(void) {
	while(1) {
		LED_RED_TOGGLE;
		vtimer_usleep(SEC);
	}
}