示例#1
0
 def plugin_write(self,cmd): # handle incoming commands
  res = False
  cmdarr = cmd.split(",")
  cmdarr[0] = cmdarr[0].strip().lower()
  if cmdarr[0] == "mcpgpio":
   pin = -1
   val = -1
   try:
    pin = int(cmdarr[1].strip())
    ti2ca, trpin = lib_mcprouter.get_pin_address(pin)
    val = int(cmdarr[2].strip())
   except:
    pin = -1
    trpin = -1
   if pin>-1 and val in [0,1] and trpin >-1:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"MCPGPIO"+str(pin)+" set to "+str(val))
    try:
     tmcp = lib_mcprouter.request_mcp_device(int(self.i2cport),int(pin))
     tmcp.set_mode(trpin, 'output')
     tmcp.output(trpin, val)
     self.syncvalue(pin,val)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCPGPIO"+str(pin)+": "+str(e))
   return True
  elif cmdarr[0]=="mcppulse":
   pin = -1
   val = -1
   try:
    pin = int(cmdarr[1].strip())
    ti2ca, trpin = lib_mcprouter.get_pin_address(pin)
    val = int(cmdarr[2].strip())
   except:
    pin = -1
    trpin = -1
   dur = 100
   try:
    dur = float(cmdarr[3].strip())
   except:
    dur = 100
   if pin>-1 and val in [0,1] and trpin >-1:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"MCPGPIO"+str(pin)+": Pulse started")
    try:
     self.syncvalue(pin,val)
     tmcp = lib_mcprouter.request_mcp_device(int(self.i2cport),int(pin))
     tmcp.set_mode(trpin, 'output')
     tmcp.output(trpin, val)
     s = float(dur/1000)
     time.sleep(s)
     tmcp.output(trpin, (1-val))
     self.syncvalue(pin,(1-val))
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCPGPIO"+str(pin)+": "+str(e))
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"MCPGPIO"+str(pin)+": Pulse ended")
   return True
  return res
示例#2
0
文件: _P009_MCP.py 项目: yiit/rpieasy
 def p009_timercb(self, stimerid, ioarray):
     if ioarray[0] > -1:
         misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                     "EXTGPIO" + str(ioarray[0]) + ": LongPulse ended")
         try:
             tmcp = lib_mcprouter.request_mcp_device(
                 int(self.i2cport), int(ioarray[0]))
             tmcp.set_mode(int(ioarray[0]), 'output')
             tmcp.output(int(ioarray[0]), int(ioarray[1]))
         except Exception as e:
             misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                         "MCPGPIO" + str(ioarray[0]) + ": " + str(e))
示例#3
0
文件: _P009_MCP.py 项目: yiit/rpieasy
    def plugin_init(self, enableplugin=None):
        plugin.PluginProto.plugin_init(self, enableplugin)
        self.decimals[0] = 0
        self.initialized = False
        if self.enabled:
            i2cport = -1
            try:
                for i in range(0, 2):
                    if gpios.HWPorts.is_i2c_usable(
                            i) and gpios.HWPorts.is_i2c_enabled(i):
                        i2cport = i
                        break
            except:
                i2cport = -1
            if i2cport > -1:
                try:
                    pinnum = int(self.taskdevicepluginconfig[0])
                except:
                    pinnum = 0
                try:
                    self.i2ca, self.rpin = lib_mcprouter.get_pin_address(
                        pinnum)
                    self.mcp = lib_mcprouter.request_mcp_device(
                        int(i2cport), pinnum)
                except Exception as e:
                    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                                "MCP device requesting failed: " + str(e))
                    self.mcp = None
            if self.mcp and self.rpin > -1:
                try:
                    if int(self.taskdevicepluginconfig[1]) == 0:
                        pmode = "input"
                        ppull = "disable"
                    elif int(self.taskdevicepluginconfig[1]) == 1:
                        pmode = "input"
                        ppull = "enable"
                    elif int(self.taskdevicepluginconfig[1]) == 2:
                        pmode = "output"
                        ppull = "disable"
                    self.mcp.set_mode(self.rpin, pmode, ppull)
                    self.set_value(1, self.mcp.input(self.rpin), True)
                except Exception as e:
                    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                                "MCP pin configuration failed: " + str(e))
                    self.mcp = None
            if self.mcp is None:
                self.enabled = False
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "MCP can not be initialized! ")
            else:
                self.i2cport = int(i2cport)
                self.initialized = True
                try:
                    if str(self.taskdevicepin[0]).strip() != "0" and str(
                            self.taskdevicepin[0]).strip() != "":
                        self.mcp.setexternalint(0, int(self.taskdevicepin[0]))
                    if self.rpin > -1 and self.taskdevicepluginconfig[1] < 2:
                        self.mcp.add_interrupt(
                            self.rpin,
                            callbackFunctLow=self.p009_handler_low,
                            callbackFunctHigh=self.p009_handler_high)


