~ni/nxt-python

a0aaf25e283a4edc92d3ace8567da401aeee3724 — Jared Dunbar 1 year, 1 month ago 10796b9 test-superpro
Examples now use 'with'
M examples/hitechnic-superpro/example1.py => examples/hitechnic-superpro/example1.py +17 -17
@@ 10,23 10,23 @@ When you press the button, it will turn on the LED.
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

# Configure B0 as output
pro.set_digital_modes_byte(0x01)
    # Configure B0 as output
    pro.set_digital_modes_byte(0x01)

while True:
    try:
        # The original demo doesn't convert to volts, but I think displaying volts to the user is better than ADC bits.
        analog_value = pro.get_analog_volts()["a0"]
        print("Analog 0: {}V".format(analog_value))
        if analog_value > 3.3/2.0:
            pro.set_digital_byte(0x01)
        else:
            pro.set_digital_byte(0x00)
    except KeyboardInterrupt:
        break
    while True:
        try:
            # The original demo doesn't convert to volts, but I think displaying volts to the user is better than bits.
            analog_value = pro.get_analog_volts()["a0"]
            print("Analog 0: {}V".format(analog_value))
            if analog_value > 3.3/2.0:
                pro.set_digital_byte(0x01)
            else:
                pro.set_digital_byte(0x00)
        except KeyboardInterrupt:
            break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)
    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)

M examples/hitechnic-superpro/example2.py => examples/hitechnic-superpro/example2.py +23 -24
@@ 10,27 10,26 @@ When you turn the dial, the voltage changes and the LED's turn on roughly indica
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)

# Configure B0-5 as output
pro.set_digital_modes_byte(0x3F)

while True:
    try:
        # The original demo doesn't convert to volts, but I think displaying volts to the user is better than ADC bits.
        analog_value = pro.get_analog_volts()["a0"]
        print("Analog 0: {}V".format(analog_value))
        # Convert voltage to 6 divisions
        segmented = int((analog_value / 3.3) * 6)
        # This prevents 3.3V "exactly" from being no LED's lit.
        if segmented == 6:
            segmented = 5
        # Set the corresponding bit
        pro.set_digital_byte(2 ** segmented)
    except KeyboardInterrupt:
        break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)

with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

    # Configure B0-5 as output
    pro.set_digital_modes_byte(0x3F)

    while True:
        try:
            # The original demo doesn't convert to volts, but I think displaying volts to the user is better than bits.
            analog_value = pro.get_analog_volts()["a0"]
            print("Analog 0: {}V".format(analog_value))
            # Convert voltage to 6 divisions
            segmented = int((analog_value / 3.3) * 6)
            # This prevents 3.3V "exactly" from being no LED's lit.
            if segmented == 6:
                segmented = 5
            # Set the corresponding bit
            pro.set_digital_byte(2 ** segmented)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)

M examples/hitechnic-superpro/example3.py => examples/hitechnic-superpro/example3.py +24 -25
@@ 11,28 11,27 @@ When you change the distance, the LED's will roughly indicate the distance in 10
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)
ultrasonic = Ultrasonic(brick, nxt.sensor.Port.S4)

# Configure B0-5 as output
pro.set_digital_modes_byte(0x3F)

while True:
    try:
        # Get distance (cm)
        distance_cm = ultrasonic.get_distance()
        print("Distance: {}cm".format(distance_cm))
        # Convert distance to 6 divisions
        segmented = int(distance_cm / 10.0)
        # This prevents longer distances from turning off the LED
        if segmented > 6:
            segmented = 5
        # Set the corresponding bit
        pro.set_digital_byte(2 ** segmented)
    except KeyboardInterrupt:
        break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)

with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)
    ultrasonic = Ultrasonic(brick, nxt.sensor.Port.S4)

    # Configure B0-5 as output
    pro.set_digital_modes_byte(0x3F)

    while True:
        try:
            # Get distance (cm)
            distance_cm = ultrasonic.get_distance()
            print("Distance: {}cm".format(distance_cm))
            # Convert distance to 6 divisions
            segmented = int(distance_cm / 10.0)
            # This prevents longer distances from turning off the LED
            if segmented > 6:
                segmented = 5
            # Set the corresponding bit
            pro.set_digital_byte(2 ** segmented)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)

M examples/hitechnic-superpro/example4.py => examples/hitechnic-superpro/example4.py +15 -15
@@ 16,18 16,18 @@ with natural light levels in my workspace being around 1.9-2.0V
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)

while True:
    try:
        # Get brightness (measured in volts)
        analog_value = pro.get_analog_volts()["a0"]
        print("Analog 0: {}V".format(analog_value))
        # Sleep 0.1s to allow console to be read.
        sleep(0.1)
    except KeyboardInterrupt:
        break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

    while True:
        try:
            # Get brightness (measured in volts)
            analog_value = pro.get_analog_volts()["a0"]
            print("Analog 0: {}V".format(analog_value))
            # Sleep 0.1s to allow console to be read.
            sleep(0.1)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)

