test_max7219.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2014-18 Richard Hull and contributors
  4. # See LICENSE.rst for details.
  5. import pytest
  6. from luma.led_matrix.device import max7219
  7. from luma.core.render import canvas
  8. from helpers import setup_function, serial, assert_invalid_dimensions # noqa: F401
  9. from unittest.mock import call
  10. def test_init_cascaded():
  11. device = max7219(serial, cascaded=4)
  12. assert device.width == 32
  13. assert device.height == 8
  14. def test_init_reversed():
  15. device = max7219(serial, cascaded=4, blocks_arranged_in_reverse_order=True)
  16. assert device.blocks_arranged_in_reverse_order is True
  17. def test_init_8x8():
  18. device = max7219(serial)
  19. assert device.cascaded == 1
  20. serial.data.assert_has_calls([
  21. call([11, 7]),
  22. call([9, 0]),
  23. call([15, 0]),
  24. call([10, 7]),
  25. call([1, 0]),
  26. call([2, 0]),
  27. call([3, 0]),
  28. call([4, 0]),
  29. call([5, 0]),
  30. call([6, 0]),
  31. call([7, 0]),
  32. call([8, 0]),
  33. call([12, 1])
  34. ])
  35. def test_init_16x8():
  36. device = max7219(serial, width=16, height=8)
  37. assert device.cascaded == 2
  38. serial.data.assert_has_calls([
  39. call([11, 7, 11, 7]),
  40. call([9, 0, 9, 0]),
  41. call([15, 0, 15, 0]),
  42. call([10, 7, 10, 7]),
  43. call([1, 0, 1, 0]),
  44. call([2, 0, 2, 0]),
  45. call([3, 0, 3, 0]),
  46. call([4, 0, 4, 0]),
  47. call([5, 0, 5, 0]),
  48. call([6, 0, 6, 0]),
  49. call([7, 0, 7, 0]),
  50. call([8, 0, 8, 0]),
  51. call([12, 1, 12, 1])
  52. ])
  53. def test_init_invalid_dimensions():
  54. assert_invalid_dimensions(max7219, serial, 59, 22)
  55. def test_hide():
  56. device = max7219(serial, cascaded=5)
  57. serial.reset_mock()
  58. device.hide()
  59. serial.data.assert_called_once_with([12, 0] * 5)
  60. def test_show():
  61. device = max7219(serial, cascaded=3)
  62. serial.reset_mock()
  63. device.show()
  64. serial.data.assert_called_once_with([12, 1] * 3)
  65. def test_contrast():
  66. device = max7219(serial, cascaded=6)
  67. serial.reset_mock()
  68. device.contrast(0x6B)
  69. serial.data.assert_called_once_with([10, 6] * 6)
  70. def test_display_16x8():
  71. device = max7219(serial, cascaded=2)
  72. serial.reset_mock()
  73. with canvas(device) as draw:
  74. draw.rectangle(device.bounding_box, outline="white")
  75. serial.data.assert_has_calls([
  76. call([1, 0x81, 1, 0xFF]),
  77. call([2, 0x81, 2, 0x81]),
  78. call([3, 0x81, 3, 0x81]),
  79. call([4, 0x81, 4, 0x81]),
  80. call([5, 0x81, 5, 0x81]),
  81. call([6, 0x81, 6, 0x81]),
  82. call([7, 0x81, 7, 0x81]),
  83. call([8, 0xFF, 8, 0x81])
  84. ])
  85. def test_display_16x16():
  86. device = max7219(serial, width=16, height=16)
  87. serial.reset_mock()
  88. with canvas(device) as draw:
  89. draw.rectangle(device.bounding_box, outline="white")
  90. serial.data.assert_has_calls([
  91. call([1, 0x80, 1, 0xFF, 1, 0x01, 1, 0xFF]),
  92. call([2, 0x80, 2, 0x80, 2, 0x01, 2, 0x01]),
  93. call([3, 0x80, 3, 0x80, 3, 0x01, 3, 0x01]),
  94. call([4, 0x80, 4, 0x80, 4, 0x01, 4, 0x01]),
  95. call([5, 0x80, 5, 0x80, 5, 0x01, 5, 0x01]),
  96. call([6, 0x80, 6, 0x80, 6, 0x01, 6, 0x01]),
  97. call([7, 0x80, 7, 0x80, 7, 0x01, 7, 0x01]),
  98. call([8, 0xFF, 8, 0x80, 8, 0xFF, 8, 0x01])
  99. ])
  100. def test_normal_alignment():
  101. device = max7219(serial, cascaded=2, block_orientation=0)
  102. serial.reset_mock()
  103. with canvas(device) as draw:
  104. draw.rectangle((0, 0, 15, 3), outline="white")
  105. serial.data.assert_has_calls([
  106. call([1, 0x09, 1, 0x0F]),
  107. call([2, 0x09, 2, 0x09]),
  108. call([3, 0x09, 3, 0x09]),
  109. call([4, 0x09, 4, 0x09]),
  110. call([5, 0x09, 5, 0x09]),
  111. call([6, 0x09, 6, 0x09]),
  112. call([7, 0x09, 7, 0x09]),
  113. call([8, 0x0F, 8, 0x09])
  114. ])
  115. def test_block_realignment_minus90():
  116. device = max7219(serial, cascaded=2, block_orientation=-90)
  117. serial.reset_mock()
  118. with canvas(device) as draw:
  119. draw.rectangle((0, 0, 15, 3), outline="white")
  120. serial.data.assert_has_calls([
  121. call([1, 0x00, 1, 0x00]),
  122. call([2, 0x00, 2, 0x00]),
  123. call([3, 0x00, 3, 0x00]),
  124. call([4, 0x00, 4, 0x00]),
  125. call([5, 0xFF, 5, 0xFF]),
  126. call([6, 0x80, 6, 0x01]),
  127. call([7, 0x80, 7, 0x01]),
  128. call([8, 0xFF, 8, 0xFF])
  129. ])
  130. def test_block_realignment_plus90():
  131. device = max7219(serial, cascaded=2, block_orientation=90)
  132. serial.reset_mock()
  133. with canvas(device) as draw:
  134. draw.rectangle((0, 0, 15, 3), outline="white")
  135. serial.data.assert_has_calls([
  136. call([1, 0xFF, 1, 0xFF]),
  137. call([2, 0x01, 2, 0x80]),
  138. call([3, 0x01, 3, 0x80]),
  139. call([4, 0xFF, 4, 0xFF]),
  140. call([5, 0x00, 5, 0x00]),
  141. call([6, 0x00, 6, 0x00]),
  142. call([7, 0x00, 7, 0x00]),
  143. call([8, 0x00, 8, 0x00])
  144. ])
  145. def test_block_realignment_plus180():
  146. device = max7219(serial, cascaded=2, block_orientation=180)
  147. serial.reset_mock()
  148. with canvas(device) as draw:
  149. draw.rectangle((0, 0, 15, 3), outline="white")
  150. serial.data.assert_has_calls([
  151. call([1, 0xF0, 1, 0x90]),
  152. call([2, 0x90, 2, 0x90]),
  153. call([3, 0x90, 3, 0x90]),
  154. call([4, 0x90, 4, 0x90]),
  155. call([5, 0x90, 5, 0x90]),
  156. call([6, 0x90, 6, 0x90]),
  157. call([7, 0x90, 7, 0x90]),
  158. call([8, 0x90, 8, 0xF0])
  159. ])
  160. def test_reversed_max7219():
  161. device = max7219(serial, cascaded=4, blocks_arranged_in_reverse_order=True)
  162. serial.reset_mock()
  163. with canvas(device) as draw:
  164. draw.rectangle((0, 0, 15, 3), outline="white")
  165. serial.data.assert_has_calls([
  166. call([1, 15, 1, 9, 1, 0, 1, 0]),
  167. call([2, 9, 2, 9, 2, 0, 2, 0]),
  168. call([3, 9, 3, 9, 3, 0, 3, 0]),
  169. call([4, 9, 4, 9, 4, 0, 4, 0]),
  170. call([5, 9, 5, 9, 5, 0, 5, 0]),
  171. call([6, 9, 6, 9, 6, 0, 6, 0]),
  172. call([7, 9, 7, 9, 7, 0, 7, 0]),
  173. call([8, 9, 8, 15, 8, 0, 8, 0])
  174. ])
  175. def test_unknown_block_orientation():
  176. with pytest.raises(AssertionError):
  177. max7219(serial, cascaded=2, block_orientation="sausages")