#      print("add int",self.rpin)
                except Exception as e:
                    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                                "MCP interrupt configuration failed:" + str(e))
                try:
                    self.ports = str(self.taskdevicepluginconfig[0])
                except:
                    self.ports = 0
        else:
            self.ports = 0
            if self.rpin > -1 and int(self.taskdevicepluginconfig[1]) < 2:
                self.mcp.remove_interrupt(self.rpin)
示例#4
0
 def plugin_init(self,enableplugin=None):
  plugin.PluginProto.plugin_init(self,enableplugin)
  self.decimals[0] = 0
  self.initialized = False
  if self.enabled:
   try:
     i2cl = self.i2c
   except:
     i2cl = -1
   try:
    i2cport = gpios.HWPorts.geti2clist()
    if i2cl==-1:
      i2cl = int(i2cport[0])
   except:
    i2cport = []
   if len(i2cport)>0 and i2cl>-1:
     try:
      pinnum = int(self.taskdevicepluginconfig[0])
     except:
      pinnum = 0
     try:
      tnum = int(self.taskdevicepluginconfig[2])
     except:
      tnum = 0
     if tnum<1:
      ctype = "MCP23017"
     else:
      ctype = "MCP23008"
     try:
      self.i2ca, self.rpin = lib_mcprouter.get_pin_address(pinnum)
      self.mcp = lib_mcprouter.request_mcp_device(int(i2cl),pinnum,ctype)
     except Exception as e:
      misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCP device requesting failed: "+str(e))
      self.mcp = None
   if self.mcp and self.rpin>-1:
    try:
     if int(self.taskdevicepluginconfig[1])==0:
      pmode = "input"
      ppull = "disable"
     elif int(self.taskdevicepluginconfig[1])==1:
      pmode = "input"
      ppull = "enable"
     elif int(self.taskdevicepluginconfig[1])==2:
      pmode = "output"
      ppull = "disable"
     self.mcp.set_mode(self.rpin,pmode,ppull)
     self.set_value(1,self.mcp.input(self.rpin),True)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCP pin configuration failed: "+str(e))
     self.mcp = None
   if self.mcp is None:
    self.enabled = False
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCP can not be initialized! ")
   else:
    self.i2cport = int(i2cl)
    self.initialized = True
    if self.taskdevicepluginconfig[1] != 2:
     try:
      if self.mcp.externalintsetted>-1:
       self.taskdevicepin[0] = int(self.mcp.extinta)
      elif str(self.taskdevicepin[0]).strip() != "":
       self.mcp.setexternalint(0,int(self.taskdevicepin[0]),self.gettaskindex())
      if self.rpin>-1 and self.taskdevicepluginconfig[1]<2:
       self.mcp.add_interrupt(self.rpin,callbackFunctLow=self.p009_handler_low,callbackFunctHigh=self.p009_handler_high)
     except Exception as e:
      misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCP interrupt configuration failed:"+str(e))
    try:
     self.ports = str(self.taskdevicepluginconfig[0])
    except:
     self.ports = 0
  else:
   self.ports = 0
   if self.rpin>-1 and int(self.taskdevicepluginconfig[1])<2:
      self.mcp.remove_interrupt(self.rpin)