Browse Source

lua functions in for #19

kmpm 8 years ago
parent
commit
2aaab27db7
2 changed files with 13 additions and 7 deletions
  1. 8 2
      lib/luacode.py
  2. 5 5
      lib/uploader.py

+ 8 - 2
lib/luacode.py

@@ -5,7 +5,7 @@
 
 # these functions are needed on the device, otherwise they will be
 # uploaded during prepare
-LUA_FUNCTIONS = ['recv_block', 'recv_name','recv','shafile']
+LUA_FUNCTIONS = ['recv_block', 'recv_name','recv','shafile', 'send_block', 'send_file']
 
 DOWNLOAD_FILE = "file.open('{filename}') print(file.seek('end', 0)) file.seek('set', {bytes_read}) uart.write(0, file.read({chunk_size}))file.close()"
 
@@ -13,7 +13,7 @@ PRINT_FILE = "file.open('{filename}') print('---{filename}---') print(file.read(
 
 LIST_FILES = 'for key,value in pairs(file.list()) do print(key,value) end'
 
-SAVE_LUA = \
+RECV_LUA = \
 r"""
 function recv_block(d)
   if string.byte(d, 1) == 1 then
@@ -37,4 +37,10 @@ function recv() uart.setup(0,{baud},8,0,1,0) uart.on('data', '\000', recv_name,
 function shafile(f) file.open(f, "r") print(crypto.toHex(crypto.hash("sha1",file.read()))) file.close() end
 """
 
+SEND_LUA = \
+r"""
+function send_block(d) l = string.len(d) uart.write(0, '\001' + string.char(l) + string.rep(' ', 128 - l)) return l end
+function send_file(f) file.open(f) s=file.seek('end', 0) p=0 while (p<s) do file.seek('set',p) p=p+send_block(file.read(128)) end send_block('') file.close() end
+"""
+
 UART_SETUP = 'uart.setup(0,{baud},8,0,1,1)'

+ 5 - 5
lib/uploader.py

@@ -9,7 +9,7 @@ import os
 import serial
 
 from .utils import default_port, system
-from .luacode import DOWNLOAD_FILE, SAVE_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP, PRINT_FILE
+from .luacode import DOWNLOAD_FILE, RECV_LUA, SEND_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP, PRINT_FILE
 
 log = logging.getLogger(__name__)
 
@@ -145,8 +145,8 @@ class Uploader(object):
         else:
             log.info('Preparation already done. Not adding functions again.')
             return True
-
-        data = SAVE_LUA.format(baud=self._port.baudrate)
+        functions = RECV_LUA + '\n' + SEND_LUA
+        data = functions.format(baud=self._port.baudrate)
         ##change any \r\n to just \n and split on that
         lines = data.replace('\r', '').split('\n')
 
@@ -159,8 +159,8 @@ class Uploader(object):
 
             d = self.exchange(line)
             #do some basic test of the result
-            if ('unexpected' in d) or ('stdin' in d) or len(d) > len(SAVE_LUA)+10:
-                log.error('error in save_lua "%s"', d)
+            if ('unexpected' in d) or ('stdin' in d) or len(d) > len(functions)+10:
+                log.error('error when preparing "%s"', d)
                 return False
         return True