Ran clang-format

This commit is contained in:
2023-10-28 17:28:13 +01:00
parent ebd2909d15
commit 5c6a89df87
6 changed files with 116 additions and 131 deletions

View File

@@ -1,6 +1,6 @@
#include "dht22.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "sdkconfig.h"
#define DATA_GPIO CONFIG_DHT22_DATA_GPIO
@@ -8,8 +8,8 @@
#error "Please define your DHT22 GPIO Data Pin"
#endif /* CONFIG_DHT22_DATA_GPIO == -1 */
static const char* TAG = "DHT22";
deci_percent _dht22_relative_humidity = UINT16_MAX;
static const char *TAG = "DHT22";
deci_percent _dht22_relative_humidity = UINT16_MAX;
deci_degrees_c _dht22_temperature = INT16_MIN;
void dht22_handle_error(dht22_error e) {
@@ -26,12 +26,13 @@ dht22_error dht22_read() {
uint8_t data[5] = {0};
// TODO: Actually figure out how to read the data from the chip.
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm shifting them to just zeros
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm
// shifting them to just zeros
_dht22_relative_humidity = data[0] << 8 | data[1];
_dht22_temperature = (data[2] & 0x7f) << 8 | data[3];
if (data[2] & 0x80) {
_dht22_temperature = -1 * _dht22_temperature;
_dht22_temperature = -1 * _dht22_temperature;
}
uint8_t checksum_val = (data[0] + data[1] + data[2] + data[3]) & 0xff;
if (checksum_val != data[4]) {
@@ -39,13 +40,9 @@ dht22_error dht22_read() {
_dht22_temperature = INT16_MIN;
return DHT22_CHECKSUM_ERROR;
}
return DHT22_OK;
}
deci_percent dht22_relative_humidity() {
return _dht22_relative_humidity;
}
deci_degrees_c dht22_temperature() {
return _dht22_temperature;
}
deci_percent dht22_relative_humidity() { return _dht22_relative_humidity; }
deci_degrees_c dht22_temperature() { return _dht22_temperature; }

View File

@@ -9,11 +9,9 @@ typedef enum {
} dht22_error;
typedef uint16_t deci_percent;
typedef int16_t deci_degrees_c;
typedef int16_t deci_degrees_c;
void dht22_handle_error();
deci_percent dht22_relative_humidity();
deci_degrees_c dht22_temperature();
dht22_error dht22_read();

View File

@@ -1,4 +1,4 @@
int test() {
int i = 5;
return i;
int i = 5;
return i;
}

View File

@@ -1,10 +1,10 @@
#include "wifi.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_sntp.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_sntp.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include <string.h>
static const char *TAG = "WIFI";
@@ -13,88 +13,79 @@ static bool ntp_time_obtained = false;
#define WIFI_CONNECTED_BIT (1 << 0)
bool has_ntp_time_obtained_once() {
return ntp_time_obtained;
}
bool has_ntp_time_obtained_once() { return ntp_time_obtained; }
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
ESP_LOGI(TAG, "connecting...");
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect();
ESP_LOGI(TAG,"connect to the AP fail - trying to reconnect");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
ESP_LOGI(TAG, "connecting...");
} else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect();
ESP_LOGI(TAG, "connect to the AP fail - trying to reconnect");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void start_wifi() {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
s_wifi_event_group = xEventGroupCreate();
s_wifi_event_group = xEventGroupCreate();
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
.failure_retry_cnt = 20,
// TODO Figure out what I actually removed here
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
wifi_config_t wifi_config = {
.sta =
{
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
.failure_retry_cnt = 20,
// TODO Figure out what I actually removed here
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
// Wait for WiFi to connect
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
esp_sntp_setservername(0, "pool.ntp.org");
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_init();
// Wait for WiFi to connect
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE,
portMAX_DELAY);
esp_sntp_setservername(0, "pool.ntp.org");
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_init();
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
tzset();
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
tzset();
for (uint32_t i = 0; i < 20; ++i) {
if (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
ESP_LOGI(TAG, "... waiting for time update");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
for (uint32_t i = 0; i < 20; ++i) {
if (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
ESP_LOGI(TAG, "... waiting for time update");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
time_t epoch;
time(&epoch);
struct tm *timeinfo = localtime(&epoch);
time_t epoch;
time(&epoch);
struct tm *timeinfo = localtime(&epoch);
ESP_LOGI(TAG, "Current time: %i:%i", timeinfo->tm_hour, timeinfo->tm_min);
ntp_time_obtained = true;
ESP_LOGI(TAG, "Current time: %i:%i", timeinfo->tm_hour, timeinfo->tm_min);
ntp_time_obtained = true;
}

View File

@@ -3,8 +3,8 @@
#include <stdbool.h>
// The idea with this function is to check if NTP time has been obtained
// at least once - and therefore time is at least vaguely accurate
// I want to call this when saving data to know whether the time is valid/trusted
// at all
// I want to call this when saving data to know whether the time is
// valid/trusted at all
bool has_ntp_time_obtained_once();
// Probably should rename this to "start networking" or something because