Beispiel #1
0
def reset_demo():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    print_millis(a)
    print ('soft reset')
    a.soft_reset()
    print_millis(a)
Beispiel #2
0
def test_pwm():
    a = ArduinoTree()
    pin8 = a.pin.get(8)
    pin9 = a.pin.get(9)

    TCCR1B = a.register.get('TCCR1B')

    eq_(pin8.pwm.available, False)
    eq_(pin9.pwm.available, True)
    eq_(pin9.pwm.timer_register_name_b, 'TCCR1B')
    eq_(pin9.pwm.base_divisor, 512)
    eq_(pin9.pwm.divisors_available, [1, 8, 64, 256, 1024])

    frequencies = sorted([
        F_CPU / 2**9,
        F_CPU / 2**12,
        F_CPU / 2**15,
        F_CPU / 2**17,
        F_CPU / 2**19,
    ])
    eq_(pin9.pwm.frequencies_available, frequencies)

    TCCR1B.value = 3
    eq_(TCCR1B.value, 3)

    eq_(pin9.pwm.read_divisor(), 2**6)
    eq_(pin9.pwm.divisor, 2**6)
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**15)
    eq_(pin9.pwm.frequency, F_CPU / 2**15)

    pin9.pwm.write_frequency(int(F_CPU / 2**19))
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**19)
    eq_(TCCR1B.value, 5)

    TCCR1B.value = 2
    eq_(pin9.pwm.read_divisor(), 2**3)
    eq_(pin9.pwm.divisor, 2**3)
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**12)
    eq_(pin9.pwm.frequency, F_CPU / 2**12)
    eq_(TCCR1B.value, 2)

    pin9.pwm.divisor = 2**10
    eq_(pin9.pwm.read_divisor(), 2**10)
    eq_(pin9.pwm.divisor, 2**10)
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**19)
    eq_(pin9.pwm.frequency, F_CPU / 2**19)
    eq_(TCCR1B.value, 5)

    pin9.pwm.write_frequency(F_CPU / 2**9)
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**9)
    eq_(TCCR1B.value, 1)

    pin9.pwm.frequency = F_CPU / 2**17
    eq_(pin9.pwm.read_frequency(), F_CPU / 2**17)
    eq_(TCCR1B.value, 4)

    TCCR1B.value = 3
    eq_(TCCR1B.value, 3)

    pin9.pwm.write_value(45)
def ItwoCmain(): 
        connection = SerialManager(sleep_after_connect=2)
        connection.open() 
        print(connection.device)
        a = ArduinoTree(connection=connection)
        master = I2C_Master(a.wire)
        master.send(I2C_ADDRESS,[0b00001000])
Beispiel #4
0
def test_counter():
    a = ArduinoTree()
    p = a.pin.get(5)
    p.write_mode(OUTPUT)
    p.pwm.write_value(128)

    counter1 = a.counter
    counter2 = Counter(F_CPU=config['F_CPU'])

    print('frequencies_available: %s' % p.pwm.frequencies_available)
    for fset in p.pwm.frequencies_available:
        p.pwm.frequency = fset
        print('---------------------------')
        print('fset=%s' % fset)
        print('---------------------------')
        for ms in [10, 20, 50, 100, 200, 500, 1000]:
            print('gate=%s ms' % ms)
            t = ms / 1000.0
            error = a.counter.error(t)
            print('error=%s' % error)

            f1 = counter1.read_frequency(t)
            f2 = counter2.read_frequency(t)
            print(f1, f2)
            diff1 = abs(f1 - fset)
            diff2 = abs(f2 - fset)
            ok_(diff1 <= (1.1 * error))
            ok_(diff2 <= (1.1 * error))
Beispiel #5
0
def counterdemo():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    p = a.pin.get(5)
    a.soft_reset()
    print ('PWM frequencies_available: %s' % p.pwm.frequencies_available)
    p.write_mode(1)
    p.pwm.write_value(128)
    p.pwm.frequency = F
    print ('frequency set: %s' % p.pwm.frequency)
    t1 = time.time()
    fread = a.counter.read_frequency(GATE_TIME)
    t2 = time.time()
    print ('frequency read: %s' % fread)
    print ('gate time: %s sec' % (GATE_TIME))
    print ('time elapsed: %s sec' % (t2 - t1))
