NTest_lcd_i2c4bit.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- Run LiquidCrystal through some basic tests. Requires `liquidcrystal.lua`
  2. -- and `l2-i2c4bit.lua` available available to `require`.
  3. --
  4. -- This file ought to be named "NTest_liquidcrystal_i2c4bit" or something,
  5. -- but it has its current name due to our default SPIFFS filename length limit.
  6. local N = ...
  7. N = (N or require "NTest")("liquidcrystal-i2c4bit")
  8. local metalcd
  9. local metaback
  10. local backend
  11. local lcd
  12. collectgarbage()
  13. print("HEAP init", node.heap())
  14. metalcd = require "liquidcrystal"
  15. collectgarbage() print("HEAP constructor imported ", node.heap())
  16. metaback = require "lc-i2c4bit"
  17. collectgarbage() print("HEAP backend imported ", node.heap())
  18. backend = metaback({
  19. address = 0x27,
  20. id = 0,
  21. speed = i2c.SLOW,
  22. sda = 2,
  23. scl = 1,
  24. })
  25. collectgarbage() print("HEAP backend built", node.heap())
  26. lcd = metalcd(backend, false, true, 20)
  27. collectgarbage() print("HEAP lcd built", node.heap())
  28. print("waiting for LCD to be unbusy before testing...")
  29. while lcd:busy() do end
  30. N.test("custom character", function()
  31. local glyph = { 0x1F, 0x15, 0x1B, 0x15, 0x1F, 0x10, 0x10, 0x0 }
  32. lcd:customChar(0, glyph)
  33. ok(eq(glyph,lcd:readCustom(0)), "read back")
  34. end)
  35. N.test("draw and readback", function()
  36. lcd:cursorMove(0)
  37. lcd:write("abc")
  38. lcd:cursorMove(10,1)
  39. lcd:write("de")
  40. lcd:cursorMove(10,2)
  41. lcd:write("fg")
  42. lcd:cursorMove(12,3)
  43. lcd:write("hi\000")
  44. lcd:cursorMove(18,4)
  45. lcd:write("jk")
  46. lcd:home() ok(eq(0x61, lcd:read()), "read back 'a'")
  47. ok(eq(0x62, lcd:read()), "read back 'b'")
  48. lcd:cursorMove(11,1) ok(eq(0x65, lcd:read()), "read back 'e'")
  49. lcd:cursorMove(11,2) ok(eq(0x67, lcd:read()), "read back 'g'")
  50. lcd:cursorMove(13,3) ok(eq(0x69, lcd:read()), "read back 'i'")
  51. lcd:cursorMove(14,3) ok(eq(0x00, lcd:read()), "read back 0" )
  52. lcd:cursorMove(19,4) ok(eq(0x6B, lcd:read()), "read back 'k'")
  53. end)
  54. N.test("update home", function()
  55. lcd:home() lcd:write("l")
  56. lcd:home() ok(eq(0x6C, lcd:read()))
  57. end)
  58. N.testasync("clear", function(next)
  59. -- clear and poll busy
  60. lcd:clear()
  61. tmr.create():alarm(5, tmr.ALARM_SEMI, function(tp)
  62. if lcd:busy() then tp:start() else next() end
  63. end)
  64. lcd:home() -- work around busy polling incrementing position (XXX)
  65. ok(eq(0x20, lcd:read()), "is space")
  66. ok(eq(1, lcd:position())) -- having just read 1 from home, we should be at 1
  67. end)