Browse Source

No. 6: Remove delay use in main loop (#8)

Also add `scripts/new-release.pl`
tags/v0.2.1.3
Ryan Joseph 1 year ago
committed by GitHub
parent
commit
f442c6e853
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 24 deletions
  1. +35
    -0
      scripts/new-release.pl
  2. +41
    -22
      zero_watch.ino
  3. +1
    -1
      zw_common.h
  4. +10
    -1
      zw_ota.cpp
  5. +1
    -0
      zw_ota.h

+ 35
- 0
scripts/new-release.pl View File

@@ -0,0 +1,35 @@
#!/usr/bin/perl

my $buildDir = shift || die "build dir\n\n";
my $destDir = shift || die "dest dir\n\n";
my $version = shift || die "version\n\n";

$version =~ s/v//ig;
$buildDir .= "/zero_watch.ino.bin";

die "$buildDir does not exist\n\n", unless (-e $buildDir && !(-d $buildDir));
die "$destDir does not exist\n\n", unless (-e $destDir && -d $destDir);

die "Bad binary version\n\n", unless (`strings $buildDir` =~ /v([0-9\.]+)/);
my $binVer = $1;
die "Versions don't match ($binVer vs $version)\n\n", unless ($version eq $binVer);

my $tFile = "zero_watch-v$version.ino.bin";
my $target = "$destDir/$tFile";

my $md5 = (split(/\s/, `md5sum $buildDir`))[0];
`md5sum $buildDir > $target.md5sum.txt`;

my $bSize = (stat($buildDir))[7];

`cp $buildDir $target`;
die "Copy failed ($?)\n\n", if ($?);

print <<__EOF__;
{
"url": "zero_watch_updates/$tFile",
"md5": "$md5",
"size": $bSize,
"otp": 0
}
__EOF__

+ 41
- 22
zero_watch.ino View File

@@ -30,14 +30,18 @@ ZWAppConfig gConfig = {
.debug = DEBUG,
.publishLogs = false,
.pauseRefresh = false};
ZWRedis *gRedis;
DisplaySpec *gDisplays;
ZWRedis *gRedis = NULL;
DisplaySpec *gDisplays = NULL;
void (*gPublishLogsEmit)(const char *fmt, ...);
unsigned long __lc = 0;
unsigned long long gSecondsSinceBoot = 0;
unsigned long long gLastRefreshTick = 0;
int _last_free = 0;
unsigned long gUDRA = 0;
unsigned long immediateLatency = 0;
StaticJsonBuffer<1024> jsonBuf;
hw_timer_t *__isrTimer = NULL;
portMUX_TYPE __isrMutex = portMUX_INITIALIZER_UNLOCKED;
volatile unsigned long __isrCount = 0;

bool wifi_init()
{
@@ -174,6 +178,12 @@ bool processControlPoint(String &imEmit, ZWRedisResponder &responder)
return true;
}

void preUpdateIRQDisableFunc()
{
zlog("preUpdateIRQDisableFunc disabling __isrTimer\n");
timerEnd(__isrTimer);
}

bool processUpdate(String &updateJson, ZWRedisResponder &responder)
{
JsonObject &updateObj = jsonBuf.parseObject(updateJson.c_str());
@@ -206,7 +216,7 @@ bool processUpdate(String &updateJson, ZWRedisResponder &responder)
zlog("Starting OTA update of %0.2fKB\n", (szb / 1024.0));
zlog("Image source (md5=%s):\n\t%s\n", md5, fqUrl);

if (runUpdate(fqUrl, md5, szb,
if (runUpdate(fqUrl, md5, szb, preUpdateIRQDisableFunc,
[]() { return gRedis->postCompletedUpdate(); }))
{
zlog("OTA update wrote successfully! Restarting in %d seconds...\n",
@@ -309,7 +319,7 @@ void heartbeat()

if ((__hb_count++ % HB_CHECKIN))
{
gRedis->checkin(__lc, WiFi.localIP().toString().c_str(),
gRedis->checkin(gSecondsSinceBoot, WiFi.localIP().toString().c_str(),
immediateLatency, gUDRA, gConfig.refresh * 5);
}
}
@@ -321,7 +331,7 @@ void tick(bool forceUpdate = false)
if (!forceUpdate)
return;

zlog("Awake at us=%lu tick=%ld\n", micros(), __lc);
zlog("Awake at us=%lu tick=%lld\n", micros(), gSecondsSinceBoot);

for (DisplaySpec *w = gDisplays; w->clockPin != -1 && w->dioPin != -1; w++)
updateDisplay(w);
@@ -329,23 +339,30 @@ void tick(bool forceUpdate = false)
_last_free = ESP.getFreeHeap();
}

void __isr()
{
portENTER_CRITICAL(&__isrMutex);
++__isrCount;
portEXIT_CRITICAL(&__isrMutex);
}

void loop()
{
if (!(__lc++ % gConfig.refresh))
if (__isrCount) {
portENTER_CRITICAL(&__isrMutex);
--__isrCount;
portEXIT_CRITICAL(&__isrMutex);
++gSecondsSinceBoot;
dprint("%c%s", !(gSecondsSinceBoot % 5) ? '|' : '.', gSecondsSinceBoot % gConfig.refresh ? "" : "\n");
}
if (!(gSecondsSinceBoot % gConfig.refresh) && gLastRefreshTick != gSecondsSinceBoot)
{
gLastRefreshTick = gSecondsSinceBoot;
readConfigAndUserKeys();
heartbeat();
tick();
}

if (!(__lc % 5))
{
blink(15);
delay(5);
blink(15);
}
dprint("%c%s", !(__lc % 5) ? '|' : '.', __lc % gConfig.refresh ? "" : "\n");
delay(900);
}

void setup()
@@ -376,14 +393,9 @@ void setup()
if (gRedis->connect())
{
zlog("Redis connection established, reading config...\n");
readConfigAndUserKeys();

if (gConfig.pauseRefresh)
{
zlog("Running tick 0 forcefully because refresh is paused at init\n");
tick(true);
}

zlog("Fully initialized! (debug %sabled)\n", gConfig.debug ? "en" : "dis");

if (gConfig.debug)
@@ -391,8 +403,15 @@ void setup()

gPublishLogsEmit = redis_publish_logs_emit;

__isrTimer = timerBegin(0, 80, true);
timerAttachInterrupt(__isrTimer, &__isr, true);
timerAlarmWrite(__isrTimer, 1000000, true);
timerAlarmEnable(__isrTimer);

zlog("Boot count: %d\n", gRedis->incrementBootcount());
zlog("%s v" ZEROWATCH_VER " up & running\n", gHostname.c_str());

tick(true);
}
else
{


+ 1
- 1
zw_common.h View File

@@ -1,7 +1,7 @@
#ifndef __ZW_COMMON__H__
#define __ZW_COMMON__H__

#define ZEROWATCH_VER "0.2.0.7"
#define ZEROWATCH_VER "0.2.1.3"
#define DEBUG 1

struct ZWAppConfig {


+ 10
- 1
zw_ota.cpp View File

@@ -16,7 +16,12 @@ void updateProg(size_t s1, size_t s2)
}
}

bool runUpdate(const char *url, const char *md5, size_t sizeInBytes, bool (*completedCallback)())
bool runUpdate(
const char *url,
const char *md5,
size_t sizeInBytes,
void (*preUpdateIRQDisable)(),
bool (*completedCallback)())
{
HTTPClient http;
if (http.begin(url))
@@ -36,6 +41,10 @@ bool runUpdate(const char *url, const char *md5, size_t sizeInBytes, bool (*comp
{
Update.onProgress(updateProg);
Update.setMD5(md5);

if (preUpdateIRQDisable)
preUpdateIRQDisable();

dprint("OTA start szb=%d\n", sizeInBytes);
auto updateTook = Update.writeStream(dataStream);
if (updateTook == sizeInBytes && !Update.hasError())


+ 1
- 0
zw_ota.h View File

@@ -10,6 +10,7 @@ bool runUpdate(
const char *url,
const char *md5,
size_t sizeInBytes,
void (*preUpdateIRQDisable)(),
bool (*completedCallback)());

#endif

Loading…
Cancel
Save