Beispiel #6
0
def main():
    connection = SerialManager(sleep_after_connect=2)
    connection.open()
    print(connection.device)
    a = ArduinoTree(connection=connection)
    master = I2C_Master(a.wire)
    print(['0x%02x' % x for x in master.scan()])
Beispiel #7
0
def test_dig():
    a = ArduinoTree()
    pin = a.pin.get(8)
    pin.reset()

    pin.write_mode(OUTPUT)

    pin.write_digital_value(1)

    eq_(pin.read_digital_value(), 1)
    eq_(pin.digital_value, 1)

    pin.write_mode(INPUT)
    pin.write_mode(OUTPUT)

    pin.write_digital_value(0)
    eq_(pin.read_digital_value(), 0)
    eq_(pin.digital_value, 0)

    pin.write_mode(INPUT)
    pin.write_mode(OUTPUT)

    pin.write_mode(INPUT)
    pin.write_pullup(True)

    eq_(pin.read_digital_value(), 1)
    eq_(pin.digital_value, 1)
Beispiel #8
0
def test_defs():
    a = ArduinoTree()

    eq_(a.define.get('A0'), config['A0'])

    d = a.define.as_dict
    print(d)

    eq_(a.define.get('ARDUINO'), config['ARDUINO'])
    eq_(d['ARDUINO'], config['ARDUINO'])

    eq_(d['A0'], config['A0'])
    eq_(d['F_CPU'], config['F_CPU'])
    eq_(d['MCU'], config['MCU'])
    ok_('xx' not in d)
    ok_(len(d.keys()) > 15, len(d.keys()))

    ARDUINO = a.define.get('ARDUINO')
    ok_(ARDUINO >= 100)
    ok_(ARDUINO < 200)

    NUM_DIGITAL_PINS = a.define.get('NUM_DIGITAL_PINS')
    ok_(NUM_DIGITAL_PINS >= 10)
    ok_(NUM_DIGITAL_PINS < 1000)

    for x in d:
        assert x.strip(), 'empty define:-->%s<--' % x
Beispiel #9
0
def checkspeed(n):
    connection = SerialManager(device='/dev/ttyACM0')
    connection.open()
    a = ArduinoTree(connection=connection)

    print('performance test for ArduinoTree()')
    print('n=%s' % n)
    print('')

    measure(a, n, 'api.digitalRead(0)')
    measure(a, n, 'api.digitalWrite(0,0)')
    measure(a, n, 'api.analogRead(0)')
    measure(a, n, 'api.analogWrite(0,0)')
    measure(a, n, 'api.pinMode(0,0)')
    measure(a, n, 'api.millis()')
    measure(a, n, 'api.shiftOut(0, 1, 0, 15)')
    measure(a, n, 'api.pulseIn(0, 0)')

    print('')
    print('performance test for AD9850()')
    print('')

    ad9850 = AD9850([0, 1, 2, 3])
    measure(ad9850, n, 'setup()', root='ad9850')
    measure(ad9850, n, 'write_frequency(400)', root='ad9850')
Beispiel #10
0
    def _connect_fired(self):
            try:
                connection = SerialManager(device=self.serial_device,
                                           baudrate=self.baudrate,
                                           sleep_after_connect=self.sleep_after_connect,
                                           timeout=self.timeout)
                connection.open()
                print ArduinoApi(connection=connection).millis()
            except Exception as e:
                traceback.print_exc()
                message(traceback.format_exc(), buttons=['OK'])
                return

            a = self.tree = ArduinoTree(connection=connection)

            d = a.define.as_dict
            s = [GuiDefine(name=k, value=str(v)) for k, v in d.items()]
            s.sort(key=lambda x: x.name)
            self.defines = s
            self.digital_pins = [PinWrapper(pin=a.pin.get(x))
                                 for x in a.pin.names_digital]
            self.analog_pins = [PinWrapper(pin=a.pin.get(x))
                                for x in a.pin.names_analog]
            self.pins = self.digital_pins + self.analog_pins

            fw = a.firmware_info
            self.arduino_version = fw.get('arduino_version')
            self.firmware_build_time = str(fw.get('compile_datetime'))
            self.avr_name = fw.get('avr_name')
            self.gcc_version = fw.get('gcc_version')
            self.libc_version = fw.get('libc_version')
            self.libc_date = str(fw.get('libc_date'))
            self.connected = True
