|
38 | 38 |
|
39 | 39 | int readDHT(int type, int pin, float *temp_p, float *hum_p)
|
40 | 40 | {
|
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; |
46 | 46 | #ifdef DEBUG
|
47 |
| - int bitidx = 0; |
48 |
| - int bits[250]; |
| 47 | + int bitidx = 0; |
| 48 | + int bits[250]; |
49 | 49 | #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; |
80 | 80 | #ifdef DEBUG
|
81 |
| - bits[bitidx++] = counter; |
| 81 | + bits[bitidx++] = counter; |
82 | 82 | #endif
|
83 | 83 |
|
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 | + } |
92 | 92 |
|
93 | 93 | #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]); |
99 | 99 | #endif
|
100 | 100 |
|
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; |
125 | 125 | }
|
126 | 126 |
|
127 | 127 |
|
128 | 128 | static PyObject *
|
129 | 129 | dhtreader_init(PyObject *self, PyObject *args)
|
130 | 130 | {
|
131 |
| - return Py_BuildValue("i", bcm2835_init()); |
| 131 | + return Py_BuildValue("i", bcm2835_init()); |
132 | 132 | }
|
133 | 133 |
|
134 | 134 | static PyObject *
|
135 | 135 | dhtreader_read(PyObject *self, PyObject *args)
|
136 | 136 | {
|
137 |
| - int type, dhtpin; |
| 137 | + int type, dhtpin; |
138 | 138 |
|
139 |
| - if (!PyArg_ParseTuple(args, "ii", &type, &dhtpin)) |
140 |
| - return NULL; |
| 139 | + if (!PyArg_ParseTuple(args, "ii", &type, &dhtpin)) |
| 140 | + return NULL; |
141 | 141 |
|
142 |
| - float t, h; |
143 |
| - int re = readDHT(type, dhtpin, &t, &h); |
| 142 | + float t, h; |
| 143 | + int re = readDHT(type, dhtpin, &t, &h); |
144 | 144 |
|
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) { |
148 | 148 | #ifdef DEBUG
|
149 |
| - printf("sensor read failed! not enough data received\n"); |
| 149 | + printf("sensor read failed! not enough data received\n"); |
150 | 150 | #endif
|
151 |
| - } else if (re == -2) { |
| 151 | + } else if (re == -2) { |
152 | 152 | #ifdef DEBUG
|
153 |
| - printf("sensor read failed! checksum failed!\n"); |
| 153 | + printf("sensor read failed! checksum failed!\n"); |
154 | 154 | #endif
|
155 |
| - } |
| 155 | + } |
156 | 156 |
|
157 |
| - return Py_BuildValue(""); |
| 157 | + return Py_BuildValue(""); |
158 | 158 | }
|
159 | 159 |
|
160 | 160 |
|
161 | 161 | 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"}, |
166 | 166 | {NULL, NULL, 0, NULL} /* Sentinel */
|
167 | 167 | };
|
168 | 168 |
|
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 |
173 | 190 |
|
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 |
177 | 206 | }
|
178 | 207 |
|
179 | 208 |
|
0 commit comments