mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Add byte mode and mouse buttons to Pi node
This commit is contained in:
		@@ -288,3 +288,47 @@
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-template-name="rpi-mouse">
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-butt"><i class="fa fa-circle"></i> Button</label>
 | 
			
		||||
        <select type="text" id="node-input-butt" style="width: 250px;">
 | 
			
		||||
            <option value="1">left</option>
 | 
			
		||||
            <option value="2">right</option>
 | 
			
		||||
            <option value="4">middle</option>
 | 
			
		||||
            <option value="7">any</option>
 | 
			
		||||
         </select>
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
 | 
			
		||||
        <input type="text" id="node-input-name" placeholder="Name">
 | 
			
		||||
    </div>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-help-name="rpi-mouse">
 | 
			
		||||
    <p>Raspberry Pi mouse button node. Generates a <b>msg.payload</b> with
 | 
			
		||||
    either a 1 or 0 when the selected mouse button is pressed and released</p>
 | 
			
		||||
    <p>Also sets <b>msg.button</b> to the code value, 1 = left, 2 = right, 4 = middle,
 | 
			
		||||
    so you can work out which button or combination was pressed.</p>
 | 
			
		||||
    <p>And sets <b>msg.topic</b> to <i>pi/mouse</i>.</p>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/javascript">
 | 
			
		||||
    RED.nodes.registerType('rpi-mouse',{
 | 
			
		||||
        category: 'Raspberry Pi',
 | 
			
		||||
        color:"#c6dbef",
 | 
			
		||||
        defaults: {
 | 
			
		||||
            name: { value:"" },
 | 
			
		||||
            butt: { value:"1",required:true }
 | 
			
		||||
        },
 | 
			
		||||
        inputs:0,
 | 
			
		||||
        outputs:1,
 | 
			
		||||
        icon: "rpi.png",
 | 
			
		||||
        label: function() {
 | 
			
		||||
            return this.name||"Pi Mouse" ;
 | 
			
		||||
        },
 | 
			
		||||
        labelStyle: function() {
 | 
			
		||||
            return this.name?"node_label_italic":"";
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
</script>
 | 
			
		||||
 
 | 
			
		||||
@@ -38,6 +38,9 @@ module.exports = function(RED) {
 | 
			
		||||
        throw "Error : nrgpio must to be executable.";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // the magic to make python print stuff immediately
 | 
			
		||||
    process.env.PYTHONUNBUFFERED = 1;
 | 
			
		||||
 | 
			
		||||
    var pinsInUse = {};
 | 
			
		||||
    var pinTypes = {"out":"digital output", "tri":"input", "up":"input with pull up", "down":"input with pull down", "pwm":"PWM output"};
 | 
			
		||||
 | 
			
		||||
@@ -209,6 +212,48 @@ module.exports = function(RED) {
 | 
			
		||||
    });
 | 
			
		||||
    RED.nodes.registerType("rpi-gpio out",GPIOOutNode);
 | 
			
		||||
 | 
			
		||||
    function PiMouseNode(n) {
 | 
			
		||||
        RED.nodes.createNode(this,n);
 | 
			
		||||
        this.butt = n.butt || 7;
 | 
			
		||||
        var node = this;
 | 
			
		||||
 | 
			
		||||
        node.child = spawn(gpioCommand+".py", ["mouse",node.butt]);
 | 
			
		||||
        node.status({fill:"green",shape:"dot",text:"OK"});
 | 
			
		||||
 | 
			
		||||
        node.child.stdout.on('data', function (data) {
 | 
			
		||||
            data = Number(data);
 | 
			
		||||
            if (data === 0) { node.send({ topic:"pi/mouse", button:data, payload:0 }); }
 | 
			
		||||
            else { node.send({ topic:"pi/mouse", button:data, payload:1 }); }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        node.child.stderr.on('data', function (data) {
 | 
			
		||||
            if (RED.settings.verbose) { node.log("err: "+data+" :"); }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        node.child.on('close', function (code) {
 | 
			
		||||
            if (RED.settings.verbose) { node.log("ret: "+code+" :"); }
 | 
			
		||||
            node.child = null;
 | 
			
		||||
            node.running = false;
 | 
			
		||||
            node.status({fill:"red",shape:"circle",text:""});
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        node.child.on('error', function (err) {
 | 
			
		||||
            if (err.errno === "ENOENT") { node.warn('Command not found'); }
 | 
			
		||||
            else if (err.errno === "EACCES") { node.warn('Command not executable'); }
 | 
			
		||||
            else { node.log('error: ' + err); }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        node.on("close", function() {
 | 
			
		||||
            if (node.child != null) {
 | 
			
		||||
                node.child.kill('SIGINT');
 | 
			
		||||
                node.child = null;
 | 
			
		||||
            }
 | 
			
		||||
            node.status({fill:"red",shape:"circle",text:""});
 | 
			
		||||
            if (RED.settings.verbose) { node.log("end"); }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    RED.nodes.registerType("rpi-mouse",PiMouseNode);
 | 
			
		||||
 | 
			
		||||
    RED.httpAdmin.get('/rpi-gpio/:id',function(req,res) {
 | 
			
		||||
        res.send( JSON.stringify(pitype) );
 | 
			
		||||
    });
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										48
									
								
								nodes/core/hardware/nrgpio.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										48
									
								
								nodes/core/hardware/nrgpio.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@@ -1,3 +1,4 @@
 | 
			
		||||
#!/usr/bin/python
 | 
			
		||||
#
 | 
			
		||||
# Copyright 2014 IBM Corp.
 | 
			
		||||
#
 | 
			
		||||
@@ -43,7 +44,7 @@ if len(sys.argv) > 1:
 | 
			
		||||
            except Exception as ex:
 | 
			
		||||
                print "bad data: "+data
 | 
			
		||||
 | 
			
		||||
    if cmd == "buzz":
 | 
			
		||||
    elif cmd == "buzz":
 | 
			
		||||
        #print "Initialised pin "+str(pin)+" to Buzz"
 | 
			
		||||
        GPIO.setup(pin,GPIO.OUT)
 | 
			
		||||
        p = GPIO.PWM(pin, 100)
 | 
			
		||||
@@ -115,11 +116,56 @@ if len(sys.argv) > 1:
 | 
			
		||||
                GPIO.cleanup(pin)
 | 
			
		||||
                sys.exit(0)
 | 
			
		||||
 | 
			
		||||
    elif cmd == "byte":
 | 
			
		||||
        #print "Initialised BYTE mode - "+str(pin)+
 | 
			
		||||
        list = [7,11,13,12,15,16,18,22]
 | 
			
		||||
        GPIO.setup(list,GPIO.OUT)
 | 
			
		||||
 | 
			
		||||
        while True:
 | 
			
		||||
            try:
 | 
			
		||||
                data = raw_input()
 | 
			
		||||
                if data == "close":
 | 
			
		||||
                    GPIO.cleanup()
 | 
			
		||||
                    sys.exit(0)
 | 
			
		||||
                data = int(data)
 | 
			
		||||
            except EOFError:        # hopefully always caused by us sigint'ing the program
 | 
			
		||||
                GPIO.cleanup()
 | 
			
		||||
                sys.exit(0)
 | 
			
		||||
            except:
 | 
			
		||||
                data = 0
 | 
			
		||||
            for bit in range(8):
 | 
			
		||||
                if pin == 1:
 | 
			
		||||
                    mask = 1 << (7 - bit)
 | 
			
		||||
                else:
 | 
			
		||||
                    mask = 1 << bit
 | 
			
		||||
                GPIO.output(list[bit], data & mask)
 | 
			
		||||
 | 
			
		||||
    elif cmd == "rev":
 | 
			
		||||
        print GPIO.RPI_REVISION
 | 
			
		||||
 | 
			
		||||
    elif cmd == "ver":
 | 
			
		||||
        print GPIO.VERSION
 | 
			
		||||
 | 
			
		||||
    elif cmd == "mouse":  # catch mice button events
 | 
			
		||||
        file = open( "/dev/input/mice", "rb" )
 | 
			
		||||
        oldbutt = 0
 | 
			
		||||
 | 
			
		||||
        def getMouseEvent():
 | 
			
		||||
          global oldbutt
 | 
			
		||||
          global pin
 | 
			
		||||
          buf = file.read(3)
 | 
			
		||||
          pin = pin & 0x07
 | 
			
		||||
          button = ord( buf[0] ) & pin # mask out just the required button(s)
 | 
			
		||||
          if button != oldbutt:  # only send if changed
 | 
			
		||||
              oldbutt = button
 | 
			
		||||
              print button
 | 
			
		||||
 | 
			
		||||
        while True:
 | 
			
		||||
            try:
 | 
			
		||||
                getMouseEvent()
 | 
			
		||||
            except:
 | 
			
		||||
                file.close()
 | 
			
		||||
                sys.exit(0)
 | 
			
		||||
 | 
			
		||||
else:
 | 
			
		||||
    print "Bad parameters - {in|out|pwm} {pin} {value|up|down}"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user