def measure(config):
    mcu = ArduinoTree()
    #     mcu.soft_reset()
    vcc = mcu.vcc.read()

    timer = Stopwatch()

    measurements = []

    #     for _ in range(config.repeat):
    while 1:
        f = mcu.counter.read(config.gate_time)
        measurements.append(dict(
            t=timer.read(),
            frequency=nominal_value(f),
        ))
        if timer.last > config.interval:
            break

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        measurements=measurements,
        gate_time=config.gate_time,
    )

    return data
def measure(config, stop_condition=None):
    mcu = ArduinoTree()
    #     if config.reset:
    #         mcu.soft_reset()
    vcc = mcu.vcc.read()
    pin = mcu.pin.get(config.pin)
    interval = config.interval

    measurements = []
    timer = Stopwatch()
    while 1:
        measurements.append(dict(
            t=timer.read(),
            A=pin.read_analog_value(),
        ))
        if timer.last > interval:
            break
        if stop_condition:
            if stop_condition(timer):
                break

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        measurements=measurements,
    )

    return data
Beispiel #13
0
def counterdemo():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    p = a.pin.get(5)
    a.soft_reset()
    print('PWM frequencies_available: %s' % p.pwm.frequencies_available)
    p.write_mode(1)
    p.pwm.write_value(128)
    p.pwm.frequency = F
    print('frequency set: %s' % p.pwm.frequency)
    t1 = time.time()
    fread = a.counter.read_frequency(GATE_TIME)
    t2 = time.time()
    print('frequency read: %s' % fread)
    print('gate time: %s sec' % (GATE_TIME))
    print('time elapsed: %s sec' % (t2 - t1))
Beispiel #14
0
def main():
    connection = SerialManager(sleep_after_connect=2)
    connection.open()
    a = ArduinoTree(connection=connection)
    bmp = Bmp180(a.wire)
    p, t = bmp.read()
    print('pressure: %s kPa' % (p / 1000))
    print('temperature: %s C' % t)
Beispiel #15
0
def boot_time():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    a.watchdog.enable(a.watchdog.WDTO_500MS)
    time.sleep(1)
    a.connection.flush_input()
    x = a.api.millis()
    return 1000 - 500 - x
Beispiel #16
0
def dumpall():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)

    print((FORMAT + ' V') % ('read_vcc', a.vcc.read()))
    print((FORMAT + ' sec') % ('millis', a.api.millis() / 1000.0))

    print('')
    print('================================')
    print('firmware classes:')
    print('================================')
    print('status:')
    pprint(a.connection.classinfo.firmware_class_status)
    print('unknown ids:')
    pprint(a.connection.classinfo.unknown_firmware_ids)

    print('')
    print('================================')
    print('pins:')
    print('================================')

    print(FORMAT % ('total_pin_count', a.pin.count))
    print(FORMAT % ('digital_names', a.pin.names_digital))
    print(FORMAT % ('analog_names', a.pin.names_analog))

    for pin_number in range(a.pin.count):
        print('---------- pin_number=%s ---------------' % pin_number)
        pin = a.pin.get(pin_number)
        dump(
            pin,
            'name pin_number pin_number_analog is_digital is_analog avr_pin mode digital_value analog_value programming_function'.split())
        if pin.pwm.available:
            print('--- pwm ---')
            dump(pin.pwm, '''frequency frequencies_available base_divisor divisor divisors_available
                                timer_mode
                                timer_register_name_a
                                timer_register_name_b
                                wgm
            '''.split())

    print('')
    print('================================')
    print('defines:')
    print('================================')
    dump_dict(a.define.as_dict)

    print('')
    print('================================')
    print('registers:')
    print('================================')
    for x in a.register.names:
        r = a.register.get(x)
        if r.size == 2:
            v = '0x%04X' % (r.value)
        else:
            v = '  0x%02X' % (r.value)

        print('%-20s = %s @0x%2X (size:%s)' % (r.name, v, r.address, r.size))
