As the title says... Chat on...

User avatar
By cwr
#55497 I thought that something like the following code would do what
I wanted, but although it runs in Lua, tail calls apparently
block the ESP8266 firmware. Possibly something might be done
by setting up a table of co-routines, and calling each of them
in turn. I'll keep tinkering.
Code: Select all#!/usr/bin/lua

idx = nil

function state_a()
    print("State A")
    return state_b()
end

function counter()
    local n = 0
    return function()
        n = (n % 16384) + 1
        return n
    end
end

function state_b()
    print("State B")
    if idx == nil
    then
        idx = counter()
    end
    n = idx()
    print(n)
    if n < 64
    then
        return state_b()
    else
        idx = nil
        return state_c()
    end
end

function state_c()
    print("State C")
    return state_a()
end

state_a()


Will
User avatar
By TerryE
#55546 Read the link below :)
User avatar
By ESP-12f
#55577
cwr wrote:I thought that something like the following code would do what
I wanted, but although it runs in Lua, tail calls apparently
block the ESP8266 firmware. Possibly something might be done
by setting up a table of co-routines, and calling each of them
in turn. I'll keep tinkering.
Code: Select all#!/usr/bin/lua

idx = nil

function state_a()
    print("State A")
    return state_b()
end

function counter()
    local n = 0
    return function()
        n = (n % 16384) + 1
        return n
    end
end

function state_b()
    print("State B")
    if idx == nil
    then
        idx = counter()
    end
    n = idx()
    print(n)
    if n < 64
    then
        return state_b()
    else
        idx = nil
        return state_c()
    end
end

function state_c()
    print("State C")
    return state_a()
end

state_a()


Will

Can not try this atm, but will this work?
User avatar
By TerryE
#55690 Siggghhh, tail calls don't block the firmware. Running Lua blocks the firmware. The non-OS SDK is non-preemptive. This means that the SDK housekeeping functions don't get a look in until you return control. I explain all this in the FAQ.