Advertisement
Cremulus

SLOG file transfer example

Jul 13th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. // The FTP transfer utility
  2. int bSetupOk
  3. int u8RxD
  4. int iCRC,iRxState,iRxStuffed,iRxCMD,iRxLength,iRxCnt
  5. str sRxData
  6. int iTxCRC
  7.  
  8. const
  9. {
  10. // Comms stuff
  11. bSTUFF = $A6
  12. bSOM = $A7
  13. cmdGetDirectory = $44
  14. cmdReadFile = $52
  15. cmdWriteFile = $57
  16. cmdResetLogger = $5A
  17. // Oh, for enumerated types...
  18. sRxIdle = 0
  19. sRxExpCMD = 1
  20. sRxExpLEN0 = 2
  21. sRxExpLEN1 = 3
  22. sRxExpLEN2 = 4
  23. sRxExpDATA = 5
  24. sRxExpCRCL = 6
  25. sRxExpCRCH = 7
  26. }
  27.  
  28. // Forwards
  29. int vHandleRxChar(int u8RxD);
  30. int vHandleRxMsg();
  31. int vHandleTxByte(int u8TxD);
  32. int vHandleTxData(int iCMD, str sDATA);
  33.  
  34. // Setup the ESP port and BT port
  35. bSetupOk = devicesetup(devESP,500000,parityNONE,2)
  36. // Assume we're setup
  37. iRxState = sRxExpIdle;
  38. iRxStuffed=0;
  39. // Now, we wait for a command
  40. repeat
  41. while rxwait(devESP)<1
  42. {
  43. // We should sleep here
  44. }
  45. // We have at least one character waiting
  46. vHandleRxChar(rxread(devESP,u8))
  47. until false
  48.  
  49. // Handle a received character
  50. int vHandleRxChar(int u8RxD)
  51. {
  52. if u8RxD = bSOM
  53. {
  54. iRxState = sRxExpCMD
  55. iCRC = $FFFF
  56. }
  57. else if u8RxD = bSTUFF
  58. iRxStuffed = bSTUFF
  59. else
  60. {
  61. // Unstuff data bytes
  62. u8RxD+=iRxStuffed
  63. iRxStuffed=0;
  64.  
  65. switch (iRxState)
  66. {
  67. case sRxExpCMD // Expect CMD
  68. iRxCMD = u8RxD
  69. crc16(iCRC,u8RxD)
  70. iRxState = sRxExpLEN0
  71. break
  72. case sRxExpLEN0 // Expect LEN LSB
  73. iRxLength = u8RxD
  74. crc16(iCRC,u8RxD)
  75. iRxState = sRxExpLEN1
  76. break
  77. case sRxExpLEN1 // Expect LEN MSB
  78. iRxLength += u8RxD * $100
  79. crc16(iCRC,u8RxD)
  80. iRxState = sRxExpLEN2
  81. break
  82. case sRxExpLEN2 // Expect LEN MSB
  83. iRxLength += u8RxD * $10000
  84. crc16(iCRC,u8RxD)
  85. // Do we have any data?
  86. sRxData = ""
  87. iRxCnt = 0
  88. if iRxLength>0
  89. iRxState = sRxExpDATA
  90. else
  91. iRxState = sRxExpCRCL
  92. break
  93. case sRxExpDATA
  94. sRxData = sRxData + chr$(u8RxD)
  95. crc16(iCRC,u8RxD)
  96. if ++iRxCnt >= iRxLength
  97. iRxState = sRxExpCRCL
  98. break;
  99. case sRxExpCRCL
  100. if u8RxD <> (iCRC and $FF)
  101. iRxState = sRxIdle
  102. else
  103. iRxState = sRxExpCRCH
  104. break;
  105. case sRxExpCRCH
  106. if u8RxD = (iCRC >> 8)
  107. vHandleRxMsg()
  108. iRxState = sRxIdle
  109. break;
  110. }
  111. }
  112. }
  113.  
  114. // Handle a received message
  115. int vHandleRxMsg()
  116. {
  117. switch(iRxCMD)
  118. {
  119. case cmdGetDirectory
  120. // get the directory and return it
  121. vHandleTxData(cmdGetDirectory,filedir("/"))
  122. break
  123.  
  124. case cmdWriteFile
  125. println "Write a file",iRxLength
  126. break
  127.  
  128. case cmdReadFile
  129. println "Read a file",iRxLength
  130. break
  131.  
  132. case cmdResetLogger
  133. reset // We just unceremoniously reset.
  134. break
  135.  
  136. default
  137. println "Unknown command..."
  138. }
  139. }
  140.  
  141. // Handle sending a stuffed byte
  142. int vHandleTxByte(int u8TxD)
  143. {
  144. // CRC the damned thing
  145. crc16(iTxCRC,u8TxD)
  146. // Send it
  147. if u8TxD = bSTUFF
  148. {
  149. txwrite(devESP,u8 bSTUFF, 0)
  150. }
  151. else if u8TxD = bSOM
  152. {
  153. txwrite(devESP,u8 bSTUFF, 1)
  154. }
  155. else
  156. txwrite(devESP,u8 u8TxD)
  157. }
  158.  
  159. // Send a reply
  160. int vHandleTxData(int iCMD, str sDATA)
  161. {
  162. int i,iLength,iTxCRCCopy
  163.  
  164. txwrite(devESP,u8 bSOM)
  165. iTxCRC = $FFFF
  166. vHandleTxByte(iCMD)
  167. iLength = length(sDATA)
  168. vHandleTxByte(iLength and $FF)
  169. vHandleTxByte((iLength >> 8) and $FF)
  170. vHandleTxByte((iLength >> 16) and $FF)
  171. i=0
  172. while i<iLength
  173. {
  174. while txfree(devESP) < 2
  175. {
  176. // Should sleep here
  177. }
  178. // Send the next character (byte)
  179. vHandleTxByte(asc(mid$(sDATA,i++,1)))
  180. }
  181. // Now, send the CRC (without changing it)
  182. iTxCRCCopy=iTxCRC
  183. vHandleTxByte(iTxCRCCopy)
  184. vHandleTxByte(iTxCRCCopy >> 8)
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement