luacode.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2015-2016 Peter Magnusson <peter@birchroad.net>
  4. # these functions are needed on the device, otherwise they will be
  5. # uploaded during prepare
  6. LUA_FUNCTIONS = ['recv_block', 'recv_name','recv','shafile']
  7. DOWNLOAD_FILE = "file.open('{filename}') print(file.seek('end', 0)) file.seek('set', {bytes_read}) uart.write(0, file.read({chunk_size}))file.close()"
  8. PRINT_FILE = "file.open('{filename}') print('---{filename}---') print(file.read()) file.close() print('---')"
  9. LIST_FILES = 'for key,value in pairs(file.list()) do print(key,value) end'
  10. SAVE_LUA = \
  11. r"""
  12. function recv_block(d)
  13. if string.byte(d, 1) == 1 then
  14. size = string.byte(d, 2)
  15. uart.write(0,'\006')
  16. if size > 0 then
  17. file.write(string.sub(d, 3, 3+size-1))
  18. else
  19. file.close()
  20. uart.on('data')
  21. uart.setup(0,{baud},8,0,1,1)
  22. end
  23. else
  24. uart.write(0, '\021' .. d)
  25. uart.setup(0,{baud},8,0,1,1)
  26. uart.on('data')
  27. end
  28. end
  29. function recv_name(d) d = string.gsub(d, '\000', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
  30. function recv() uart.setup(0,{baud},8,0,1,0) uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
  31. function shafile(f) file.open(f, "r") print(crypto.toHex(crypto.hash("sha1",file.read()))) file.close() end
  32. """
  33. UART_SETUP = 'uart.setup(0,{baud},8,0,1,1)'