/* 2014/8/8 Rev1.0 Copyright (C) Sadaji Sunaga This module is free. */ #include #include #include #include #include #include #include #include #include "my_i2c_sensors.h" #include #include #define I2C_NAME "/dev/i2c-3" #define MAX_SENSORS 2 #define ID_BASE SENSORS_HANDLE_BASE #define ID_TEMPERATURE (ID_BASE + 0) #define ID_PRESSURE (ID_TEMPERATURE + 1) struct sensors_poll_device_t g_device; sensors_event_t g_sensorEvents[MAX_SENSORS]; int g_fd; int64_t g_delay[MAX_SENSORS]; static const struct sensor_t g_SensorList[] = { { .name = "LPS331", .vendor = "ST", .version = 1, .handle = ID_TEMPERATURE, .type = SENSOR_TYPE_TEMPERATURE, .maxRange = 85.0f, .resolution = 0.06f, .power = 0.006f, .reserved = {} }, { .name = "LPS331", .vendor = "ST", .version = 1, .handle = ID_PRESSURE, .type = SENSOR_TYPE_PRESSURE, .maxRange = 1260.0f, .resolution = 0.1f, .power = 0.0003f, .reserved = {} }, }; static int64_t get_current_ns() { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; } void init_sensors() { int i; memset(&g_sensorEvents, 0, sizeof(g_sensorEvents)); int64_t currentNs = get_current_ns(); for(i = 0; i < MAX_SENSORS; i++) { g_sensorEvents[i].version = sizeof(sensors_event_t); g_sensorEvents[i].sensor = i; g_sensorEvents[i].type = g_SensorList[i].type; g_sensorEvents[i].timestamp = currentNs; } g_delay[0] = ms2ns(4000); g_delay[1] = ms2ns(2000); } static void open_DeviceFile() { if (g_fd < 0) { g_fd = openI2C(I2C_NAME); } } //sleepしているセンサーを起動する //起動しているセンサーをsleepにする static int sensor_activate(struct sensors_poll_device_t *dev, int handle, int enabled) { if(enabled == 1) { switch(handle) { case ID_TEMPERATURE: case ID_PRESSURE: activateLPS331AP(g_fd); break; } } return 0; } int getSensorData(int index, int64_t nsCurTime) { // fprintf(stderr, "%s: index=%d\n", __FUNCTION__, index); if(nsCurTime >= g_sensorEvents[index].timestamp + g_delay[index]) { g_sensorEvents[index].timestamp = nsCurTime; } else { return -1; } int fd = g_fd; // get data switch(index) { case ID_TEMPERATURE: g_sensorEvents[index].temperature = getTempareture(fd); break; case ID_PRESSURE: g_sensorEvents[index].pressure = getPressure(fd); break; default: break; } return 1; } static int getPolling(sensors_event_t* pSnsEvet, int index, int64_t nsCurTime) { fprintf(stderr, "%s: index=%d\n", __FUNCTION__, index); if(getSensorData(index, nsCurTime) == -1) { return -1; } *pSnsEvet = g_sensorEvents[index]; return 1; } static int device_common_close(struct hw_device_t* dev) { close(g_fd); if (g_fd >= 0) { close(g_fd); } g_fd = -1; return 0; } static int device_poll(struct sensors_poll_device_t *dev, sensors_event_t* pSnsEvet, int count) { int i, index, ret, detectCnt; detectCnt = 0; int64_t cur = get_current_ns(); for (i = 0; i < MAX_SENSORS; i++) { ret = getPolling(pSnsEvet, i, cur); if(ret ==1) { detectCnt++; pSnsEvet++; } } return detectCnt; } static int device_activate(struct sensors_poll_device_t *dev, int handle, int enabled) { int ret; // fprintf(stderr, "%s: dev=%p handle=%x enable=%d\n", __FUNCTION__, dev, handle, enabled); init_sensors(); if (g_fd < 0) { open_DeviceFile(); } ret = sensor_activate(dev, handle, enabled); return ret; } static int device_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns) { fprintf(stderr, "%s: handle=%d ns=%"PRId64"\n", __FUNCTION__, handle, ns); // g_delay[handle] = ns; return 0; } static int get_sensors_list(struct sensors_module_t* module, struct sensor_t const** list) { *list = g_SensorList; return MAX_SENSORS; } //OSとのインターフェース static int open_sensors(const struct hw_module_t* module, const char* name, struct hw_device_t* *device) { int status = -EINVAL; if (!strcmp(name, SENSORS_HARDWARE_POLL)) { memset(&g_device, 0, sizeof(g_device)); g_device.common.tag = HARDWARE_DEVICE_TAG; g_device.common.version = 0; g_device.common.module = (struct hw_module_t*)module; g_device.common.close = device_common_close; g_device.poll = device_poll; g_device.activate = device_activate; g_device.setDelay = device_setDelay; g_fd = -1; *device = &g_device.common; status = 0; } return status; } static struct hw_module_methods_t sensors_module_methods = { .open = open_sensors }; struct sensors_module_t HAL_MODULE_INFO_SYM = { .common = { .tag = HARDWARE_MODULE_TAG, .version_major = 1, .version_minor = 0, .id = SENSORS_HARDWARE_MODULE_ID, .name = "MySensors", .author = "Sadaji Sunaga", .methods = &sensors_module_methods, }, .get_sensors_list = get_sensors_list };