Skip to content

Commit 41ba63e

Browse files
committed
Python3 support added
1 parent 2d4678f commit 41ba63e

File tree

1 file changed

+129
-100
lines changed

1 file changed

+129
-100
lines changed

Adafruit_DHT_Driver_Python/dhtreader.c

Lines changed: 129 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -38,142 +38,171 @@
3838

3939
int readDHT(int type, int pin, float *temp_p, float *hum_p)
4040
{
41-
int counter = 0;
42-
int laststate = HIGH;
43-
int i = 0;
44-
int j = 0;
45-
int checksum = 0;
41+
int counter = 0;
42+
int laststate = HIGH;
43+
int i = 0;
44+
int j = 0;
45+
int checksum = 0;
4646
#ifdef DEBUG
47-
int bitidx = 0;
48-
int bits[250];
47+
int bitidx = 0;
48+
int bits[250];
4949
#endif
50-
int data[100];
51-
52-
// Set GPIO pin to output
53-
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
54-
55-
bcm2835_gpio_write(pin, HIGH);
56-
usleep(500000); // 500 ms
57-
bcm2835_gpio_write(pin, LOW);
58-
usleep(20000);
59-
60-
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
61-
62-
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
63-
64-
// wait for pin to drop?
65-
while (bcm2835_gpio_lev(pin) == 1) {
66-
usleep(1);
67-
}
68-
69-
// read data!
70-
for (i = 0; i < MAXTIMINGS; i++) {
71-
counter = 0;
72-
while ( bcm2835_gpio_lev(pin) == laststate) {
73-
counter++;
74-
//nanosleep(1); // overclocking might change this?
75-
if (counter == 1000)
76-
break;
77-
}
78-
laststate = bcm2835_gpio_lev(pin);
79-
if (counter == 1000) break;
50+
int data[100];
51+
52+
// Set GPIO pin to output
53+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
54+
55+
bcm2835_gpio_write(pin, HIGH);
56+
usleep(500000); // 500 ms
57+
bcm2835_gpio_write(pin, LOW);
58+
usleep(20000);
59+
60+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
61+
62+
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
63+
64+
// wait for pin to drop?
65+
while (bcm2835_gpio_lev(pin) == 1) {
66+
usleep(1);
67+
}
68+
69+
// read data!
70+
for (i = 0; i < MAXTIMINGS; i++) {
71+
counter = 0;
72+
while ( bcm2835_gpio_lev(pin) == laststate) {
73+
counter++;
74+
//nanosleep(1); // overclocking might change this?
75+
if (counter == 1000)
76+
break;
77+
}
78+
laststate = bcm2835_gpio_lev(pin);
79+
if (counter == 1000) break;
8080
#ifdef DEBUG
81-
bits[bitidx++] = counter;
81+
bits[bitidx++] = counter;
8282
#endif
8383

84-
if ((i>3) && (i%2 == 0)) {
85-
// shove each bit into the storage bytes
86-
data[j/8] <<= 1;
87-
if (counter > 200)
88-
data[j/8] |= 1;
89-
j++;
90-
}
91-
}
84+
if ((i>3) && (i%2 == 0)) {
85+
// shove each bit into the storage bytes
86+
data[j/8] <<= 1;
87+
if (counter > 200)
88+
data[j/8] |= 1;
89+
j++;
90+
}
91+
}
9292

9393
#ifdef DEBUG
94-
for (int i=3; i<bitidx; i+=2) {
95-
printf("bit %d: %d\n", i-3, bits[i]);
96-
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200);
97-
}
98-
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
94+
for (int i=3; i<bitidx; i+=2) {
95+
printf("bit %d: %d\n", i-3, bits[i]);
96+
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200);
97+
}
98+
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
9999
#endif
100100

