ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

99 lines
2.2KB

  1. static const char *TAG = "gamma";
  2. #include <esp_log.h>
  3. #include <cJSON.h>
  4. #include "gamma.hpp"
  5. #include "utils.hpp"
  6. namespace Gamma {
  7. static uint8_t gamma[256];
  8. static uint8_t inv_gamma[256];
  9. static void default_table(uint8_t *table) {
  10. for (int val = 0; val < 256; ++val) *table++ = val;
  11. }
  12. static void json_table(cJSON *jarray, uint8_t *table) {
  13. if (jarray->type != cJSON_Array) {
  14. ESP_LOGE(TAG, "Not an array: %s", jarray->string);
  15. }
  16. int count = cJSON_GetArraySize(jarray);
  17. if (count != 256) {
  18. ESP_LOGE(TAG, "Wrong size for %s: %d", jarray->string, count);
  19. }
  20. uint8_t *ptr = table;
  21. cJSON *jnumber;
  22. cJSON_ArrayForEach(jnumber, jarray) {
  23. if (jnumber->type != cJSON_Number) {
  24. ESP_LOGE(TAG, "Not a number: %s", jnumber->string);
  25. default_table(table);
  26. return;
  27. }
  28. *ptr++ = (uint8_t)jnumber->valueint;
  29. }
  30. }
  31. static inline cJSON* load_json(const char *path) {
  32. return cJSON_Parse(read_file(path).c_str());
  33. }
  34. void reload() {
  35. default_table(gamma);
  36. default_table(inv_gamma);
  37. cJSON *json = load_json("/spiffs/gamma.json");
  38. if (!json) {
  39. ESP_LOGE(TAG, "No gamma values!");
  40. return;
  41. }
  42. cJSON *jgamma = cJSON_GetObjectItem(json, "gamma");
  43. if (!jgamma) {
  44. ESP_LOGE(TAG, "No gamma values!");
  45. cJSON_Delete(json);
  46. return;
  47. }
  48. cJSON *jinv_gamma = cJSON_GetObjectItem(json, "inv_gamma");
  49. if (!jinv_gamma) {
  50. ESP_LOGE(TAG, "No inverse gamma values!");
  51. cJSON_Delete(json);
  52. return;
  53. }
  54. json_table(jgamma, gamma);
  55. json_table(jinv_gamma, inv_gamma);
  56. cJSON_Delete(json);
  57. }
  58. /*
  59. static inline uint8_t scale(int val, int num, int dem) {
  60. if (dem == 0) return 0;
  61. int val = (val * num) / dem;
  62. return (val >= 256 ? 255 : val);
  63. }
  64. */
  65. static inline Color correct_single(Color c, int b) {
  66. return {gamma[(b * c.r) / 255], gamma[(b * c.g) / 255], gamma[(b * c.b) / 255]};
  67. }
  68. void correct(Color *dst, const Color *src, int n, int b) {
  69. while(n-- > 0) *dst++ = correct_single(*src++, b);
  70. }
  71. Color wrong_single(Color c) {
  72. return {inv_gamma[c.r], inv_gamma[c.g], inv_gamma[c.b]};
  73. }
  74. } // Gamma