python-socket-examples / button_can.lua

-- Save references to pins and buttons
local BTN_UP = pio.PE_0
local BTN_DOWN = pio.PE_1
local BTN_LEFT = pio.PE_2
local BTN_RIGHT = pio.PE_3
local BTN_SELECT = pio.PF_1
local STA_LED = pio.PF_0
local sta_led_status = 0

pio.pin.setdir(pio.INPUT, BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, BTN_SELECT)
pio.pin.setpull(pio.PULLUP, BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, BTN_SELECT)
pio.pin.setdir(pio.OUTPUT, STA_LED)

local setlow = pio.pin.setlow
local sethigh = pio.pin.sethigh
local getval = pio.pin.getval

function toggle_status_led()
    if sta_led_status == 0 then
        sta_led_status = 1
        sethigh(STA_LED)
    else
        sta_led_status = 0
        setlow(STA_LED)
    end
    tmr.delay(500000)
end

function btn_pressed( button )
  return getval( button ) == 0
end

local canID = 0x42
local canport = 0

can.setup(canport, 105263)
local CANSTD = can.ID_STD

--- Send a message over the Controller Area Network
--- @param data Message data as string (up to 8 bytes) 
function canSend(data)
	print("Sending can message", data)
    can.send(canport, canID, CANSTD, data)
end

running = true
while running do
    if (btn_pressed(BTN_SELECT)) then
    	running = false
    	print("done")
    elseif (btn_pressed(BTN_UP)) then
    	canSend("up")
    elseif (btn_pressed(BTN_DOWN)) then
    	canSend("down")
    elseif (btn_pressed(BTN_LEFT)) then
    	canSend("left")
    elseif (btn_pressed(BTN_RIGHT)) then
    	canSend("right")
    end
    tmr.delay(100000)
end
print("finished")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.