| 52 | */ |
| 53 | |
| 54 | static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) |
| 55 | { |
| 56 | int chan = pin - node->pinBase ; |
| 57 | int fd = node->fd ; |
| 58 | uint8_t data [4] ; |
| 59 | uint32_t sTemp, sHumid ; |
| 60 | double fTemp, fHumid ; |
| 61 | int cTemp, cHumid ; |
| 62 | |
| 63 | if (chan == 0) // Read Temperature |
| 64 | { |
| 65 | |
| 66 | // Send read temperature command: |
| 67 | |
| 68 | data [0] = 0xF3 ; |
| 69 | if (write (fd, data, 1) != 1) |
| 70 | return -9999 ; |
| 71 | |
| 72 | // Wait then read the data |
| 73 | |
| 74 | delay (50) ; |
| 75 | if (read (fd, data, 3) != 3) |
| 76 | return -9998 ; |
| 77 | |
| 78 | if (!checksum (data)) |
| 79 | return -9997 ; |
| 80 | |
| 81 | // Do the calculation |
| 82 | |
| 83 | sTemp = (data [0] << 8) | data [1] ; |
| 84 | fTemp = -48.85 + 175.72 * (double)sTemp / 63356.0 ; |
| 85 | cTemp = (int)rint (((100.0 * fTemp) + 0.5) / 10.0) ; |
| 86 | return cTemp ; |
| 87 | } |
| 88 | else if (chan == 1) // humidity |
| 89 | { |
| 90 | // Send read humidity command: |
| 91 | |
| 92 | data [0] = 0xF5 ; |
| 93 | if (write (fd, data, 1) != 1) |
| 94 | return -9999 ; |
| 95 | |
| 96 | // Wait then read the data |
| 97 | |
| 98 | delay (50) ; |
| 99 | if (read (fd, data, 3) != 3) |
| 100 | return -9998 ; |
| 101 | |
| 102 | if (!checksum (data)) |
| 103 | return -9997 ; |
| 104 | |
| 105 | sHumid = (data [0] << 8) | data [1] ; |
| 106 | fHumid = -6.0 + 125.0 * (double)sHumid / 65536.0 ; |
| 107 | cHumid = (int)rint (((100.0 * fHumid) + 0.5) / 10.0) ; |
| 108 | return cHumid ; |
| 109 | } |
| 110 | else |
| 111 | return -9999 ; |