Clean temperature code
This commit is contained in:
parent
d7fc147111
commit
62ff08c654
@ -10,12 +10,12 @@
|
|||||||
|
|
||||||
void *watch_temperature(void *arg)
|
void *watch_temperature(void *arg)
|
||||||
{
|
{
|
||||||
init_temperature();
|
temperature_handle_t temp_handle = init_temperature();
|
||||||
|
|
||||||
get_temperature();
|
get_temperature(temp_handle);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
double temperature = get_temperature();
|
double temperature = get_temperature(temp_handle);
|
||||||
|
|
||||||
char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1);
|
char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1);
|
||||||
sprintf(str, "%lf", temperature);
|
sprintf(str, "%lf", temperature);
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include "temperature.h"
|
||||||
|
|
||||||
#define MPC9808_BUS "/dev/i2c-2"
|
#define MPC9808_BUS "/dev/i2c-2"
|
||||||
#define MPC9808_ADR 0x18
|
#define MPC9808_ADR 0x18
|
||||||
|
|
||||||
@ -21,14 +23,12 @@
|
|||||||
#define DEVID_REG 0x07
|
#define DEVID_REG 0x07
|
||||||
#define RES_REG 0x08
|
#define RES_REG 0x08
|
||||||
|
|
||||||
int file;
|
|
||||||
|
|
||||||
uint8_t get_byte_in_integer(int num, int n)
|
uint8_t get_byte_in_integer(int num, int n)
|
||||||
{
|
{
|
||||||
return (num >> (8*n)) & 0xff;
|
return (num >> (8*n)) & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
double get_temperature()
|
double get_temperature(temperature_handle_t file)
|
||||||
{
|
{
|
||||||
double temperature;
|
double temperature;
|
||||||
int32_t reg32;
|
int32_t reg32;
|
||||||
@ -51,9 +51,10 @@ double get_temperature()
|
|||||||
return temperature;
|
return temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_temperature(void)
|
temperature_handle_t init_temperature(void)
|
||||||
{
|
{
|
||||||
file = open(MPC9808_BUS, O_RDWR);
|
int file = open(MPC9808_BUS, O_RDWR);
|
||||||
|
|
||||||
if (file < 0) {
|
if (file < 0) {
|
||||||
fprintf(stderr, "Error opening temperature sensor device (%s): %s\n", MPC9808_BUS, strerror(errno));
|
fprintf(stderr, "Error opening temperature sensor device (%s): %s\n", MPC9808_BUS, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -71,7 +72,7 @@ void init_temperature(void)
|
|||||||
// Read manufactorer ID
|
// Read manufactorer ID
|
||||||
reg32 = i2c_smbus_read_word_data(file, MANID_REG);
|
reg32 = i2c_smbus_read_word_data(file, MANID_REG);
|
||||||
|
|
||||||
if ( reg32 < 0 ) {
|
if (reg32 < 0) {
|
||||||
fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", MANID_REG,strerror(errno));
|
fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", MANID_REG,strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -90,5 +91,7 @@ void init_temperature(void)
|
|||||||
fprintf(stderr, "Manufactorer ID OK but device ID wrong is 0x%x should be 0x4\n",reg8poi[0]);
|
fprintf(stderr, "Manufactorer ID OK but device ID wrong is 0x%x should be 0x4\n",reg8poi[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
void init_temperature();
|
typedef int temperature_handle_t;
|
||||||
|
|
||||||
double get_temperature();
|
temperature_handle_t init_temperature();
|
||||||
|
|
||||||
|
double get_temperature(temperature_handle_t handle);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user