def openDevice(self, address): try: self.board = mcc152(address) except: return False else: return True
def initBoard(self): # Try to initialize the device try: self.board = mcc152(0) serial = self.board.serial() self.serial_number.set(serial) # set DIO states and values self.board.dio_reset() self.board.dio_config_write_port(DIOConfigItem.DIRECTION, 0xF0) self.board.dio_output_write_port(0x00) self.d_out_values = [0] * 4 self.d_in_values = [0] * 4 for index in range(4): self.d_in_values[index] = self.board.dio_input_read_bit(index + 4) # set analog output values self.board.a_out_write_all([self.ao_voltage, self.ao_voltage]) self.ready_led.set(1) self.device_open = True except: self.software_errors += 1 self.current_failures += 1
def ResetMcc152(): boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) board.dio_reset() return jsonify("MCC152 RESET")
def main(): """ This function is executed automatically when the module is run directly. """ options = OptionFlags.DEFAULT print('MCC 152 all channel analog output example.') print('Writes the specified voltages to the analog outputs.') print(" Methods demonstrated:") print(" mcc152.a_out_write_all") print(" mcc152.info") print() # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) run_loop = True error = False while run_loop and not error: # Get the values from the user. try: values = get_input_values() except ValueError: run_loop = False else: # Write the values. try: hat.a_out_write_all(values=values, options=options) except (HatError, ValueError): error = True
def open_device(self, address): """ Open selected device """ try: self.board = mcc152(address) except HatError: return False else: return True
def main(): """ This function is executed automatically when the module is run directly. """ print("MCC 152 digital output write example.") print("Sets all digital I/O channels to outputs then gets values from") print("the user and updates the outputs. The value can be specified") print("as decimal (0 - 255,) hexadecimal (0x0 - 0xFF,)") print("octal (0o0 - 0o377,) or binary (0b0 - 0b11111111.)") print(" Methods demonstrated:") print(" mcc152.dio_reset") print(" mcc152.dio_output_write_port") print(" mcc152.dio_config_write_port") print(" mcc152.info") print() # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). hat.dio_reset() # Set all channels as outputs. try: hat.dio_config_write_port(DIOConfigItem.DIRECTION, 0x00) except (HatError, ValueError): print("Could not configure the port as outputs.") sys.exit() run_loop = True error = False # Loop until the user terminates or we get a library error. while run_loop and not error: write_value = get_input() if write_value is None: run_loop = False else: try: hat.dio_output_write_port(write_value) except (HatError, ValueError): error = True if error: print("Error writing the outputs.") # Return the DIO to default settings if not error: hat.dio_reset()
def main(): """ This function is executed automatically when the module is run directly. """ print("MCC 152 digital output write example.") print("Sets all digital I/O channels to output then gets channel and") print("value input from the user and updates the output.") print(" Methods demonstrated:") print(" mcc152.dio_reset") print(" mcc152.dio_output_write_bit") print(" mcc152.dio_config_write_bit") print(" mcc152.info") print() # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). hat.dio_reset() # Set all channels as outputs. for channel in range(mcc152.info().NUM_DIO_CHANNELS): try: hat.dio_config_write_bit(channel, DIOConfigItem.DIRECTION, 0) except (HatError, ValueError): print("Could not configure the channel as output.") sys.exit() run_loop = True error = False # Loop until the user terminates or we get a library error. while run_loop and not error: channel, value = get_input() if channel is None: run_loop = False else: try: hat.dio_output_write_bit(channel, value) except (HatError, ValueError): error = True if error: print("Error writing the output.") # Return the DIO to default settings if not error: hat.dio_reset()
def main(): """ This function is executed automatically when the module is run directly. """ print("MCC 152 digital input read example.") print("Reads the inputs individually and displays their state.") print(" Methods demonstrated:") print(" mcc152.dio_reset") print(" mcc152.dio_input_read_bit") print(" mcc152.info") print() # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). hat.dio_reset() num_channels = hat.info().NUM_DIO_CHANNELS run_loop = True error = False # Loop until the user terminates or we get a library error. while run_loop and not error: # Read and display the individual channels. for channel in range(num_channels): try: value = hat.dio_input_read_bit(channel) except (HatError, ValueError): error = True break else: print("DIO{0}: {1}\t".format(channel, value), end="") if error: print("\nError reading the input.") else: # Wait for the user to enter a response message = "\nEnter Q to exit, anything else to read again: " if version_info.major > 2: response = input(message) else: response = raw_input(message) if response == "q" or response == "Q": # Exit the loop run_loop = False
def api_id(): if 'id' in request.args: id = int(request.args['id']) else: return "Error detected" #get MCC152 board address boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) bit = (0, 1, 2, 3, 4, 5, 6, 7, 8) board.dio_config_write_port(DIOConfigItem.DIRECTION, 0x00) board.dio_output_write_port(id) bit0 = str(board.dio_input_read_bit(0)) bit1 = str(board.dio_input_read_bit(1)) bit2 = str(board.dio_input_read_bit(2)) bit3 = str(board.dio_input_read_bit(3)) bit4 = str(board.dio_input_read_bit(4)) bit5 = str(board.dio_input_read_bit(5)) bit6 = str(board.dio_input_read_bit(6)) bit7 = str(board.dio_input_read_bit(7)) myDIO = [{ 'bit': 0, 'value': bit0, }, { 'bit': 1, 'value': bit1, }, { 'bit': 2, 'value': bit2, }, { 'bit': 3, 'value': bit3, }, { 'bit': 4, 'value': bit4, }, { 'bit': 5, 'value': bit5, }, { 'bit': 6, 'value': bit7, }, { 'bit': 7, 'value': bit7, }] return jsonify(myDIO)
def AnaOut0(): if 'id' in request.args: id = float(request.args['id']) else: return "Error detected" options = OptionFlags.DEFAULT boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) board.a_out_write(channel=0, value=id, options=options) return "SUCCESS"
def discover(cls): device_enumeration: list = [] board_list = hat_list(filter_by_id=HatIDs.ANY) board_num = 0 board_index = 0 if not board_list: return device_enumeration for device in board_list: board_num = device.address # ul.create_daq_device(board_num, device) board_index = board_index + 2 device_enumeration.append(MCCDAQHat(board_num, mcc152(board_num))) return device_enumeration
def main(): """ This function is executed automatically when the module is run directly. """ print("MCC 152 digital input read example.") print("Reads the inputs as a port and displays their state.") print(" Methods demonstrated:") print(" mcc152.dio_reset") print(" mcc152.dio_input_read_port") print() # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). hat.dio_reset() run_loop = True error = False # Loop until the user terminates or we get a library error. while run_loop and not error: # Read and display the inputs try: value = hat.dio_input_read_port() except (HatError, ValueError): error = True break else: print("Digital Inputs: 0x{0:02X}".format(value)) if error: print("Error reading the inputs.") else: print("Enter Q to exit, anything else to read again: ") # Wait for the user to enter a response if version_info.major > 2: response = input("") else: response = raw_input("") if response == "q" or response == "Q": # Exit the loop run_loop = False
def main(): """ Main function """ if KEY == "<my_key>": print("The default key must be changed to the user's personal IFTTT " "Webhooks key before using this example.") sys.exit() # Find the first MCC 152 mylist = hat_list(filter_by_id=HatIDs.MCC_152) if not mylist: print("No MCC 152 boards found") sys.exit() print("Using MCC 152 {}.".format(mylist[0].address)) board = mcc152(mylist[0].address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). board.dio_reset() # Read the initial input values so we don't trigger an interrupt when # we enable them. value = board.dio_input_read_bit(CHANNEL) # Enable latched input so we know that the value changed even if it changes # back to the original value before the interrupt callback. board.dio_config_write_bit(CHANNEL, DIOConfigItem.INPUT_LATCH, 1) # Unmask (enable) interrupts on the channel. board.dio_config_write_bit(CHANNEL, DIOConfigItem.INT_MASK, 0) print("Current input value is {}".format(value)) print("Waiting for changes, Ctrl-C to exit. ") while True: # wait for a change wait_for_interrupt(-1) # a change occurred, verify it was from this channel before sending # a trigger status = board.dio_int_status_read_bit(CHANNEL) if status == 1: # Read the input to clear the active interrupt. value = board.dio_input_read_bit(CHANNEL) send_trigger(EVENT_NAME, "{}".format(value)) print("Sent value {}.".format(value))
def main(): """ This function is executed automatically when the module is run directly. """ options = OptionFlags.DEFAULT channel = 0 num_channels = mcc152.info().NUM_AO_CHANNELS # Ensure channel is valid. if channel not in range(num_channels): error_message = ('Error: Invalid channel selection - must be ' '0 - {}'.format(num_channels - 1)) raise Exception(error_message) print('MCC 152 single channel analog output example.') print('Writes the specified voltage to the analog output.') print(" Methods demonstrated:") print(" mcc152.a_out_write") print(" mcc152.info") print(" Channel: {}\n".format(channel)) # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) hat = mcc152(address) run_loop = True error = False while run_loop and not error: # Get the value from the user. try: value = get_input_value() except ValueError: run_loop = False else: # Write the value to the selected channel. try: hat.a_out_write(channel=channel, value=value, options=options) except (HatError, ValueError): error = True if error: print("Error writing analog output.")
def BitStatus(): if 'id' in request.args: id = int(request.args['id']) else: return "error detected" boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) bitValue = board.dio_input_read_bit(6) #buttonStatus = [ # {'bitStatus': id, # 'bitValue': bitValue} # ] return str(bitValue)
def ButtonClear(): options = OptionFlags.DEFAULT if 'id' in request.args: id = int(request.args['id']) else: return jsonify("Error detected clearing bit 6") boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) board.dio_output_write_bit(6, 0) board.a_out_write(channel=1, value=0, options=options) buttonReset = [{ 'ButtonStatus': id, 'value': board.dio_input_read_bit(6), }] return jsonify(buttonReset)
def init_dev(self): address = select_hat_device(HatIDs.MCC_152) self.HAT = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). self.HAT.dio_reset() # Read the initial input values so we don't trigger an interrupt when # we enable them. self.HAT.dio_input_read_port() # set digital ouptput channels for ch in self.do_ch: try: self.HAT.dio_config_write_bit(ch, DIOConfigItem.DIRECTION, 0) # since default output register value is 1, # reset to 0 self.write_outputvalue(ch, 0) except (HatError, ValueError): logging.error( 'could not configure the channel{} as output'.format(ch)) sys.exit() # set digital iput channels as latched input for ch in self.di_ch: try: self.HAT.dio_config_write_bit(ch, DIOConfigItem.DIRECTION, 1) # Enable latched inputs so we know that a value changed even if it changes # back to the original value before the interrupt callback. self.HAT.dio_config_write_bit(ch, DIOConfigItem.INPUT_LATCH, 1) # interrupt enabled self.HAT.dio_config_write_bit(ch, DIOConfigItem.INT_MASK, 0) except (HatError, ValueError): logging.error( 'could not configure the channel{} as output'.format(ch)) sys.exit() self.callback = HatCallback(self.interrupt_callback)
import sys from daqhats import hat_list, HatIDs, mcc152 # get hat list of MCC daqhat boards board_list = hat_list(filter_by_id=HatIDs.ANY) if not board_list: print("No boards found") sys.exit() # Read and display every channel for entry in board_list: if entry.id == HatIDs.MCC_152: print("Board {}: MCC 152".format(entry.address)) board = mcc152(entry.address) for channel in range(board.info().NUM_AO_CHANNELS): value = board.a_in_read(channel) print("Ch {0}: {1:.3f}".format(channel, value))
print datetime.datetime.now() # Create the DMM instrument dmm = gpib.DMM() if len(sys.argv) > 2: # get board address board_num = int(sys.argv[1]) channel = int(sys.argv[2]) else: board_num = 0 channel = 0 # Create an instance of the board board = hats.mcc152(board_num) print "Serial " + board.serial() # Check the offset error at the top of the zero-scale error region board.a_out_write(channel, 0.010) time.sleep(0.1) dmm_reading = dmm.read_voltage(0) error = dmm_reading - 0.010 print("Offset error: {:6.3f} mV".format(error * 1000)) print # Modify these for the specific test num_points = 10 min_voltage = 0.0 max_voltage = 5.0 output_path = "accuracy_152_{0}_{1}.csv".format(board_num, channel)
def ButtonSet(): # setup button options = OptionFlags.DEFAULT if 'id' in request.args: id = int(request.args['id']) else: return "Error detected" #print("MCC 152 digital input interrupt example.") #print("Enables interrupts on the inputs and displays their state when") #print("they change.") #print(" Functions / Methods demonstrated:") #print(" mcc152.dio_reset") #print(" mcc152.dio_config_write_port") #print(" mcc152.dio_input_read_port") #print(" mcc152.dio_int_status_read_port") #print(" mcc152.info") #print(" interrupt_callback_enable") #print(" interrupt_callback_disable") #print() # Get an instance of the selected HAT device object. address = select_hat_device(HatIDs.MCC_152) #print("\nUsing address {}.\n".format(address)) global HAT # pylint: disable=global-statement HAT = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). HAT.dio_reset() # use bit 6 as status of button press and set to 0 HAT.dio_config_write_bit(6, DIOConfigItem.DIRECTION, 0) HAT.dio_output_write_bit(6, 0) # set up Analog out 1 to supply 5VDC to button HAT.a_out_write(channel=1, value=.5, options=options) # Read the initial input values so we don't trigger an interrupt when # we enable them. value = HAT.dio_input_read_port() # Enable latched inputs so we know that a value changed even if it changes # back to the original value before the interrupt callback. HAT.dio_config_write_port(DIOConfigItem.INPUT_LATCH, 0xFF) # Unmask (enable) interrupts on all channels. HAT.dio_config_write_port(DIOConfigItem.INT_MASK, 0x00) #print("Current input values are 0x{:02X}".format(value)) #print("Waiting for changes, enter any text to exit. ") # Create a HAT callback object for our function callback = HatCallback(interrupt_callback) # Enable the interrupt callback function. Provide a mutable value for # user_data that counts the number of interrupt occurrences. int_count = [0] interrupt_callback_enable(callback, int_count) # Wait for the user to enter anything, then exit. if version_info.major > 2: input("") else: raw_input("") # Return the digital I/O to default settings. HAT.dio_reset() # Disable the interrupt callback. interrupt_callback_disable() return jsonify("Button Complete")
def BitSet(): boardAddr = select_hat_device(HatIDs.MCC_152) board = mcc152(boardAddr) board.dio_output_write_bit(6, 1) return jsonify("Bit set")
def main(): """ This function is executed automatically when the module is run directly. """ print("MCC 152 digital input interrupt example.") print("Enables interrupts on the inputs and displays their state when") print("they change.") print(" Functions / Methods demonstrated:") print(" mcc152.dio_reset") print(" mcc152.dio_config_write_port") print(" mcc152.dio_input_read_port") print(" mcc152.dio_int_status_read_port") print(" mcc152.info") print(" interrupt_callback_enable") print(" interrupt_callback_disable") print() # Get an instance of the selected HAT device object. address = select_hat_device(HatIDs.MCC_152) print("\nUsing address {}.\n".format(address)) global HAT # pylint: disable=global-statement HAT = mcc152(address) # Reset the DIO to defaults (all channels input, pull-up resistors # enabled). HAT.dio_reset() # Read the initial input values so we don't trigger an interrupt when # we enable them. value = HAT.dio_input_read_port() # Enable latched inputs so we know that a value changed even if it changes # back to the original value before the interrupt callback. HAT.dio_config_write_port(DIOConfigItem.INPUT_LATCH, 0xFF) # Unmask (enable) interrupts on all channels. HAT.dio_config_write_port(DIOConfigItem.INT_MASK, 0x00) print("Current input values are 0x{:02X}".format(value)) print("Waiting for changes, enter any text to exit. ") # Create a HAT callback object for our function callback = HatCallback(interrupt_callback) # Enable the interrupt callback function. Provide a mutable value for # user_data that counts the number of interrupt occurrences. int_count = [0] interrupt_callback_enable(callback, int_count) # Wait for the user to enter anything, then exit. if version_info.major > 2: input("") else: raw_input("") # Return the digital I/O to default settings. HAT.dio_reset() # Disable the interrupt callback. interrupt_callback_disable()