Exemplo n.º 1
0
def test_mode():
    assert GPIO.getmode() is None
    GPIO.setmode(GPIO.BCM)
    assert GPIO.getmode() == GPIO.BCM
    GPIO.cleanup()
    assert GPIO.getmode() is None
    with pytest.raises(AssertionError):
        GPIO.setmode(54335)
Exemplo n.º 2
0
def test_custom_dict():
    GPIO.cleanup()
    assert GPIO.getmode() is None
    with patch("OPi.GPIO.sysfs") as mock:
        GPIO.setmode({"A": 5, "B": 37})
        assert GPIO.getmode() is GPIO.CUSTOM
        GPIO.setup("A", GPIO.IN)
        mock.export.assert_called_with(5)
        mock.direction.assert_called_with(5, GPIO.IN)
        assert "A" in GPIO._exports
Exemplo n.º 3
0
def test_custom_object():
    class mapper(object):
        def __getitem__(self, value):
            return value + 4

    GPIO.cleanup()
    assert GPIO.getmode() is None
    with patch("OPi.GPIO.sysfs") as mock:
        GPIO.setmode(mapper())
        assert GPIO.getmode() is GPIO.CUSTOM
        GPIO.setup(11, GPIO.IN)
        mock.export.assert_called_with(15)
        mock.direction.assert_called_with(15, GPIO.IN)
        assert 11 in GPIO._exports
    from hx711 import HX711
else:
    from emulated_hx711 import HX711


def cleanAndExit():
    print "Cleaning..."

    if not EMULATE_HX711:
        GPIO.cleanup()

    print "Bye!"
    sys.exit()


print GPIO.getmode()

hx = HX711(7, 3)

# I've found out that, for some reason, the order of the bytes is not always the same between versions of python, numpy and the hx711 itself.
# Still need to figure out why does it change.
# If you're experiencing super random values, change these values to MSB or LSB until to get more stable values.
# There is some code below to debug and log the order of the bits and the bytes.
# The first parameter is the order in which the bytes are used to build the "long" value.
# The second paramter is the order of the bits inside each byte.
# According to the HX711 Datasheet, the second parameter is MSB so you shouldn't need to modify it.
hx.set_reading_format("MSB", "MSB")

# HOW TO CALCULATE THE REFFERENCE UNIT
# To set the reference unit to 1. Put 1kg on your sensor or anything you have and know exactly how much it weights.
# In this case, 92 is 1 gram because, with 1 as a reference unit I got numbers near 0 without any weight
Exemplo n.º 5
0
import OPi.GPIO as GPIO
import time


def init_pins(pins):
    for pin in pins:
        print('Configuring pin {}'.format(pin))
        GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)


GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
print('Mode is board {}'.format(GPIO.getmode() == GPIO.BOARD))

left_control_pins = [3, 5, 7, 16]
right_control_pins = [15, 19, 21, 23]

init_pins(left_control_pins)
init_pins(right_control_pins)

GPIO.cleanup()