Norns: missing battery globals cause errors

Created on 6 Dec 2018  路  10Comments  路  Source: monome/norns

Issue only on RasPi

if not initialized, norns.battery_percent and norns.battery_current will throw errors on the main menu and cause the HOME menu to become unaccessible if the stats are accessed.

Quick fix for this would be to set those to zero in battery.c

Adding this code after fprintf(stderr, "BATTERY: FAIL.\n"); solves the issue

        union event_data *ev = event_data_new(EVENT_BATTERY);
        ev->battery.percent = 0;
        ev->battery.current = 0;
        event_post(ev);

Alternately you can comment those variables out in menu.lua but it throws off the formatting on the stats screen a bit.

Would this be a reasonable PR for the main code branch, or something I should just keep for my own install?

raspi

All 10 comments

@okyeron that also has to cover the case when battery_init fails to create a thread. have you considered initializing these variables in norns.lua instead?

agreed. initializing them in lua would be a more appropriate fix.

Understood.

So in battery percent handler in norns.lua I could check to those variables for nil and set to zero - which might look like this:

--- battery percent handler
-- @param percent battery full percentage
norns.battery = function(percent, current)
  norns.battery_percent = tonumber(percent)
  norns.battery_current = tonumber(current)
  if (norns.battery_percent == nil) then norns.battery_percent = 0 end
  if (norns.battery_current == nil) then norns.battery_current = 0 end
  --print("battery: "..norns.battery_percent.."% "..norns.battery_current.."mA")
end

It's possible for tonumber() to return nil, so checking the value after seems appropriate?

This will only set the variables to 0 on first event received. Just set them to 0 outside of the function.

It makes sense to use nil percentage value though as an error indicator for RPi and desktops and do the check in menu to hide battery indicator in case of no data.

Perhaps setting norns.battery_percent = "n/a" or norns.battery_percent = "-" (or similar)? This seems a bit less prone to errors vs. passing a nil value through to the menu

Passing these values to menu doesn't signal about any errors. I like nil because it breaks an unhandled special case that should break (and should be handled as special case). And it only requires simple value check in display code (where stuff like error indication belongs).

okie dokie... then that circles me back around to menu

    if (norns.battery_percent ~= nil) then 
      screen.text("BAT " .. norns.battery_percent)
    end 
    screen.move(36,10)
    if (norns.battery_current ~= nil) then 
      screen.text(norns.battery_current .. "mA")
    end

Yeah, or

if norns.battery_percent and norns.battery_current then
    screen.move(0,10)
    screen.text("BAT " .. norns.battery_percent)
    screen.move(36,10)
    screen.text(norns.battery_current .. "mA")
end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

catfact picture catfact  路  9Comments

pq picture pq  路  3Comments

pq picture pq  路  6Comments

pq picture pq  路  8Comments

catfact picture catfact  路  3Comments