|
| 1 | +/* |
| 2 | + * Copyright 2020 Mircho Mirev <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#ifdef MGOS_HAVE_BH1750 |
| 19 | +#include <math.h> |
| 20 | +#include "mgos.h" |
| 21 | +#include "mgos_homeassistant_bh1750.h" |
| 22 | + |
| 23 | +static void bh1750_timer(void *user_data) { |
| 24 | + struct mgos_homeassistant_object *o = (struct mgos_homeassistant_object *) user_data; |
| 25 | + if (!o) return; |
| 26 | + mgos_homeassistant_object_send_status(o); |
| 27 | +} |
| 28 | + |
| 29 | +static void bh1750_stat_light(struct mgos_homeassistant_object *o, struct json_out *json) { |
| 30 | + struct mgos_homeassistant_bh1750 *d = NULL; |
| 31 | + float lux = NAN; |
| 32 | + |
| 33 | + if (!o || !json) return; |
| 34 | + if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return; |
| 35 | + |
| 36 | + lux = mgos_bh1750_read_lux(d->dev, NULL); |
| 37 | + json_printf(json, "%.1f", lux); |
| 38 | + //initiate another measurment |
| 39 | + mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT ); |
| 40 | +} |
| 41 | + |
| 42 | +static void bh1750_pre_remove_cb(struct mgos_homeassistant_object *o) { |
| 43 | + struct mgos_homeassistant_bh1750 *d = NULL; |
| 44 | + if (!o) return; |
| 45 | + if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return; |
| 46 | + mgos_clear_timer(d->timer); |
| 47 | + if (d->dev) mgos_bh1750_free(d->dev); |
| 48 | + free(o->user_data); |
| 49 | + o->user_data = NULL; |
| 50 | +} |
| 51 | + |
| 52 | +bool mgos_homeassistant_bh1750_fromjson(struct mgos_homeassistant *ha, struct json_token val) { |
| 53 | + int i2caddr = -1; |
| 54 | + int period = 60; |
| 55 | + bool ret = false; |
| 56 | + struct mgos_homeassistant_object *o = NULL; |
| 57 | + struct mgos_homeassistant_bh1750 *d = NULL; |
| 58 | + char object_name[20]; |
| 59 | + char *name = NULL; |
| 60 | + char *nameptr = NULL; |
| 61 | + |
| 62 | + if (!ha) goto exit; |
| 63 | + if (!(d = calloc(1, sizeof(*d)))) goto exit; |
| 64 | + |
| 65 | + json_scanf(val.ptr, val.len, "{i2caddr:%d,period:%d,name:%Q}", &i2caddr, &period, &name); |
| 66 | + d->dev = mgos_bh1750_create(i2caddr); |
| 67 | + |
| 68 | + if (!d->dev) { |
| 69 | + LOG(LL_ERROR, ("Could not create bh1750 at i2caddr=%d", i2caddr)); |
| 70 | + goto exit; |
| 71 | + } |
| 72 | + |
| 73 | + //initiate a one time measurement |
| 74 | + mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT ); |
| 75 | + |
| 76 | + if (!name) { |
| 77 | + mgos_homeassistant_object_generate_name(ha, "bh1750_", object_name, sizeof(object_name)); |
| 78 | + nameptr = object_name; |
| 79 | + } else { |
| 80 | + nameptr = name; |
| 81 | + } |
| 82 | + |
| 83 | + o = mgos_homeassistant_object_add(ha, nameptr, COMPONENT_SENSOR, NULL, NULL, d); |
| 84 | + if (!o) { |
| 85 | + LOG(LL_ERROR, ("Could not add object %s to homeassistant", nameptr)); |
| 86 | + goto exit; |
| 87 | + } |
| 88 | + o->pre_remove_cb = bh1750_pre_remove_cb; |
| 89 | + |
| 90 | + // if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) { |
| 91 | + if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) { |
| 92 | + LOG(LL_ERROR, ("Could not add 'illuminance' class to object %s", nameptr)); |
| 93 | + goto exit; |
| 94 | + } |
| 95 | + |
| 96 | + if (period > 0) d->timer = mgos_set_timer(period * 1000, true, bh1750_timer, o); |
| 97 | + |
| 98 | + ret = true; |
| 99 | + LOG(LL_DEBUG, ("Successfully created object %s", nameptr)); |
| 100 | +exit: |
| 101 | + if (name) free(name); |
| 102 | + if (!ret && o) mgos_homeassistant_object_remove(&o); |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +#endif // MGOS_HAVE_bh1750_I2C |
0 commit comments