def buddy_get_adc_value(channel, sample_rate=1): hid_handle = buddy_open() general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() packet = bt.general_packet_t() general_settings.function = bt.GENERAL_CTRL_ADC_ENABLE general_settings.mode = bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = (1 << int(channel)) general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) timing_settings.averaging = 1 runtime_settings.adc_mode = bt.RUNTIME_ADC_MODE_SINGLE_ENDED runtime_settings.adc_ref = bt.RUNTIME_ADC_REF_VDD if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): return -1 time.sleep(0.05) err_code = bt.buddy_clear(hid_handle) err_code = bt.buddy_read_adc(hid_handle, packet, False) adc_value = bt.int32_t_ptr_getitem(packet.channels, int(channel)) return adc_value
def buddy_set_pwm_duty_value(channel, value, sample_rate=1000, streaming=False): hid_handle = buddy_open() general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_PWM_ENABLE general_settings.mode = bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = (1 << int(channel)) general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(int(sample_rate)) runtime_settings.pwm_mode = bt.RUNTIME_PWM_MODE_DUTY_CYCLE runtime_settings.pwm_timebase = bt.RUNTIME_PWM_TIMEBASE_SYSCLK if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): return -1 packet = bt.general_packet_t() for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, int(value)) if (bt.buddy_send_pwm(hid_handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): return -1 time.sleep(0.1) bt.buddy_flush(hid_handle)
def buddy_get_counter_value(channel, sample_rate=1000, streaming=False): hid_handle = buddy_open() general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() packet = bt.general_packet_t() general_settings.function = bt.GENERAL_CTRL_COUNTER_ENABLE general_settings.mode = bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = (1 << int(channel)) general_settings.resolution = bt.RESOLUTION_CTRL_SUPER timing_settings.period = bt.FREQUENCY_TO_NSEC(int(sample_rate)) runtime_settings.counter_control = bt.RUNTIME_COUNTER_CONTROL_ACTIVE_LOW if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): return -1 test_time_start = time.time() err_code = bt.buddy_read_counter(hid_handle, packet, streaming) start_value = bt.int32_t_ptr_getitem(packet.channels, int(channel)) time.sleep(1) err_code = bt.buddy_read_counter(hid_handle, packet, streaming) end_value = bt.int32_t_ptr_getitem(packet.channels, int(channel)) test_time_end = time.time() diff_value = end_value - start_value return diff_value
def test_seq_dac(handle, sample_rate, streaming, poncho_mode): mask = bt.BUDDY_CHAN_0_MASK | bt.BUDDY_CHAN_1_MASK | bt.BUDDY_CHAN_2_MASK | bt.BUDDY_CHAN_3_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_DAC_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_HIGH if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.dac_power = bt.RUNTIME_DAC_POWER_ON runtime_settings.dac_ref = bt.RUNTIME_DAC_REF_EXT if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_dac: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() test_seq_dac_count = 0 for k in range(0, 4095 + 1): #for k in range(2048, 2048 + 1): for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, k) print 'test_seq_dac: sending %d packet with value %d' % \ (test_seq_dac_count, k) test_seq_dac_count += 1 if (bt.buddy_send_dac(handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_dac: could not send DAC packet' return -1 if not streaming: time.sleep(1.0 / sample_rate) bt.buddy_flush(handle) time.sleep(0.1) return 0
def set_dac_value(hid_handle, channel, value, mode): general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_DAC_ENABLE general_settings.mode = bt.MODE_CTRL_STREAM general_settings.channel_mask = (1 << channel) general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(BUDDY_TEST_DAC_FREQ) runtime_settings.dac_power = bt.RUNTIME_DAC_POWER_ON runtime_settings.dac_ref = bt.RUNTIME_DAC_REF_EXT if mode: print('Poncho mode activated') general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = bt.BUDDY_CHAN_0_MASK else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'set_dac_value: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, value) print 'set_dac_value: setting DAC channel %d with value %d' % (channel, value) if (bt.buddy_send_dac(hid_handle, packet, True) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_dac: could not send DAC packet' return -1 time.sleep(1.0 / BUDDY_TEST_DAC_FREQ) bt.buddy_flush(hid_handle) time.sleep(0.1) return 0
def set_pwm_value(hid_handle, channel, value, poncho_mode): general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_PWM_ENABLE general_settings.mode = bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = (1 << channel) general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(BUDDY_TEST_PWM_FREQ) runtime_settings.pwm_mode = bt.RUNTIME_PWM_MODE_FREQUENCY runtime_settings.pwm_timebase = bt.RUNTIME_PWM_TIMEBASE_SYSCLK_DIV_12 if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = (1 << channel) else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'set_pwm_value: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, value) print 'set_pwm_value: setting PWM channel %d with value %d' % (channel, value) if (bt.buddy_send_pwm(hid_handle, packet, False) != bt.BUDDY_ERROR_CODE_OK): print 'set_pwm_value: could not send PWM packet' return -1 time.sleep(1.0 / BUDDY_TEST_PWM_FREQ) bt.buddy_flush(hid_handle) time.sleep(0.1) return 0
def buddy_set_dac_value(channel, value, sample_rate=1000, streaming=False): hid_handle = buddy_open() general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_DAC_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = (1 << int(channel)) general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(int(sample_rate)) runtime_settings.dac_power = bt.RUNTIME_DAC_POWER_ON runtime_settings.dac_ref = bt.RUNTIME_DAC_REF_EXT if (bt.buddy_configure(hid_handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): return -1 packet = bt.general_packet_t() for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, int(value)) if (bt.buddy_send_dac(hid_handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): return -1 if not streaming: time.sleep(1.0 / int(sample_rate)) time.sleep(0.5) bt.buddy_flush(hid_handle)
def test_seq_pwm_duty(handle, sample_rate, streaming, poncho_mode): mask = bt.BUDDY_CHAN_2_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_PWM_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_HIGH #general_settings.resolution = bt.RESOLUTION_CTRL_LOW timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.pwm_mode = bt.RUNTIME_PWM_MODE_DUTY_CYCLE runtime_settings.pwm_timebase = bt.RUNTIME_PWM_TIMEBASE_SYSCLK if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_pwm_duty: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() test_seq_pwm_count = 0 # in counts #for k in range(0, 65535, 10): #for k in range(128, 129): #for k in range(63,64): #for k in range(191, 192): #for k in range(32767, 32768): for k in range(16383, 16384): for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, k) print 'test_seq_pwm_duty: sending %d packet with value %d' % \ (test_seq_pwm_count, k) test_seq_pwm_count += 1 if (bt.buddy_send_pwm(handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_pwm_duty: could not send PWM packet' return -1 if not streaming: time.sleep(1.0 / sample_rate) bt.buddy_flush(handle) time.sleep(0.1) return 0
def test_seq_pwm_freq(handle, sample_rate, streaming, poncho_mode): mask = bt.BUDDY_CHAN_0_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_PWM_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = mask #general_settings.resolution = bt.RESOLUTION_CTRL_LOW general_settings.resolution = bt.RESOLUTION_CTRL_HIGH #general_settings.resolution = bt.RESOLUTION_CTRL_SUPER timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.pwm_mode = bt.RUNTIME_PWM_MODE_FREQUENCY runtime_settings.pwm_timebase = bt.RUNTIME_PWM_TIMEBASE_SYSCLK_DIV_12 if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_pwm_freq: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() test_seq_pwm_count = 0 #for k in range(50000, (50000 + 1)): #for k in range(6000, (6000 + 1)): for k in range(10000, (60000 + 1)): #for k in range(2, (500 + 1)): #for k in range(100000, (100000 + 1)): #for k in range(45000, (45000 +1)): for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, k) print 'test_seq_pwm_freq: sending %d packet with value %d' % \ (test_seq_pwm_count, k) test_seq_pwm_count += 1 if (bt.buddy_send_pwm(handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): print 'ERROR: test_seq_pwm_freq: could not send PWM packet' return -1 if not streaming: time.sleep(1.0 / sample_rate) bt.buddy_flush(handle) time.sleep(0.1) return 0
def test_seq_adc(handle, sample_rate, streaming, log_file, poncho_mode): mask = bt.BUDDY_CHAN_1_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() packet = bt.general_packet_t() general_settings.function = bt.GENERAL_CTRL_ADC_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_HIGH #general_settings.resolution = bt.RESOLUTION_CTRL_LOW timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) timing_settings.averaging = 1 runtime_settings.adc_mode = bt.RUNTIME_ADC_MODE_SINGLE_ENDED #runtime_settings.adc_mode = bt.RUNTIME_ADC_MODE_DIFFERENTIAL runtime_settings.adc_ref = bt.RUNTIME_ADC_REF_VDD if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_IN general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE print 'timing_settings.period = %d (0x%x)' % (timing_settings.period, timing_settings.period) # write CSV header if log_file: header = [] header.append('time') header.append('index') if runtime_settings.adc_mode == bt.RUNTIME_ADC_MODE_SINGLE_ENDED: for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): if (general_settings.channel_mask & (1 << j)): header.append('sensor %d' % j) elif runtime_settings.adc_mode == bt.RUNTIME_ADC_MODE_DIFFERENTIAL: for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_3 + 1): if (general_settings.channel_mask & (1 << j)): header.append('sensor %d' % j) try: log_file.writerow(header) except csv.Error as e: raise NameError('Could not write into CSV writer object') return False if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_adc: could not configure Buddy device' return -1 time.sleep(0.1) recv_packets = 0 first_packet = True for i in range(0, 10000): if ((recv_packets % 10) == 0): print('got packet {}'.format(recv_packets)) err_code = bt.buddy_read_generic_noblock(handle, packet, streaming, 1000) if err_code == bt.BUDDY_ERROR_CODE_OK: if first_packet: test_time_start = time.time() first_packet = False entry = [] entry.append('%.10f' % (time.time() - test_time_start)) entry.append('%d' % recv_packets) if runtime_settings.adc_mode == bt.RUNTIME_ADC_MODE_SINGLE_ENDED: for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): if (general_settings.channel_mask & (1 << j)): value = bt.int32_t_ptr_getitem(packet.channels, j) entry.append('%d' % value) elif runtime_settings.adc_mode == bt.RUNTIME_ADC_MODE_DIFFERENTIAL: for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_3 + 1): if (general_settings.channel_mask & (1 << j)): value = bt.int32_t_ptr_getitem(packet.channels, j) entry.append('%d' % value) recv_packets += 1 if log_file: try: log_file.writerow(entry) except csv.Error as e: raise NameError('Could not write into CSV writer object') return False elif err_code == bt.BUDDY_ERROR_CODE_GENERAL: print 'test_seq_adc: could not read ADC packet' print 'err_code = %d' % err_code return -1 else: print 'unknown error code, err_code = %d' % err_code return 0
def test_waveform_dac(handle, fw_info, sample_rate, wave_type, streaming, poncho_mode): mask = bt.BUDDY_CHAN_0_MASK | bt.BUDDY_CHAN_1_MASK | bt.BUDDY_CHAN_2_MASK | bt.BUDDY_CHAN_3_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_DAC_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE #general_settings.channel_mask = bt.BUDDY_CHAN_ALL_MASK general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.dac_power = bt.RUNTIME_DAC_POWER_ON runtime_settings.dac_ref = bt.RUNTIME_DAC_REF_EXT if poncho_mode: print('Poncho mode activated') general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_waveform_dac: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() test_seq_dac_count = 0 #y_mag = 255 #y_mag = 2700 y_mag = 4000 t = np.linspace(0, WAVEFORM_TIME, sample_rate * WAVEFORM_TIME, endpoint=False) if wave_type == 'square': y = ((scisig.square(np.pi * 2.0 * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag elif wave_type == 'sine': y = ((np.sin(np.pi * (1 / 2.0) * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag elif wave_type == 'sawtooth': y = ((scisig.sawtooth(np.pi * (1 / 2.0) * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag else: return -1 for k in y: for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, int(k)) print 'test_waveform_dac: sending %d packet with value %d' % (test_seq_dac_count, k) test_seq_dac_count += 1 if (bt.buddy_send_dac(handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): print 'test_waveform_dac: could not send DAC packet' return -1 if not streaming: time.sleep(1.0 / sample_rate) bt.buddy_flush(handle) time.sleep(0.1) return 0
def test_waveform_dac(handle, fw_info, sample_rate, wave_type, streaming, poncho_mode): mask = bt.BUDDY_CHAN_0_MASK | bt.BUDDY_CHAN_1_MASK | bt.BUDDY_CHAN_2_MASK | bt.BUDDY_CHAN_3_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() general_settings.function = bt.GENERAL_CTRL_DAC_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE #general_settings.channel_mask = bt.BUDDY_CHAN_ALL_MASK general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_HIGH timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.dac_power = bt.RUNTIME_DAC_POWER_ON runtime_settings.dac_ref = bt.RUNTIME_DAC_REF_EXT if poncho_mode: print('Poncho mode activated') general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_OUT general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_waveform_dac: could not configure Buddy device' return -1 time.sleep(0.1) packet = bt.general_packet_t() test_seq_dac_count = 0 #y_mag = 255 #y_mag = 2700 y_mag = 4000 t = np.linspace(0, WAVEFORM_TIME, sample_rate * WAVEFORM_TIME, endpoint=False) if wave_type == 'square': y = ((scisig.square(np.pi * 2.0 * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag elif wave_type == 'sine': y = ((np.sin(np.pi * (1 / 2.0) * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag elif wave_type == 'sawtooth': y = ((scisig.sawtooth(np.pi * (1 / 2.0) * WAVEFORM_FREQUENCY * t) + 1) / 2) * y_mag else: return -1 for k in y: for i in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_7 + 1): bt.int32_t_ptr_setitem(packet.channels, i, int(k)) print 'test_waveform_dac: sending %d packet with value %d' % ( test_seq_dac_count, k) test_seq_dac_count += 1 if (bt.buddy_send_dac(handle, packet, streaming) != bt.BUDDY_ERROR_CODE_OK): print 'test_waveform_dac: could not send DAC packet' return -1 if not streaming: time.sleep(1.0 / sample_rate) bt.buddy_flush(handle) time.sleep(0.1) return 0
def test_seq_counter(handle, sample_rate, streaming, log_file, poncho_mode): mask = bt.BUDDY_CHAN_0_MASK general_settings = bt.ctrl_general_t() timing_settings = bt.ctrl_timing_t() runtime_settings = bt.ctrl_runtime_t() packet = bt.general_packet_t() general_settings.function = bt.GENERAL_CTRL_COUNTER_ENABLE general_settings.mode = \ bt.MODE_CTRL_STREAM if streaming else bt.MODE_CTRL_IMMEDIATE general_settings.channel_mask = mask general_settings.resolution = bt.RESOLUTION_CTRL_SUPER timing_settings.period = bt.FREQUENCY_TO_NSEC(sample_rate) runtime_settings.counter_control = bt.RUNTIME_COUNTER_CONTROL_ACTIVE_LOW if poncho_mode: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_PONCHO general_settings.expander_mode = bt.BUDDY_EXPANDER_PONCHO_MODE_IN general_settings.expander_pin_state = mask else: general_settings.expander_type = bt.BUDDY_EXPANDER_TYPE_BASE # write CSV header if log_file: header = [] header.append('time') header.append('index') # counter operation can only occur on channel0 and channel1 for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_1 + 1): if (general_settings.channel_mask & (1 << j)): header.append('sensor %d' % j) try: log_file.writerow(header) except csv.Error as e: raise NameError('Could not write into CSV writer object') return False if (bt.buddy_configure(handle, general_settings, runtime_settings, timing_settings) != bt.BUDDY_ERROR_CODE_OK): print 'test_seq_counter: could not configure Buddy device' return -1 time.sleep(0.1) recv_packets = 0 first_packet = True for i in range(0, 1000): err_code = bt.buddy_read_counter(handle, packet, streaming) if err_code == bt.BUDDY_ERROR_CODE_OK: if first_packet: test_time_start = time.time() first_packet = False entry = [] entry.append('%.10f' % (time.time() - test_time_start)) entry.append('%d' % recv_packets) for j in range(bt.BUDDY_CHAN_0, bt.BUDDY_CHAN_1 + 1): if (general_settings.channel_mask & (1 << j)): value = bt.int32_t_ptr_getitem(packet.channels, j) entry.append('%d' % value) recv_packets += 1 if log_file: try: log_file.writerow(entry) except csv.Error as e: raise NameError('Could not write into CSV writer object') return False print 'entry = {}'.format(entry) elif err_code == bt.BUDDY_ERROR_CODE_GENERAL: print 'test_seq_counter: could not read counter packet' print 'err_code = %d' % err_code return -1 else: print 'unknown error code, err_code = %d' % err_code return 0