Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | /* * Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net> * * SPDX-License-Identifier: Apache-2.0 */ #include <stdio.h> #include <zephyr/zephyr.h> #include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor/tmp108.h> void temperature_one_shot(const struct device *dev, const struct sensor_trigger *trigger) { struct sensor_value temp_value; int result; result = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp_value); if (result) { printf("error: sensor_channel_get failed: %d\n", result); return; } printf("One shot power saving mode enabled, temperature is %gC\n", sensor_value_to_double(&temp_value)); } void temperature_alert(const struct device *dev, const struct sensor_trigger *trigger) { struct sensor_value temp_flags = { 0 }; sensor_attr_get(dev, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_CONFIGURATION, &temp_flags); if (OVER_TEMP_MASK & temp_flags.val1) { printf("High temperature alert!\n"); } else if (UNDER_TEMP_MASK & temp_flags.val1) { printf("Low temperature alert!\n"); } else { printf("Temperature alert cleared!\n"); } } void enable_temp_alerts(const struct device *tmp108) { struct sensor_trigger sensor_trigger_type_temp_alert = { .chan = SENSOR_CHAN_AMBIENT_TEMP, .type = SENSOR_TRIG_THRESHOLD }; struct sensor_value alert_upper_thresh = { CONFIG_APP_TEMP_ALERT_HIGH_THRESH, 0 }; struct sensor_value alert_lower_thresh = { CONFIG_APP_TEMP_ALERT_LOW_THRESH, 0 }; struct sensor_value alert_hysterisis = { 1, 0 }; struct sensor_value thermostat_mode = { 0, 0 }; sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_ALERT, &thermostat_mode); sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_HYSTERESIS, &alert_hysterisis); sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_UPPER_THRESH, &alert_upper_thresh); sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_LOWER_THRESH, &alert_lower_thresh); sensor_trigger_set(tmp108, &sensor_trigger_type_temp_alert, temperature_alert); } void enable_one_shot(const struct device *tmp108) { struct sensor_trigger sensor_trigger_type_temp_one_shot = { .chan = SENSOR_CHAN_AMBIENT_TEMP, .type = SENSOR_TRIG_DATA_READY }; sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_TMP108_ONE_SHOT_MODE, NULL); sensor_trigger_set(tmp108, &sensor_trigger_type_temp_one_shot, temperature_one_shot); } void get_temperature_continuous(const struct device *tmp108) { struct sensor_value temp_value; int result; result = sensor_channel_get(tmp108, SENSOR_CHAN_AMBIENT_TEMP, &temp_value); if (result) { printf("error: sensor_channel_get failed: %d\n", result); return; } printf("temperature is %gC\n", sensor_value_to_double(&temp_value)); } void main(void) { const struct device *tmp108; int result; printf("TI TMP108 Example, %s\n", CONFIG_ARCH); tmp108 = DEVICE_DT_GET_ANY(ti_tmp108); if (!tmp108) { printf("error: no tmp108 device found\n"); return; } if (!device_is_ready(tmp108)) { printf("error: tmp108 device not ready\n"); return; } sensor_attr_set(tmp108, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_TMP108_CONTINUOUS_CONVERSION_MODE, NULL); #if CONFIG_APP_ENABLE_ONE_SHOT enable_one_shot(tmp108); #endif #if CONFIG_APP_REPORT_TEMP_ALERTS enable_temp_alerts(tmp108); #endif while (1) { result = sensor_sample_fetch(tmp108); if (result) { printf("error: sensor_sample_fetch failed: %d\n", result); break; } #if !CONFIG_APP_ENABLE_ONE_SHOT get_temperature_continuous(tmp108); #endif k_sleep(K_MSEC(3000)); } } |