esp32 firmware for a toaster reflow oven WIP!!!!!

utils.h 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Utilities and helpers
  3. */
  4. #ifndef CSPEMU_UTILS_H
  5. #define CSPEMU_UTILS_H
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include <string.h>
  9. #include <nvs.h>
  10. #include <common_utils/utils.h>
  11. /** Error check macro for FreeRTOS status codes */
  12. #define RTOS_ERROR_CHECK(code) if (pdPASS != (code)) ESP_ERROR_CHECK(ESP_FAIL);
  13. /** Error check macro for NULL return value */
  14. #define NULL_CHECK(code) if (NULL == (code)) ESP_ERROR_CHECK(ESP_FAIL);
  15. /** Error check macro for CSP status codes */
  16. #define CSP_ERROR_CHECK(code) if (CSP_ERR_NONE != (code)) ESP_ERROR_CHECK(ESP_FAIL);
  17. /**
  18. * Reconnect to WiFi if there are saved credentials.
  19. * Called from the main event group handler.
  20. */
  21. void try_reconn_if_have_wifi_creds(void);
  22. /**
  23. * Helper to retrieve a bool value from the NVS. Internally uses 'u8'
  24. *
  25. * @param handle - NVS storage
  26. * @param key
  27. * @param out_value
  28. * @return success
  29. */
  30. esp_err_t nvs_get_bool(nvs_handle handle, const char* key, bool* out_value);
  31. /**
  32. * Helper to store a bool into the NVS. Internally uses 'u8'
  33. *
  34. * @param handle - NVS storage
  35. * @param key
  36. * @param value
  37. * @return success
  38. */
  39. esp_err_t nvs_set_bool(nvs_handle handle, const char* key, bool value);
  40. /**
  41. * Feed watchdogs (inside a busy wait loop)
  42. */
  43. void feed_all_dogs(void);
  44. /**
  45. * @brief malloc() and snprintf() combined
  46. * @attention DO NOT use in if/for/do etc without braces.
  47. *
  48. * The caller is responsible for disposing of the allocated string afterwards.
  49. */
  50. #define malloc_sprintf(var, n, format, ...) \
  51. char *var = malloc(n); \
  52. if (!var) assert(0); \
  53. snprintf(var, n, format, ##__VA_ARGS__);
  54. /**
  55. * Generate externs for an embedded file.
  56. * Variables {varname} and {varname}_end will be produced.
  57. */
  58. #define efile(varname, filename) \
  59. extern const char varname[] asm("_binary_"filename"_start"); \
  60. extern const char varname##_end[] asm("_binary_"filename"_end");
  61. /** Get embedded file size (must be declared with efile() first) */
  62. #define efsize(varname) (varname##_end - varname)
  63. union uf32 {
  64. uint32_t u;
  65. float f;
  66. };
  67. #endif //CSPEMU_UTILS_H