Initial checkin of empty MCP9808 driver
This commit is contained in:
82
src/mgos_mcp9808.c
Normal file
82
src/mgos_mcp9808.c
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2018 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mgos.h"
|
||||
#include "mgos_mcp9808_internal.h"
|
||||
#include "mgos_i2c.h"
|
||||
|
||||
// Datasheet:
|
||||
// https://cdn-shop.adafruit.com/datasheets/MCP9808.pdf
|
||||
|
||||
// Private functions follow
|
||||
// Private functions end
|
||||
|
||||
// Public functions follow
|
||||
struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr) {
|
||||
struct mgos_mcp9808 *sensor;
|
||||
|
||||
if (!i2c) return NULL;
|
||||
|
||||
sensor=calloc(1, sizeof(struct mgos_mcp9808));
|
||||
if (!sensor) return NULL;
|
||||
sensor->i2caddr=i2caddr;
|
||||
sensor->i2c=i2c;
|
||||
sensor->last_read_time=0;
|
||||
|
||||
// Check for the right chip.
|
||||
if (false) {
|
||||
LOG(LL_INFO, ("MCP9808 created at I2C 0x%02x", i2caddr));
|
||||
return sensor;
|
||||
}
|
||||
|
||||
LOG(LL_ERROR, ("Failed to create MCP9808 at I2C 0x%02x", i2caddr));
|
||||
free(sensor);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) {
|
||||
if (!*sensor) return;
|
||||
free (*sensor);
|
||||
*sensor=NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) {
|
||||
double now = mg_time();
|
||||
|
||||
if (!sensor || !sensor->i2c)
|
||||
return false;
|
||||
|
||||
if (now - sensor->last_read_time < MGOS_MCP9808_READ_DELAY) {
|
||||
return true;
|
||||
}
|
||||
// Read out sensor data here
|
||||
//
|
||||
LOG(LL_DEBUG, ("temperature=%.2fC", sensor->temperature));
|
||||
sensor->last_read_time=now;
|
||||
return true;
|
||||
}
|
||||
|
||||
float mgos_mcp9808_getTemperature(struct mgos_mcp9808 *sensor) {
|
||||
if (!mgos_mcp9808_read(sensor)) return NAN;
|
||||
|
||||
return sensor->temperature;
|
||||
}
|
||||
|
||||
bool mgos_mcp9808_i2c_init(void) {
|
||||
return true;
|
||||
}
|
||||
// Public functions end
|
Reference in New Issue
Block a user