101-
if (j >= 39) {
102-
checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;
103-
if (data[4] == checksum) {
104-
/* yay! checksum is valid */
105-
if (type == DHT11) {
106-
/*printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);*/
107-
*temp_p = (float)data[2];
108-
*hum_p = (float)data[0];
109-
} else if (type == DHT22) {
110-
*hum_p = data[0] * 256 + data[1];
111-
*hum_p /= 10;
112-
113-
*temp_p = (data[2] & 0x7F)* 256 + data[3];
114-
*temp_p /= 10.0;
115-
if (data[2] & 0x80)
116-
*temp_p *= -1;
117-
/*printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);*/
118-
}
119-
return 0;
120-
}
121-
return -2;
122-
}
123-
124-
return -1;
101+
if (j >= 39) {
102+
checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;
103+
if (data[4] == checksum) {
104+
/* yay! checksum is valid */
105+
if (type == DHT11) {
106+
/*printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);*/
107+
*temp_p = (float)data[2];
108+
*hum_p = (float)data[0];
109+
} else if (type == DHT22) {
110+
*hum_p = data[0] * 256 + data[1];
111+
*hum_p /= 10;
112+
113+
*temp_p = (data[2] & 0x7F)* 256 + data[3];
114+
*temp_p /= 10.0;
115+
if (data[2] & 0x80)
116+
*temp_p *= -1;
117+
/*printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);*/
118+
}
119+
return 0;
120+
}
121+
return -2;
122+
}
123+
124+
return -1;
125125
}
126126

127127

128128
static PyObject *
129129
dhtreader_init(PyObject *self, PyObject *args)
130130
{
131-
return Py_BuildValue("i", bcm2835_init());
131+
return Py_BuildValue("i", bcm2835_init());
132132
}
133133

134134
static PyObject *
135135
dhtreader_read(PyObject *self, PyObject *args)
136136
{
137-
int type, dhtpin;
137+
int type, dhtpin;
138138

139-
if (!PyArg_ParseTuple(args, "ii", &type, &dhtpin))
140-
return NULL;
139+
if (!PyArg_ParseTuple(args, "ii", &type, &dhtpin))
140+
return NULL;
141141

142-
float t, h;
143-
int re = readDHT(type, dhtpin, &t, &h);
142+
float t, h;
143+
int re = readDHT(type, dhtpin, &t, &h);
144144

145-
if (re == 0) {
146-
return Py_BuildValue("(d,d)", t, h);
147-
} else if (re == -1) {
145+
if (re == 0) {
146+
return Py_BuildValue("(d,d)", t, h);
147+
} else if (re == -1) {
148148
#ifdef DEBUG
149-
printf("sensor read failed! not enough data received\n");
149+
printf("sensor read failed! not enough data received\n");
150150
#endif
151-
} else if (re == -2) {
151+
} else if (re == -2) {
152152
#ifdef DEBUG
153-
printf("sensor read failed! checksum failed!\n");
153+
printf("sensor read failed! checksum failed!\n");
154154
#endif
155-
}
155+
}
156156

157-
return Py_BuildValue("");
157+
return Py_BuildValue("");
158158
}
159159

160160

161161
static PyMethodDef DHTReaderMethods[] = {
162-
{"init", dhtreader_init, METH_VARARGS,
163-
"initialize dht reader"},
164-
{"read", dhtreader_read, METH_VARARGS,
165-
"temperature and humidity from sensor"},
162+
{"init", dhtreader_init, METH_VARARGS,
163+
"initialize dht reader"},
164+
{"read", dhtreader_read, METH_VARARGS,
165+
"temperature and humidity from sensor"},
166166
{NULL, NULL, 0, NULL} /* Sentinel */
167167
};
168168

169-
PyMODINIT_FUNC
170-
initdhtreader(void)
171-
{
172-
PyObject *m;
169+
#if PY_MAJOR_VERSION >= 3
170+
171+
static struct PyModuleDef moduledef = {
172+
PyModuleDef_HEAD_INIT,
173+
"dhtreader",
174+
NULL,
175+
-1,
176+
DHTReaderMethods,
177+
NULL,
178+
NULL,
179+
NULL,
180+
NULL
181+
};
182+
183+
#define INITERROR return NULL
184+
185+
PyObject *
186+
PyInit_myextension(void)
187+
188+
#else
189+
#define INITERROR return
173190

174-
m = Py_InitModule("dhtreader", DHTReaderMethods);
175-
if (m == NULL)
176-
return;
191+
void
192+
initmyextension(void)
193+
#endif
194+
{
195+
#if PY_MAJOR_VERSION >= 3
196+
PyObject *module = PyModule_Create(&moduledef);
197+
#else
198+
PyObject *module = Py_InitModule("dhtreader", DHTReaderMethods);
199+
#endif
200+
if (module == NULL)
201+
INITERROR;
202+
203+
#if PY_MAJOR_VERSION >= 3
204+
return module;
205+
#endif
177206
}
178207

179208

0 commit comments

Comments
 (0)