M examples/hitechnic-superpro/example6.py => examples/hitechnic-superpro/example6.py +28 -28
@@ 14,32 14,32 @@ If you press the B4 button, B0 turns on, and if you press B5, B1 turns on.
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)

# Configure B0,B1 as output
pro.set_digital_modes_byte(0b0000011)
pro.set_digital_byte(0x00)
while True:
    try:
        digital_input = pro.get_digital()
        left_button = digital_input["b4"]
        right_button = digital_input["b5"]
        left_button_status = ""
        if left_button:
            left_button_status = "Pressed"
        else:
            left_button_status = "Released"
        right_button_status = ""
        if right_button:
            right_button_status = "Pressed"
        else:
            right_button_status = "Released"
        print("Left Button: {}\nRight Button: {}\n".format(left_button_status, right_button_status))
        pro.set_digital_byte(left_button + right_button * 2)
    except KeyboardInterrupt:
        break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

    # Configure B0,B1 as output
    pro.set_digital_modes_byte(0b0000011)
    pro.set_digital_byte(0x00)
    while True:
        try:
            digital_input = pro.get_digital()
            left_button = digital_input["b4"]
            right_button = digital_input["b5"]
            left_button_status = ""
            if left_button:
                left_button_status = "Pressed"
            else:
                left_button_status = "Released"
            right_button_status = ""
            if right_button:
                right_button_status = "Pressed"
            else:
                right_button_status = "Released"
            print("Left Button: {}\nRight Button: {}\n".format(left_button_status, right_button_status))
            pro.set_digital_byte(left_button + right_button * 2)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)


M examples/hitechnic-superpro/example7.py => examples/hitechnic-superpro/example7.py +18 -18
@@ 16,21 16,21 @@ Note: My magnetic hall effect sensor appears to be broken, so I couldn't really 
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)

# Configure B4 as output
pro.set_digital_modes_byte(0b00010000)
pro.set_digital_byte(0x00)

while True:
    try:
        # Read B0 (magnet signal is inverted, low = magnet, then write back B4 with the value.
        hall_effect_sensor = pro.get_digital()["b0"]
        print("Magnet: {}".format(not hall_effect_sensor))
        pro.set_digital_byte((not hall_effect_sensor) << 4)
    except KeyboardInterrupt:
        break

# When program stopped, turn off outputs
pro.set_digital_byte(0x00)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

    # Configure B4 as output
    pro.set_digital_modes_byte(0b00010000)
    pro.set_digital_byte(0x00)

    while True:
        try:
            # Read B0 (magnet signal is inverted, low = magnet, then write back B4 with the value.
            hall_effect_sensor = pro.get_digital()["b0"]
            print("Magnet: {}".format(not hall_effect_sensor))
            pro.set_digital_byte((not hall_effect_sensor) << 4)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)

M examples/hitechnic-superpro/example9.py => examples/hitechnic-superpro/example9.py +21 -21
@@ 15,27 15,27 @@ F4_FREQ = 349
G4_FREQ = 392

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, C4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, DS4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, C4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, DS4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)

sleep(0.2)
    sleep(0.2)

pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, F4_FREQ, 3.3)
pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, A4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, G4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, C4_FREQ, 3.3)
pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
sleep(0.2)
pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)
pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, F4_FREQ, 3.3)
    pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, A4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
    pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, G4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, C4_FREQ, 3.3)
    pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, E4_FREQ, 3.3)
    sleep(0.2)
    pro.analog_out_voltage(0, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)
    pro.analog_out_voltage(1, SuperPro.AnalogOutputMode.SQUARE, 1, 0.0)

M examples/hitechnic-superpro/hitechnic-superpro-bar-display.py => examples/hitechnic-superpro/hitechnic-superpro-bar-display.py +14 -14
@@ 12,20 12,20 @@ WARNING: If you are light sensitive, avoid this demo. It flashes the B0 LED at ~
"""

# Find NXT, configure sensor
brick = nxt.locator.find()
pro = SuperPro(brick, nxt.sensor.Port.S1)
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

# Configure digital pins as outputs
# Outputs have 220 ohm resistors in series, so directly connect LED's from pins to GND
pro.set_digital_modes_byte(0xFF)
    # Configure digital pins as outputs.
    # Outputs have 220 ohm resistors in series, so directly connect LED's from pins to GND
    pro.set_digital_modes_byte(0xFF)

# For x in 0 to 255 (inclusive) - byte representation, Python range() is range(inclusive, exclusive)
for x in range(0, 256):
    pro.set_digital_byte(x)
    print("Outputting {0:3} ({0:<08b})".format(x))
    sleep(0.1)
    # For x in 0 to 255 (inclusive) - byte representation, Python range() is range(inclusive, exclusive)
    for x in range(0, 256):
        pro.set_digital_byte(x)
        print("Outputting {0:3} ({0:<08b})".format(x))
        sleep(0.1)

# Output 0 to turn off all pins
pro.set_digital_byte(0x00)
# Put all pins back as inputs (default state)
pro.set_digital_modes_byte(0x00)
    # Output 0 to turn off all pins
    pro.set_digital_byte(0x00)
    # Put all pins back as inputs (default state)
    pro.set_digital_modes_byte(0x00)