Advertisement
doublemintben

my atmega.lua

Mar 10th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. --This module contains code which is used to communicate with Arduino.
  2.  
  3. -- Length of packets sent by Arduino on state requests
  4. local OUTPUTS_COUNT = 8
  5. local INPUTS_COUNT = 8
  6. local ANALOGS_COUNT = 8
  7. local RELAYS_COUNT = 6
  8. local TIME_LENGTH = 2+2
  9. local DATE_LENGTH = 4+2+2
  10. local STATE_PACKET_SIZE =
  11.     RELAYS_COUNT +
  12.     INPUTS_COUNT +
  13.     OUTPUTS_COUNT +
  14.     ANALOGS_COUNT*4 +
  15.     TIME_LENGTH +
  16.     DATE_LENGTH +
  17.     1 -- 'e'
  18. local message = "test 2"
  19.  
  20.  
  21. -- Utility functions.
  22.  
  23. local function setData(data)
  24.     message = data:gsub("%ยง","")
  25. end
  26.  
  27. ---------------------------------------------------------------------------------------------------------------------
  28.  
  29.  
  30. local atmega = {}
  31.  
  32. -- Send command, e.g. R1 or O2
  33. function atmega.sendCommand(cmd)
  34.     uart.write(0, "C" .. cmd)
  35. end
  36.  
  37. function atmega.getMessage()
  38.     return message
  39. end
  40.  
  41.  
  42. ---------------------------------------------------------------------------------------------------------------------
  43.  
  44.  
  45. -- Arduino sends only state packets via Serial.
  46. --uart.on("data", "e", parseState, 0)
  47. uart.on("data", "ยง", setData, 0)
  48.  
  49. -- Start polling
  50. tmr.alarm(3, 1000, 1, function()
  51.     atmega.sendCommand("ss")
  52. end)
  53.  
  54.  
  55. ---------------------------------------------------------------------------------------------------------------------
  56.  
  57. return atmega
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement