每次运行这段代码,我都会得到不同的输出

2024-04-24 03:51:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这个代码来控制继电器,使用一个节点为红色的移位寄存器,但每次我运行它,我都会得到不同的输出。 我从GitHub上的一个人那里找到的代码是用python编写的,所以我尝试将其转换为javascript Python中的代码:

import RPi.GPIO as GPIO

HIGH = True
LOW = False
class Shifter(object):
    def __init__(self, data_pin=22, latch_pin=18, clock_pin=16, invert=False):
        self.data_pin = data_pin
        self.latch_pin = latch_pin
        self.clock_pin = clock_pin
        self.invert = invert
        GPIO.setup(self.latch_pin, GPIO.OUT) ## Setup GPIO Pin to OUT
        GPIO.setup(self.clock_pin, GPIO.OUT) ## Setup GPIO Pin to OUT
        GPIO.setup(self.data_pin, GPIO.OUT) ## Setup GPIO Pin to OUT

    def shift_out(self, *values):
        """
        Shifts out an arbitrary number of *values* which should be integers
        representing a binary values for the shift register.  For example::
            >>> s = ShiftRegister()
            >>> s.shift_out(0b00000001) # Set pin 0 HIGH (all other pins LOW)
            >>> s.shift_out(0b10000000) # Set pin 7 HIGH (all other pins LOW)
            >>> s.shift_out(0b11111111) # Set all pins to HIGH
        If using more than one shift register simply pass multiple values::
            >>> s.shift_out(0b11111111, 0b10101010)
        In the above example, 0b11111111 would go to the first shift register
        while 0b10101010 would go to the second.
        """
        bits = {"0": False, "1": True}
        if self.invert:
            bits = {"1": False, "0": True}
        GPIO.output(self.latch_pin, LOW)
        for val in reversed(values):
            for bit in '{0:08b}'.format(val):
                GPIO.output(self.clock_pin, LOW)
                GPIO.output(self.data_pin, bits[bit])
                GPIO.output(self.clock_pin, HIGH)
        GPIO.output(self.latch_pin, HIGH)

    def test(self):
        """
        Performs a test of the shift register by setting each pin HIGH for .25
        seconds then LOW for .25 seconds in sequence.
        """
        import time
        for i in range(8):
            self.shift_out(1 << i)
            time.sleep(0.25)
            self.shift_out(0)
            time.sleep(0.25)

    def all(self, state=LOW):
        """
        Sets all pins on the shift register to the given *state*.  Can be
        either HIGH or LOW (1 or 0, True or False).
        """
        if state:
            self.shift_out(0b11111111)
        else:
            self.shift_out(0)

if __name__=="__main__":
    print("Testing shift register connection...")
    print("Each pin should go HIGH and LOW in order until you press Ctrl-C.")
    GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
    s = Shifter()
    try:
        while True:
            s.test()
    except KeyboardInterrupt:
        print("Ctrl-C detected.  Quitting...")

我把轮班的部分做出来,让它变成这样

var q = "00000010";
msg.payload;

var sNumber = q.toString();
msg.payload = 0;
node.send([null, msg, null]);

for (var i = 8; i > 0; i--) 
{

    msg.payload = 0;
    node.send([null, null, msg]);

    var integer = q.charAt(i);
    msg.payload = parseInt(integer);
    node.send([msg, null, null]);

    msg.payload = 1;
    node.send([null, null, msg]);
}
msg.payload = 1;
node.send([null, msg, null]);

节点红色的流程为:

  [{"id":"73aad823.d24248","type":"tab","label":"Home","disabled":false,"info":""},{"id":"3892b935.9f3446","type":"rpi-gpio out","z":"73aad823.d24248","name":"","pin":"11","set":"","level":"0","freq":"","out":"out","x":740,"y":140,"wires":[]},{"id":"2a4cb23e.3901fe","type":"rpi-gpio out","z":"73aad823.d24248","name":"","pin":"12","set":"","level":"0","freq":"","out":"out","x":740,"y":200,"wires":[]},{"id":"eca94bfd.01e9d8","type":"rpi-gpio out","z":"73aad823.d24248","name":"","pin":"13","set":"","level":"0","freq":"","out":"out","x":740,"y":260,"wires":[]},{"id":"4b1dfb9c.513e24","type":"function","z":"73aad823.d24248","name":"","func":"var q = \"00000010\";\n//msg.payload;\n\nvar sNumber = q.toString();\nmsg.payload = 0;\nnode.send([null, msg, null]);\n\nfor (var i = 8; i > 0; i--) \n{\n\n\tmsg.payload = 0;\n\tnode.send([null, null, msg]);\n\t\n\tvar integer = q.charAt(i);\n\tmsg.payload = parseInt(integer);\n\tnode.send([msg, null, null]);\n\t\n\tmsg.payload = 1;\n\tnode.send([null, null, msg]);\n}\nmsg.payload = 1;\nnode.send([null, msg, null]);","outputs":3,"noerr":0,"x":530,"y":220,"wires":[["3892b935.9f3446"],["2a4cb23e.3901fe"],["eca94bfd.01e9d8"]]},{"id":"6b3208fe.e3b1c8","type":"inject","z":"73aad823.d24248","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":220,"wires":[["4b1dfb9c.513e24"]]}]

Tags: thetoselfsendforgpioshiftpin