Beispiel #17
0
def main():
    connection = SerialManager(sleep_after_connect=2)
    connection.open()
    a = ArduinoTree(connection=connection)
    bme = BME280(a.wire)
    t, h, p = bme.read()
    print('pressure: %d kPa' % (p / 1000))
    print('temperature: %d C' % t)
    print('humidity: %d %' % h)
Beispiel #18
0
def test_an():
    a = ArduinoTree()
    pin = a.pin.get('A0')
    pin.mode = INPUT
    ok_an(pin.read_analog_value())
    ok_an(pin.analog_value)

    pin.write_pullup(True)

    ok_an(pin.read_analog_value(), pullup=True)
Beispiel #19
0
def highfreqpwm():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    pin9 = a.pin.get(9)
    pin9.mode = 1
    pin9.write_digital_value(1)
    pwm = pin9.pwm
    print('set  frequency=%s Hz' % FREQ)
    pwm.set_high_freq_around(FREQ)
    print('real frequency=%s Hz' % pwm.read_frequency())
Beispiel #20
0
def fw_check():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)

    print('Firmware classes enabled in cfg.h:')
    print('  ' + '\n  '.join(a.connection.classinfo.firmware_name_list))

    d = a.define.as_dict
    print('\nYour firmware was built on:\n  %s %s' %
          (d.get('__DATE__'), d.get('__TIME__')))
Beispiel #21
0
def test():
    a = ArduinoTree()
    pin13 = a.pin.get(13)
    ok_(a.api.millis() > 1)

    ok_an(a.api.analogRead(0))

    eq_(pin13.read_mode(), 0)
    a.api.pinMode(13, 1)
    eq_(pin13.read_mode(), 1)
    a.api.pinMode(13, 0)
    eq_(pin13.read_mode(), 0)
Beispiel #22
0
def measure(config):
    mcu = ArduinoTree()
#     mcu.soft_reset()
    vcc = mcu.vcc.read()
    p_pwm = mcu.pin.get(config.pin_pwm)
    pwm_manager = PwmManager(config.pwm, [p_pwm])
    p_in = mcu.pin.get(config.pin_in)
    timer = Stopwatch()

    # max freq
#    pwm.divisor = config.pwm.divisor
#    print 'freq=%s' % pwm.frequency

    def meas():
        def loop(measurements, pwm_value):
            measurements.append(dict(
                                t=timer.read(),
                                pwm_value=pwm_value,
                                Ain=p_in.read_analog_value(),
                                ))
        measurements = pwm_manager.measure(loop)

        return measurements

#        pwm.write_value(config.pwm.start)

#        time.sleep(1)

#        measurements = []
#        tstart = time.time()
#        for i in range(config.pwm.start, config.pwm.end + 1):
#            pwm.write_value(i)
#            time.sleep(config.wait)

#            A = p_in.read_analog_value()
#            measurements.append(dict(
#                                t=timer.read(),
#                                pwm_value=i,
#                                A=A,
#                                ))
#        return measurements

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        measurements=meas(),
        frequency=p_pwm.pwm.frequency,
    )

    p_pwm.reset()

    return data
Beispiel #23
0
def ramdump():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    size = a.ram.size
    free = a.ram.free()
    print('Reading RAM (%s bytes, %s bytes free)...' % (size, free))

    data_list = []
    for i in range(0, size):
        s = a.ram.read(i)
        data_list.append(s)

    display(data_list, size)
