This C code snippet is a driver for MaxDetect series sensors using the wiringPi library, providing two functions to wait for pin transitions and read bytes from the MaxDetect bus. The code is licensed under the LGPL version 3 or later and is specific to the MaxDetect series sensors and wiringPi library.
/*
* maxdetect.c:
* Driver for the MaxDetect series sensors
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
#include <wiringPi.h>
#include "maxdetect.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
/*
* maxDetectLowHighWait:
* Wait for a transition from high to low on the bus
*********************************************************************************
*/
static void maxDetectLowHighWait (const int pin)
{
unsigned int timeOut = millis () + 2000 ;
while (digitalRead (pin) == HIGH)
if (millis () > timeOut)
return ;
while (digitalRead (pin) == LOW)
if (millis () > timeOut)
return ;
}
/*
* maxDetectClockByte:
* Read in a single byte from the MaxDetect bus
*********************************************************************************
*/
static unsigned int maxDetectClockByte (const int pin)
{
unsigned int byte = 0 ;
int bit ;
for (bit = 0 ; bit < 8 ; ++bit)
{
maxDetectLowHighWait (pin) ;
// bit starting now - we need to time it.
delayMicroseconds (30) ;
byte <<= 1 ;
if (digitalRead (pin) == HIGH) // It's a 1
byte |= 1 ;
}
return byte ;
}
/*
* maxDetectRead:
* Read in and return the 4 data bytes from the MaxDetect sensor.
* Return TRUE/FALSE depending on the checksum validity
*********************************************************************************
*/
int maxDetectRead (const int pin, unsigned char buffer [4])
{
int i ;
unsigned int checksum ;
unsigned char localBuf [5] ;
// Wake up the RHT03 by pulling the data line low, then high
// Low for 10mS, high for 40uS.
pinMode (pin, OUTPUT) ;
digitalWrite (pin, 0) ; delay (10) ;
digitalWrite (pin, 1) ; delayMicroseconds (40) ;
pinMode (pin, INPUT) ;
// Now wait for sensor to pull pin low
maxDetectLowHighWait (pin) ;
// and read in 5 bytes (40 bits)
for (i = 0 ; i < 5 ; ++i)
localBuf [i] = maxDetectClockByte (pin) ;
checksum = 0 ;
for (i = 0 ; i < 4 ; ++i)
{
buffer [i] = localBuf [i] ;
checksum += localBuf [i] ;
}
checksum &= 0xFF ;
return checksum == localBuf [4] ;
}
/*
* readRHT03:
* Read the Temperature & Humidity from an RHT03 sensor
*********************************************************************************
*/
int readRHT03 (const int pin, int *temp, int *rh)
{
static unsigned int nextTime = 0 ;
static int lastTemp = 0 ;
static int lastRh = 0 ;
static int lastResult = TRUE ;
unsigned char buffer [4] ;
// Don't read more than once a second
if (millis () < nextTime)
{
*temp = lastTemp ;
*rh = lastRh ;
return lastResult ;
}
lastResult = maxDetectRead (pin, buffer) ;
if (lastResult)
{
*temp = lastTemp = (buffer [2] * 256 + buffer [3]) ;
*rh = lastRh = (buffer [0] * 256 + buffer [1]) ;
nextTime = millis () + 2000 ;
return TRUE ;
}
else
{
return FALSE ;
}
}
/**
* maxdetect.c:
* Driver for the MaxDetect series sensors
*
* Copyright (c) 2013 Gordon Henderson.
*
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see .
*/
#include
#include "maxdetect.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
/**
* maxDetectLowHighWait:
* Wait for a transition from high to low on the bus
*
* @param pin Pin number to read from
*/
static void maxDetectLowHighWait(const int pin) {
unsigned int timeOut = millis() + 2000;
while (digitalRead(pin) == HIGH) {
if (millis() > timeOut) {
// Return if timeout occurs
return;
}
}
while (digitalRead(pin) == LOW) {
if (millis() > timeOut) {
// Return if timeout occurs
return;
}
}
}
/**
* maxDetectClockByte:
* Read in a single byte from the MaxDetect bus
*
* @param pin Pin number to read from
* @return Single byte read from the bus
*/
static unsigned int maxDetectClockByte(const int pin) {
unsigned int byte = 0;
int bit;
for (bit = 0; bit < 8; ++bit) {
maxDetectLowHighWait(pin);
// Time the bit
delayMicroseconds(30);
byte <<= 1;
if (digitalRead(pin) == HIGH) {
// Bit is high
byte |= 1;
}
}
return byte;
}
/**
* maxDetectRead:
* Read in and return the 4 data bytes from the MaxDetect sensor
*
* @param pin Pin number to read from
* @param buffer Buffer to store the data bytes
* @return TRUE if checksum is valid, FALSE otherwise
*/
int maxDetectRead(const int pin, unsigned char buffer[4]) {
int i;
unsigned int checksum;
unsigned char localBuf[5];
// Wake up the RHT03 by pulling the data line low, then high
// Low for 10mS, high for 40uS
pinMode(pin, OUTPUT);
digitalWrite(pin, 0);
delay(10);
digitalWrite(pin, 1);
delayMicroseconds(40);
pinMode(pin, INPUT);
// Wait for sensor to pull pin low
maxDetectLowHighWait(pin);
// Read in 5 bytes (40 bits)
for (i = 0; i < 5; ++i) {
localBuf[i] = maxDetectClockByte(pin);
}
checksum = 0;
for (i = 0; i < 4; ++i) {
buffer[i] = localBuf[i];
checksum += localBuf[i];
}
checksum &= 0xFF;
return checksum == localBuf[4];
}
/**
* readRHT03:
* Read the Temperature & Humidity from an RHT03 sensor
*
* @param pin Pin number to read from
* @param temp Buffer to store temperature value
* @param rh Buffer to store humidity value
* @return TRUE if read is successful, FALSE otherwise
*/
int readRHT03(const int pin, int *temp, int *rh) {
static unsigned int nextTime = 0;
static int lastTemp = 0;
static int lastRh = 0;
static int lastResult = TRUE;
unsigned char buffer[4];
// Don't read more than once a second
if (millis() < nextTime) {
*temp = lastTemp;
*rh = lastRh;
return lastResult;
}
lastResult = maxDetectRead(pin, buffer);
if (lastResult) {
// Calculate temperature and humidity
*temp = lastTemp = (buffer[2] * 256 + buffer[3]);
*rh = lastRh = (buffer[0] * 256 + buffer[1]);
nextTime = millis() + 2000;
return TRUE;
} else {
return FALSE;
}
}
Code Breakdown
This is a C code snippet that appears to be a driver for the MaxDetect series sensors. The code is part of the wiringPi project, which is a C library for accessing GPIO pins on the Raspberry Pi.
Includes
The code includes the following header files:
wiringPi.h
: The main header file for the wiringPi library.maxdetect.h
: A header file specific to this driver, which is not shown in this snippet.Copyright and License
The code is licensed under the GNU Lesser General Public License (LGPL) version 3 or later.
Functions
Two functions are defined:
maxDetectLowHighWait(pin)
: Waits for a transition from high to low on the specified pin.maxDetectClockByte(pin)
: Reads a single byte from the MaxDetect bus by sending a clock pulse on the specified pin and reading the resulting signal.Implementation Details
The maxDetectLowHighWait
function uses a timeout of 2000 milliseconds to wait for a transition from high to low on the pin. If the pin remains high or low for too long, the function returns.
The maxDetectClockByte
function reads a single byte from the MaxDetect bus by sending a clock pulse on the specified pin and reading the resulting signal. The clock pulse is timed using the millis
function, which returns the number of milliseconds since the program started. The function uses a loop to read each bit of the byte, waiting for the transition from high to low on the pin before reading the next bit.
Notes
The code is quite specific to the MaxDetect series sensors and the wiringPi library, so it may not be easily adaptable to other systems or sensors.