The firmware of the Grove Node
Dependencies: BLE_API color_pixels mbed-src-nrf51822 nRF51822
Fork of BLE_LoopbackUART by
udriver/temperature_sensor.cpp@11:c0885b74a63a, 2015-06-04 (annotated)
- Committer:
- yihui
- Date:
- Thu Jun 04 12:39:32 2015 +0000
- Revision:
- 11:c0885b74a63a
- Parent:
- 10:f34ff4e47741
fixed direct control bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 10:f34ff4e47741 | 1 | |
yihui | 10:f34ff4e47741 | 2 | #include "udriver.h" |
yihui | 10:f34ff4e47741 | 3 | #include "mbed.h" |
yihui | 10:f34ff4e47741 | 4 | |
yihui | 10:f34ff4e47741 | 5 | int temperature_sensor_init(void *obj, void *params) |
yihui | 10:f34ff4e47741 | 6 | { |
yihui | 10:f34ff4e47741 | 7 | int pin = *(int *)params; |
yihui | 10:f34ff4e47741 | 8 | *(AnalogIn **)obj = new AnalogIn((PinName)pin); |
yihui | 10:f34ff4e47741 | 9 | |
yihui | 10:f34ff4e47741 | 10 | return 0; |
yihui | 10:f34ff4e47741 | 11 | } |
yihui | 10:f34ff4e47741 | 12 | |
yihui | 10:f34ff4e47741 | 13 | |
yihui | 10:f34ff4e47741 | 14 | int temperature_sensor_read(void *obj, void *data) |
yihui | 10:f34ff4e47741 | 15 | { |
yihui | 10:f34ff4e47741 | 16 | const uint32_t beta = 3975; |
yihui | 10:f34ff4e47741 | 17 | float a; |
yihui | 10:f34ff4e47741 | 18 | float temperature; |
yihui | 10:f34ff4e47741 | 19 | float resistance; |
yihui | 10:f34ff4e47741 | 20 | AnalogIn *probe = *(AnalogIn **)obj; |
yihui | 10:f34ff4e47741 | 21 | |
yihui | 10:f34ff4e47741 | 22 | a = probe->read(); |
yihui | 10:f34ff4e47741 | 23 | |
yihui | 10:f34ff4e47741 | 24 | /* Calculate the resistance of the thermistor from analog votage read. */ |
yihui | 10:f34ff4e47741 | 25 | resistance = (float) 10000.0 * ((1 / a) - 1); |
yihui | 10:f34ff4e47741 | 26 | |
yihui | 10:f34ff4e47741 | 27 | /* Convert the resistance to temperature using Steinhart's Hart equation */ |
yihui | 10:f34ff4e47741 | 28 | temperature = (1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; |
yihui | 10:f34ff4e47741 | 29 | |
yihui | 10:f34ff4e47741 | 30 | *(float *)data = temperature; |
yihui | 10:f34ff4e47741 | 31 | |
yihui | 10:f34ff4e47741 | 32 | return 0; |
yihui | 10:f34ff4e47741 | 33 | } |
yihui | 10:f34ff4e47741 | 34 | |
yihui | 10:f34ff4e47741 | 35 | int temperature_sensor_write(void *obj, void *data) |
yihui | 10:f34ff4e47741 | 36 | { |
yihui | 10:f34ff4e47741 | 37 | return 0; |
yihui | 10:f34ff4e47741 | 38 | } |
yihui | 10:f34ff4e47741 | 39 | |
yihui | 10:f34ff4e47741 | 40 | int temperature_sensor_fini(void *obj) |
yihui | 10:f34ff4e47741 | 41 | { |
yihui | 10:f34ff4e47741 | 42 | AnalogIn *ptr = *(AnalogIn **)obj; |
yihui | 10:f34ff4e47741 | 43 | delete ptr; |
yihui | 10:f34ff4e47741 | 44 | |
yihui | 10:f34ff4e47741 | 45 | return 0; |
yihui | 10:f34ff4e47741 | 46 | } |
yihui | 10:f34ff4e47741 | 47 | |
yihui | 10:f34ff4e47741 | 48 | driver_t temperature_sensor_driver = |
yihui | 10:f34ff4e47741 | 49 | { |
yihui | 10:f34ff4e47741 | 50 | .init = temperature_sensor_init, |
yihui | 10:f34ff4e47741 | 51 | .read = temperature_sensor_read, |
yihui | 10:f34ff4e47741 | 52 | .write = temperature_sensor_write, |
yihui | 10:f34ff4e47741 | 53 | .fini = temperature_sensor_fini, |
yihui | 10:f34ff4e47741 | 54 | |
yihui | 10:f34ff4e47741 | 55 | .d = 1, |
yihui | 10:f34ff4e47741 | 56 | }; |