--- a/main/config.c Mon Jun 01 20:27:00 2020 +0200 +++ b/main/config.c Sat Jun 06 13:28:46 2020 +0200 @@ -366,6 +366,7 @@ runtime.Logfile[0] = '\0'; runtime.PumpCooling = false; runtime.TimeBrewing = 0; + runtime.MashStep = 0; write_runtime(); } else { dst = (uint8_t*)&runtime; @@ -373,6 +374,8 @@ fclose(f); if (bytes != sizeof(runtime)) { ESP_LOGE(TAG, "/spiffs/etc/runtime.conf read %d/%d bytes", bytes, sizeof(runtime)); + runtime.MashStep = 0; + runtime.MaltAdded = false; } #if 0 printf("Auto started %s\n", runtime.AutoModeStarted ? "yes":"no"); @@ -389,7 +392,8 @@ -void append_recipe() { +void append_recipe() +{ uint8_t *dst = (uint8_t *)&recipe; FILE *f = fopen("/spiffs/etc/recipe.conf", "a"); @@ -398,7 +402,7 @@ return; } - size_t bytes = fwrite(dst, 1, sizeof(recipe), f); + size_t bytes = fwrite(dst, 1, recipe_hdr.recsize, f); fclose(f); ESP_LOGI(TAG, "/spiffs/etc/recipe.conf appended %d bytes", bytes); } @@ -415,11 +419,11 @@ return; } - fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); - size_t bytes = fwrite(dst, 1, sizeof(recipe), f); + fseek(f, (RecNo - 1) * recipe_hdr.recsize + recipe_hdr.hdrsize, SEEK_SET); + size_t bytes = fwrite(dst, 1, recipe_hdr.recsize, f); fclose(f); - if (bytes != sizeof(recipe)) { - ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write record %d, %d/%d bytes", RecNo, bytes, sizeof(recipe)); + if (bytes != recipe_hdr.recsize) { + ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write record %d, %d/%d bytes", RecNo, bytes, recipe_hdr.recsize); } } @@ -428,31 +432,44 @@ void read_recipe(int RecNo) { uint8_t *dst; + size_t bytes; FILE *f = fopen("/spiffs/etc/recipe.conf", "r"); if (f == NULL) { // No recipe yet, create it. + dst = (uint8_t*)&recipe_hdr; + memset(dst, 0, sizeof(recipe_hdr)); + recipe_hdr.version = RECIPE_VERSION; + recipe_hdr.hdrsize = sizeof(recipe_hdr); + recipe_hdr.recsize = sizeof(recipe); + recipe_hdr.mashmax = MASH_MAX; + recipe_hdr.additionmax = ADDITION_MAX; + f = fopen("/spiffs/etc/recipe.conf", "w"); + bytes = fwrite(dst, 1, sizeof(recipe_hdr), f); + if (bytes != sizeof(recipe_hdr)) { + ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write header, %d/%d bytes", bytes, sizeof(recipe_hdr)); + } dst = (uint8_t*)&recipe; memset(dst, 0, sizeof(recipe)); - recipe.Version = 1; sprintf(recipe.Name, "Recipe 1"); sprintf(recipe.Code, "001"); sprintf(recipe.MashStep[0].Name, "Mash-in"); recipe.MashStep[0].Type = MASHTYPE_INFUSION; - recipe.MashStep[0].Temperature = recipe.MashStep[0].Infusion_temp = 67.5; - recipe.MashStep[0].Infusion_amount = 15.0; - recipe.MashStep[0].Resttime = 1; - recipe.MashStep[0].Ramptime = 1; - for (int i = 1; i < 8; i++) + recipe.MashStep[0].Step_temp = recipe.MashStep[0].End_temp = recipe.MashStep[0].Infuse_temp = 67.5; + recipe.MashStep[0].Infuse_amount = 15.0; + recipe.MashStep[0].Step_time = 1; + recipe.MashStep[0].Ramp_time = 1; + for (int i = 1; i < MASH_MAX; i++) recipe.MashStep[i].Type = MASHTYPE_TEMPERATURE; sprintf(recipe.MashStep[1].Name, "Mash"); - recipe.MashStep[1].Temperature = 67.0; - recipe.MashStep[1].Resttime = 75; - recipe.MashStep[1].Ramptime = 1; - sprintf(recipe.MashStep[7].Name, "Mash-out"); - recipe.MashStep[7].Temperature = 78.0; - recipe.MashStep[7].Resttime = 5; - recipe.MashStep[7].Ramptime = 11; + recipe.MashStep[1].Step_temp = recipe.MashStep[1].End_temp = 67.0; + recipe.MashStep[1].Step_time = 75; + recipe.MashStep[1].Ramp_time = 1; + sprintf(recipe.MashStep[2].Name, "Mash-out"); + recipe.MashStep[2].Step_temp = recipe.MashStep[2].End_temp = 78.0; + recipe.MashStep[2].Step_time = 5; + recipe.MashStep[2].Ramp_time = 11; + recipe.Mashsteps = 3; recipe.BoilTime = 60; recipe.Additions = 2; sprintf(recipe.Addition[0].Name, "Hop"); @@ -467,11 +484,48 @@ recipe.Whirlpool6 = 0; recipe.Whirlpool2 = 0; recipe.SpargeTemp = 85.0; - append_recipe(); + bytes = fwrite(dst, 1, sizeof(recipe), f); + fclose(f); } else { + /* + * Try to read the new file header + */ + dst = (uint8_t*)&recipe_hdr; + fseek(f, 0, SEEK_SET); + bytes = fread(dst, 1, sizeof(recipe_hdr), f); + if (bytes != sizeof(recipe_hdr)) { + ESP_LOGE(TAG, "/spiffs/etc/recipe.conf read header, %d/%d bytes", bytes, sizeof(recipe_hdr)); + fclose(f); + return; + } +/* + if (recipe_hdr.version < RECIPE_VERSION) { + FILE *nf = fopen("/spiffs/etc/recipe.new", "w"); + + ESP_LOGI(TAG, "/spiffs/etc/recipe.conf version %d, new %d", recipe_hdr.version, RECIPE_VERSION); + + fseek(f, recipe_hdr.hdrsize, SEEK_SET); + dst = (uint8_t*)&recipe; + while ((bytes = fread(dst, 1, recipe_hdr.recsize, f))) { + + // Upgrade data here + bytes = fwrite(dst, 1, sizeof(recipe), nf); + if (bytes != sizeof(recipe)) { + ESP_LOGE(TAG, "/spiffs/etc/recipe.new write data, %d/%d bytes", bytes, sizeof(recipe)); + } + } + // Update the header with new sizes + fclose(nf); + fclose(f); + rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old"); + rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf"); + unlink("/spiffs/etc/recipe.old"); + f = fopen("/spiffs/etc/recipe.conf", "r"); + } +*/ dst = (uint8_t*)&recipe; - fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); - size_t bytes = fread(dst, 1, sizeof(recipe), f); + fseek(f, (RecNo - 1) * recipe_hdr.recsize + recipe_hdr.hdrsize, SEEK_SET); + bytes = fread(dst, 1, sizeof(recipe), f); fclose(f); if (bytes != sizeof(recipe)) { ESP_LOGE(TAG, "/spiffs/etc/recipe.conf read record %d, %d/%d bytes", RecNo, bytes, sizeof(recipe)); @@ -500,9 +554,13 @@ return; } + dst = (uint8_t*)&recipe_hdr; + fread(dst, 1, recipe_hdr.hdrsize, o); + fwrite(dst, 1, recipe_hdr.hdrsize, n); + dst = (uint8_t*)&recipe; while (true) { - bytes = fread(dst, 1, sizeof(recipe), o); + bytes = fread(dst, 1, recipe_hdr.recsize, o); if (bytes == 0) break; @@ -513,7 +571,7 @@ config.RecipeRec = RecWrite; write_config(); } - fwrite(dst, 1, sizeof(recipe), n); + fwrite(dst, 1, recipe_hdr.recsize, n); RecWrite++; } RecRead++;