cob.forth 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. \ program the on-board flash then run some tests
  2. base @ decimal
  3. : get-image-file ( b u addr -- flag )
  4. >r
  5. r/o bin open-file ?dup
  6. if
  7. lcd-." open error = " lcd-dec. lcd-cr
  8. r> 2drop
  9. false exit
  10. then
  11. r> \ file-id buffer
  12. swap >r \ buffer
  13. flash-rom-size
  14. begin
  15. \ b u1
  16. 2dup
  17. r@ read-file ?dup \ b u1 u2 ior ior?
  18. if
  19. lcd-." read error = " lcd-dec. lcd-cr
  20. 2drop drop
  21. r> close-file drop
  22. false exit
  23. then
  24. \ b u1 u2
  25. swap over - \ b u2 (u1-u2)
  26. >r + r> \ (b+u2) (u1-u2)
  27. dup 0=
  28. until
  29. 2drop
  30. r> close-file drop
  31. true
  32. ;
  33. create serial-number-buffer
  34. flash-serial-number-length allot
  35. : program-rom ( b u -- )
  36. flash-select-internal
  37. lcd-." Load File: " 2dup lcd-type lcd-cr
  38. here get-image-file 0=
  39. if
  40. lcd-." Read file failed" lcd-cr
  41. exit
  42. then
  43. lcd-." S/N: "
  44. \ display the serial number
  45. serial-number-buffer flash-serial-number-offset +
  46. flash-serial-number-length
  47. flash-serial-number-offset flash-read
  48. if
  49. flash-serial-number-length 0
  50. ?do
  51. serial-number-buffer flash-serial-number-offset + i + c@
  52. dup bl 127 within
  53. if
  54. lcd-emit
  55. else
  56. drop
  57. then
  58. loop
  59. else
  60. lcd-." FAIL" lcd-cr
  61. exit
  62. then
  63. lcd-cr lcd-." Erase: "
  64. flash-write-enable
  65. if
  66. flash-chip-erase
  67. if
  68. lcd-." Done"
  69. else
  70. lcd-." FAIL"
  71. lcd-cr exit
  72. then
  73. else
  74. lcd-." FAIL"
  75. lcd-cr exit
  76. then
  77. lcd-cr lcd-." Program: "
  78. flash-rom-size 0 ?do
  79. flash-write-enable 0=
  80. if
  81. lcd-." FAIL" lcd-cr
  82. unloop
  83. exit
  84. then
  85. here i + flash-page-size i flash-write 0=
  86. if
  87. lcd-." FAIL" lcd-cr
  88. unloop
  89. exit
  90. then
  91. i flash-sector-size 1- and 0=
  92. if
  93. [char] . lcd-emit
  94. then
  95. flash-page-size +loop
  96. lcd-cr lcd-." Verify: "
  97. flash-rom-size 0 ?do
  98. here i + flash-sector-size i flash-verify
  99. if
  100. [char] . lcd-emit
  101. else
  102. [char] E lcd-emit
  103. then
  104. flash-sector-size +loop
  105. lcd-cr
  106. ;
  107. \ ===========================================
  108. : within-box ( x y x0 y0 x1 y1 -- flag )
  109. swap >r rot >r \ x y y0 y1
  110. within \ x flag
  111. swap r> r> \ flag x x0 x1
  112. within and
  113. ;
  114. variable sector
  115. variable error-count
  116. : scan-sd-sector ( -- )
  117. sector @ 1+ $fffff and dup sector !
  118. 8 lcd-text-rows 3 - lcd-at-xy
  119. dup 8 lcd-u.r
  120. >r here 1024 + 1 r> read-sectors ?dup
  121. if
  122. drop
  123. 8 lcd-text-rows 2 - lcd-at-xy
  124. 1 error-count +! error-count @ 8 lcd-u.r
  125. filesystem-init
  126. then
  127. ;
  128. 50 constant box-width
  129. 50 constant box-height
  130. lcd-width-pixels 2/ 15 - dup
  131. constant touch-x0
  132. box-width +
  133. constant touch-x1
  134. lcd-height-pixels 2/ 15 - dup
  135. constant touch-y0
  136. box-height +
  137. constant touch-y1
  138. 1 constant event-no-touch
  139. 2 constant event-touch
  140. 3 constant event-release
  141. 4 constant event-k1
  142. 5 constant event-k2
  143. 6 constant event-k3
  144. 7 constant event-timeout
  145. variable touched
  146. variable time-limit
  147. : read-event ( -- e )
  148. 0 time-limit !
  149. begin
  150. ctp-pos? if
  151. ctp-pos dup 0<
  152. if
  153. 2drop
  154. touched @
  155. if
  156. false touched !
  157. touch-x0 touch-y0 lcd-move-to
  158. 2 2 lcd-move-rel
  159. box-width 4 - box-height 4 - lcd-white lcd-box
  160. lcd-black
  161. event-no-touch exit
  162. then
  163. else
  164. touch-x0 touch-y0
  165. touch-x1 touch-y1
  166. within-box
  167. touched @ 0= and
  168. if
  169. true touched !
  170. touch-x0 touch-y0 lcd-move-to
  171. 2 2 lcd-move-rel
  172. box-width 4 - box-height 4 - lcd-box
  173. event-touch exit
  174. then
  175. then
  176. then
  177. button? if
  178. button
  179. case
  180. button-none of
  181. event-release exit
  182. endof
  183. button-left of
  184. event-k1 exit
  185. endof
  186. button-centre of
  187. event-k2 exit
  188. endof
  189. button-right of
  190. event-k3 exit
  191. endof
  192. endcase
  193. then
  194. key? if
  195. key-flush
  196. then
  197. scan-sd-sector
  198. 1 time-limit +!
  199. time-limit @ 500 >
  200. if
  201. event-timeout exit
  202. then
  203. again
  204. ;
  205. variable bitset
  206. variable in-ok
  207. : cob-clear-log ( -- )
  208. 12 6 ?do
  209. 0 i lcd-at-xy 11 lcd-spaces
  210. loop
  211. ;
  212. : cob-ok ( -- )
  213. true in-ok !
  214. 21 7 lcd-at-xy lcd-." OO k "
  215. 21 8 lcd-at-xy lcd-." O O k k"
  216. 21 9 lcd-at-xy lcd-." O O kk "
  217. 21 10 lcd-at-xy lcd-." OO k k"
  218. ;
  219. : cob-ok-off ( -- )
  220. in-ok @
  221. if
  222. 11 7 ?do
  223. 21 i lcd-at-xy 8 lcd-spaces
  224. loop
  225. false in-ok !
  226. then
  227. ;
  228. : cob-test ( -- )
  229. false touched !
  230. 0 bitset !
  231. begin
  232. read-event
  233. case
  234. event-timeout of
  235. 0 bitset !
  236. cob-clear-log
  237. cob-ok-off
  238. endof
  239. event-no-touch of
  240. 0 7 lcd-at-xy lcd-." CTP release"
  241. $01 bitset @ or bitset !
  242. endof
  243. event-touch of
  244. 0 6 lcd-at-xy lcd-." CTP touch "
  245. 0 7 lcd-at-xy lcd-." "
  246. $02 bitset @ or bitset !
  247. $01 invert bitset @ and bitset !
  248. cob-ok-off
  249. endof
  250. event-release of
  251. 0 8 lcd-at-xy lcd-." Key release"
  252. $04 bitset @ or bitset !
  253. endof
  254. event-k1 of
  255. 0 8 lcd-at-xy lcd-." "
  256. 0 9 lcd-at-xy lcd-." Key 1 "
  257. $08 bitset @ or bitset !
  258. $04 invert bitset @ and bitset !
  259. cob-ok-off
  260. endof
  261. event-k2 of
  262. 0 8 lcd-at-xy lcd-." "
  263. 0 10 lcd-at-xy lcd-." Key 2 "
  264. $10 bitset @ or bitset !
  265. $04 invert bitset @ and bitset !
  266. cob-ok-off
  267. endof
  268. event-k3 of
  269. 0 8 lcd-at-xy lcd-." "
  270. 0 11 lcd-at-xy lcd-." Key 3 "
  271. $20 bitset @ or bitset !
  272. $04 invert bitset @ and bitset !
  273. cob-ok-off
  274. endof
  275. endcase
  276. bitset @ $3f and $3f =
  277. until
  278. cob-ok
  279. ;
  280. : cob-main ( -- )
  281. button-flush
  282. key-flush
  283. ctp-flush
  284. lcd-cls
  285. lcd-." COB Testing" lcd-cr
  286. \ s" flash.rom" program-rom
  287. 10 lcd-text-rows 1- lcd-at-xy lcd-." Key1"
  288. 17 lcd-text-rows 1- lcd-at-xy lcd-." Key2"
  289. 24 lcd-text-rows 1- lcd-at-xy lcd-." Key3"
  290. 0 lcd-text-rows 3 - lcd-at-xy
  291. lcd-." sector: " lcd-cr
  292. lcd-." errors: "
  293. touch-x0 touch-y0 lcd-move-to
  294. box-width box-height lcd-box
  295. begin
  296. cob-clear-log
  297. cob-test
  298. again
  299. ;
  300. base !