Beispiel #24
0
def pwm_demo():
    connection = SerialManager()
    a = ArduinoTree(connection=connection)
    p = a.pin.get(PIN)
    print('PWM frequencies_available: %s' % p.pwm.frequencies_available)
    p.write_mode(OUTPUT)
    p.pwm.write_value(VALUE)

    # set frequency here
    p.pwm.frequency = 61

    print('PWM frequency: %s' % p.pwm.frequency)
    print('PWM duty cycle: %s %% ' % (DUTY_CYCLE * 100))
Beispiel #25
0
def test_pin_range():
    a = ArduinoTree()
    eq_(a.pin.count, 20)
    eq_(a.pin.count_analog, 6)
    eq_(a.pin.count_digital, 14)

    #     eq_(a.pin.get.range_all, range(0, 20))
    #     eq_(a.pin.get.range_analog, range(14, 20))
    #     eq_(a.pin.get.range_digital, range(0, 14))

    a.pin.get('A5')
    exc_(ValueError, lambda: a.pin.get('A6'))
    exc_(ValueError, lambda: a.pin.get('D14'))
def measure(config):
    mcu = ArduinoTree()
    #     mcu.soft_reset()
    vcc = mcu.vcc.read()
    p_pwm = mcu.pin.get(config.pin_pwm)
    pwm_manager = PwmManager(config.pwm, [p_pwm])
    #    pwm = p_pwm.pwm
    p_amp = mcu.pin.get(config.pin_amp_out)
    p_out_an = mcu.pin.get(config.pin_out_an)
    p_out_dig = mcu.pin.get(config.pin_out_dig)
    timer = Stopwatch()

    #    pwm.divisor = config.pwm.divisor

    def meas(dig_out):
        p_out_dig.write_digital_value(dig_out)

        def loop(measurements, pwm_value):
            measurements.append(
                dict(
                    t=timer.read(),
                    pwm_value=pwm_value,
                    Aamp=p_amp.read_analog_value(),
                    Aout=p_out_an.read_analog_value(),
                    Dout=dig_out,
                ))

        measurements = pwm_manager.measure(loop)

        return measurements

    measurements = []

    p_out_dig.write_mode(OUTPUT)
    measurements += meas(1)
    measurements += meas(0)

    p_out_dig.reset()
    p_pwm.reset()

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        frequency=p_pwm.pwm.frequency,
        measurements=measurements,
    )

    return data
Beispiel #27
0
def measure(config):
    mcu = ArduinoTree()
    #     mcu.soft_reset()
    vcc = mcu.vcc.read()
    p_enable_input = mcu.pin.get(config.pin_enable_input)

    timer = Stopwatch()

    def measure1(enable_input, repeat):
        if enable_input:
            p_enable_input.write_mode(OUTPUT)
            p_enable_input.write_digital_value(1)
        else:
            p_enable_input.reset()
        measurements = []

        for _ in range(repeat):
            f = mcu.counter.read(config.gate_time)
            measurements.append(
                dict(
                    t=timer.read(),
                    frequency=nominal_value(f),
                    enable_input=int(enable_input),
                ))
        return measurements

    measurements = []
    #     measurements += measure1(False, config.repeat_disable_input)
    #     measurements += measure1(True, config.repeat_enable_input)
    measurements += measure1(False, 8)
    measurements += measure1(True, 8)
    measurements += measure1(False, 8)
    measurements += measure1(True, 15)
    measurements += measure1(False, 8)
    measurements += measure1(True, 30)

    p_enable_input.reset()

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        measurements=measurements,
        gate_time=config.gate_time,
    )

    return data
Beispiel #28
0
def test_register_proxy():
    a = ArduinoTree()
    r = a.register.proxy

    eq_(r.DDRB, 0)

    r.DDRB = 5
    eq_(r.DDRB, 5)
    r.DDRB = 3
    eq_(r.DDRB, 3)
    eq_(a.register.get('DDRB').value, 3)

    r.DDRB = 0

    # 9 bit
    r.EEAR = 511
    eq_(r.EEAR, 511)
