[go: up one dir, main page]

File: test_parallel.py

package info (click to toggle)
luma.core 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,040 kB
  • sloc: python: 6,186; makefile: 204
file content (83 lines) | stat: -rw-r--r-- 2,363 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:module:`luma.core.interface.parallel` module.
"""

from unittest.mock import Mock, call
from luma.core.interface.parallel import bitbang_6800
import luma.core.error

import pytest

from helpers import rpi_gpio_missing, assert_only_cleans_whats_setup


gpio = Mock(unsafe=True)


def setup_function(function):
    gpio.reset_mock()
    gpio.HIGH = 200
    gpio.LOW = 100
    gpio.RS = 7
    gpio.E = 8
    gpio.PINS = [25, 24, 23, 18]
    gpio.DATA = gpio.HIGH
    gpio.CMD = gpio.LOW
    gpio.OUT = 300


def test_data():
    eight_to_four = lambda data: [f(x) for x in data for f in (lambda x: x >> 4, lambda x: 0x0F & x)]

    data = (0x41, 0x42, 0x43)  # ABC
    serial = bitbang_6800(gpio=gpio, RS=7, E=8, PINS=[25, 24, 23, 18])

    serial.command(*eight_to_four([0x80]))
    serial.data(eight_to_four(data))

    setup = [call(gpio.RS, gpio.OUT), call(gpio.E, gpio.OUT)] + \
        [call(gpio.PINS[i], gpio.OUT) for i in range(4)]
    prewrite = lambda mode: [call(gpio.RS, mode), call(gpio.E, gpio.LOW)]
    pulse = [call(gpio.E, gpio.HIGH), call(gpio.E, gpio.LOW)]
    send = lambda v: [call(gpio.PINS[i], (v >> i) & 0x01) for i in range(serial._datalines)]

    calls = \
        prewrite(gpio.CMD) + send(0x08) + pulse + send(0x00) + pulse + \
        prewrite(gpio.DATA) + \
        send(data[0] >> 4) + pulse + \
        send(data[0]) + pulse + \
        send(data[1] >> 4) + pulse + \
        send(data[1]) + pulse + \
        send(data[2] >> 4) + pulse + \
        send(data[2]) + pulse

    gpio.setup.assert_has_calls(setup)
    gpio.output.assert_has_calls(calls)


def test_wrong_number_of_pins():
    try:
        bitbang_6800(gpio=gpio, RS=7, E=8, PINS=[25, 24, 23])
    except AssertionError as ex:
        assert str(ex) == 'You\'ve provided 3 pins but a bus must contain either four or eight pins'


def test_cleanup():
    serial = bitbang_6800(gpio=gpio)
    serial._managed = True
    serial.cleanup()
    assert_only_cleans_whats_setup(gpio)


def test_unsupported_gpio_platform():
    try:
        bitbang_6800()
    except luma.core.error.UnsupportedPlatform as ex:
        assert str(ex) == 'GPIO access not available'
    except ImportError:
        pytest.skip(rpi_gpio_missing)