/** * Initializes the temperature sensor. * This method is called when the sensor is first created and also any time the sensor reports it's disconnected. * If the result is TEMP_SENSOR_DISCONNECTED then subsequent calls to read() will also return TEMP_SENSOR_DISCONNECTED. * Clients should attempt to re-initialize the sensor by calling init() again. */ bool OneWireTempSensor::init(){ // save address and pinNr for log messages char addressString[17]; printBytes(sensorAddress, 8, addressString); uint8_t pinNr = oneWire->pinNr(); bool success = false; if (sensor==NULL) { sensor = new DallasTemperature(oneWire); if (sensor==NULL) { logErrorString(ERROR_SRAM_SENSOR, addressString); } } logDebug("init onewire sensor"); // This quickly tests if the sensor is connected and initializes the reset detection. // During the main TempControl loop, we don't want to spend many seconds // scanning each sensor since this brings things to a halt. if (sensor && sensor->initConnection(sensorAddress) && requestConversion()) { logDebug("init onewire sensor - wait for conversion"); waitForConversion(); temperature temp = readAndConstrainTemp(); DEBUG_ONLY(logInfoIntStringTemp(INFO_TEMP_SENSOR_INITIALIZED, pinNr, addressString, temp)); success = temp!=DEVICE_DISCONNECTED && requestConversion(); } setConnected(success); logDebug("init onewire sensor complete %d", success); return success; }
int myAnalogRead (struct wiringPiNodeStruct *node, int chan) { unsigned char config ; unsigned char buffer [4] ; int value = 0 ; int realChan = (chan & 3) - node->pinBase ; // One-shot mode, trigger plus the other configs. config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ; wiringPiI2CWrite (node->fd, config) ; switch (node->data0) // Sample rate { case MCP3422_SR_3_75: // 18 bits waitForConversion (node->fd, &buffer [0], 4) ; value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ; break ; case MCP3422_SR_15: // 16 bits waitForConversion (node->fd, buffer, 3) ; value = (buffer [0] << 8) | buffer [1] ; break ; case MCP3422_SR_60: // 14 bits waitForConversion (node->fd, buffer, 3) ; value = ((buffer [0] & 0x3F) << 8) | buffer [1] ; break ; case MCP3422_SR_240: // 12 bits - default waitForConversion (node->fd, buffer, 3) ; value = ((buffer [0] & 0x0F) << 8) | buffer [1] ; break ; } return value ; }
/** * Initializes the temperature sensor. * This method is called when the sensor is first created and also any time the sensor reports it's disconnected. * If the result is TEMP_SENSOR_DISCONNECTED then subsequent calls to read() will also return TEMP_SENSOR_DISCONNECTED. * Clients should attempt to re-initialize the sensor by calling init() again. */ bool OneWireTempSensor::init() { // save address and pinNr for log messages char addressString[17]; printBytes(sensorAddress, 8, addressString); #if BREWPI_DEBUG uint8_t pinNr = oneWire->pinNr(); #endif bool success = false; if (sensor == NULL) { sensor = new DallasTemperature(oneWire); if (sensor == NULL) { logErrorString(ERROR_SRAM_SENSOR, addressString); } } logDebug("init onewire sensor"); // This quickly tests if the sensor is connected and initializes the reset detection if necessary. if (sensor){ // If this is the first conversion after power on, the device will return DEVICE_DISCONNECTED_RAW // Because HIGH_ALARM_TEMP will be copied from EEPROM int16_t temp = sensor->getTempRaw(sensorAddress); if(temp == DEVICE_DISCONNECTED_RAW){ // Device was just powered on and should be initialized if(sensor->initConnection(sensorAddress)){ requestConversion(); waitForConversion(); temp = sensor->getTempRaw(sensorAddress); } } DEBUG_ONLY(logInfoIntStringTemp(INFO_TEMP_SENSOR_INITIALIZED, pinNr, addressString, temp)); success = temp != DEVICE_DISCONNECTED_RAW; if(success){ requestConversion(); // piggyback request for a new conversion } } setConnected(success); logDebug("init onewire sensor complete %d", success); return success; }