Beispiel #29
0
def measure(config):
    mcu = ArduinoTree()
    #     mcu.soft_reset()
    vcc = mcu.vcc.read()
    p_pwm = mcu.pin.get(config.pin_pwm)
    pwm_manager = PwmManager(config.pwm, [p_pwm])
    p_amp = mcu.pin.get(config.pin_amp_out)
    p_in = mcu.pin.get(config.pin_x_in)
    p_out = mcu.pin.get(config.pin_x_out)
    p_rail = mcu.pin.get(config.pin_rail)
    timer = Stopwatch()

    def meas(rail):
        p_rail.write_mode(OUTPUT)
        p_rail.write_digital_value(rail)

        def loop(measurements, pwm_value):
            measurements.append(
                dict(
                    t=timer.read(),
                    pwm_value=pwm_value,
                    Aamp=p_amp.read_analog_value(),
                    Ain=p_in.read_analog_value(),
                    Aout=p_out.read_analog_value(),
                    rail=rail,
                ))

        measurements = pwm_manager.measure(loop)
        p_rail.reset()

        return measurements

    measurements = []
    measurements += meas(1)
    measurements += meas(0)

    p_pwm.reset()

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        pwm_frequency=p_pwm.pwm.frequency,
        measurements=measurements,
    )
    return data
def test_ports():
    a = ArduinoTree()
    eq_(a.core.portInputRegister(0), 0)  # NOT_A_PORT
    eq_(a.core.portInputRegister(1), 0)  # NOT_A_PORT
    eq_(a.core.portInputRegister(2), 35)  # PINB
    eq_(a.core.portInputRegister(3), 38)  # PINC
    eq_(a.core.portInputRegister(4), 41)  # PIND

    eq_(a.core.portModeRegister(0), 0)  # NOT_A_PORT
    eq_(a.core.portModeRegister(1), 0)  # NOT_A_PORT
    eq_(a.core.portModeRegister(2), 36)  # DDRB
    eq_(a.core.portModeRegister(3), 39)  # DDRC
    eq_(a.core.portModeRegister(4), 42)  # DDRD

    eq_(a.core.portOutputRegister(0), 0)  # NOT_A_PORT
    eq_(a.core.portOutputRegister(1), 0)  # NOT_A_PORT
    eq_(a.core.portOutputRegister(2), 37)  # PORTB
    eq_(a.core.portOutputRegister(3), 40)  # PORTC
    eq_(a.core.portOutputRegister(4), 43)  # PORTD
def measure(config):
    mcu = ArduinoTree()
    #     mcu.soft_reset()

    vcc = mcu.vcc.read()
    p_pwm = mcu.pin.get(config.pin_pwm)
    pwm_manager = PwmManager(config.pwm, [p_pwm])
    #    p_in = mcu.pin.get(config.pin_x_in)
    #    p_out = mcu.pin.get(config.pin_x_out)
    #    p_rail = mcu.pin.get(config.pin_rail)
    pin_an_in = mcu.pin.get(config.pin_an_in)
    pin_dig_in = mcu.pin.get(config.pin_dig_in)

    timer = Stopwatch()

    def meas(reverse):
        def loop(measurements, pwm_value):
            measurements.append(
                dict(
                    t=timer.read(),
                    pwm_value=pwm_value,
                    Ain=pin_an_in.read_analog_value(),
                    Din=pin_dig_in.read_digital_value(),
                    reverse=reverse,
                ))

        measurements = pwm_manager.measure(loop, reverse=reverse)
        return measurements

    measurements = []
    measurements += meas(0)
    measurements += meas(1)

    p_pwm.reset()

    data = dict(
        vcc=vcc,
        model=avr_name(mcu),
        pwm_frequency=p_pwm.pwm.frequency,
        measurements=measurements,
    )
    return data
Beispiel #32
0
def test_long():
    a = ArduinoTree()
    for x in range(REPEAT_TEST):
        print (x, ('uptime: %s sec' % (a.api.millis() / 1000.0)))
        a.soft_reset()