static void twi_light_sampling(void * p_context){
	UNUSED_PARAMETER(p_context);	
	static bool startup = true;
	static bool set_pin = true;
	uint32_t err_code;
	if(do_measurements){
		if(set_pin){
			set_pin = false;
			//TURN ON THE TRANSISTOR AND WAIT FOR IT TO STABILIZE
			nrf_gpio_pin_set(TSL2561_BASE_PIN);
			err_code = app_timer_start(iss_struct.meas_timer, APP_TIMER_TICKS(50, APP_TIMER_PRESCALER), NULL);
			APP_ERROR_CHECK(err_code);
		}else{
			uint8_t twi_data_tx[2] = {0,0};
			uint8_t twi_data_rx[2] = {0,0};
			uint8_t light_twi_command = TSL2561_COMMAND_BIT | TSL2561_CLEAR_BIT | TSL2561_WORD_BIT;
			if(startup){
				// POWER ON and wait 450 ms
				startup = false;
				twi_data_tx[0] = light_twi_command | TSL2561_REGISTER_CONTROL;
				twi_data_tx[1] = TSL2561_CONTROL_POWERON;
				err_code = !twi_master_write(light_sens_adr, twi_data_tx, 2,&my_twi_config);
				APP_ERROR_CHECK(err_code);
				err_code = app_timer_start(iss_struct.meas_timer, APP_TIMER_TICKS(450, APP_TIMER_PRESCALER), NULL);
				APP_ERROR_CHECK(err_code);
			}else{
				uint16_t ch0,ch1 = 0;
				twi_data_tx[0] = light_twi_command | TSL2561_REGISTER_CHAN0_LOW;	
				err_code = !twi_master_write_read(light_sens_adr, twi_data_tx, 1, twi_data_rx, 2,&my_twi_config);
				APP_ERROR_CHECK(err_code);
				ch0 = twi_data_rx[0];
				ch0 = ch0 | (((uint16_t)(twi_data_rx[1]))<<8);
				
				twi_data_tx[0] = light_twi_command | TSL2561_REGISTER_CHAN1_LOW;
				err_code = !twi_master_write_read(light_sens_adr, twi_data_tx, 1, twi_data_rx, 2,&my_twi_config);
				APP_ERROR_CHECK(err_code);
				ch1 = twi_data_rx[0];
				ch1 = ch1 | (((uint16_t)(twi_data_rx[1]))<<8);
				
				nrf_gpio_pin_clear(TSL2561_BASE_PIN);

				int32_t lux = calc_lux(ch0,ch1);
				err_code = update_iss_measurement(&iss_struct, &lux);
				APP_ERROR_CHECK(err_code);
				startup = true;
				set_pin = true;
				err_code = app_timer_start(iss_struct.meas_timer, APP_TIMER_TICKS(iss_struct.samp_freq_in_m_sec, APP_TIMER_PRESCALER), &iss_struct);
				APP_ERROR_CHECK(err_code);
			}	
		}		
	}
}
static void twi_ht_read_measurement(void * p_context){
	UNUSED_PARAMETER(p_context);	
	static bool toggle = false;
	uint32_t err_code;
	if(do_measurements ){
			toggle = !toggle;
			if(toggle){
				uint8_t twi_data_tx[2] = {0,0};
				uint8_t twi_data_rx[2] = {0,0};
				uint16_t hum = 0;
				if(first_run){
					first_run = false;
					twi_data_tx[0] = SI7021_WRITE_USER_REG;	
					twi_data_tx[1] = SI7021_USER_REG_CONFIG;
					err_code = !twi_master_write(SI7021_ADR, twi_data_tx, 2,&my_twi_config);
					APP_ERROR_CHECK(err_code);
				}
				twi_data_tx[0] = SI7021_MEASURE_RH_COM;	
				err_code = !twi_master_write_read(SI7021_ADR, twi_data_tx, 1, twi_data_rx, 2,&my_twi_config);
				APP_ERROR_CHECK(err_code);
				hum = twi_data_rx[0]<< 8 | twi_data_rx[1];
				
				twi_data_tx[0] = SI7021_GET_TEMP_COM;	
				err_code = !twi_master_write_read(SI7021_ADR, twi_data_tx, 1, twi_data_rx, 2,&my_twi_config);
				APP_ERROR_CHECK(err_code);
				uint16_t last_temp_value  = (twi_data_rx[0] << 8) | twi_data_rx[1];
				int32_t humidity = (((125.0 * hum)/65536)-6.0)*10.0;
				err_code = update_iss_measurement(&iss_struct_h, &humidity);
				APP_ERROR_CHECK(err_code);
				temperature = (((175.72 * last_temp_value)/65536)-46.85) * 10.0;
				err_code = app_timer_start(iss_struct_h.meas_timer, APP_TIMER_TICKS(150, APP_TIMER_PRESCALER), NULL);
				APP_ERROR_CHECK(err_code);
			}else{
				err_code = update_iss_measurement(&iss_struct_t, &temperature);
				APP_ERROR_CHECK(err_code);
				err_code = app_timer_start(iss_struct_h.meas_timer, APP_TIMER_TICKS(iss_struct_h.samp_freq_in_m_sec, APP_TIMER_PRESCALER), NULL);
				APP_ERROR_CHECK(err_code);
			}
	}		
}
Exemplo n.º 3
0
static void ds2483_write_read(ds2483_dev_t *dev, uint8_t txbytes,
                                     uint8_t *txbuf, uint8_t rxbytes, uint8_t *rxbuf) {
	twi_master_write_read(dev->twim, DS2483_I2C_ADDR, txbytes, txbuf, rxbytes, rxbuf);
}