Apr-06-2018, 09:13 PM
I am a middle school technology teacher and I am creating tutorials for the Raspberry Pi. This code lights up a 7 segment display which lights up the numbers 1 - 9 then a - f and lastly a period. The code works great, but I don't understand what is happening with the math! I don't get how the writeOneByte function works. What is happening in
I can't create an extension exercise or help explain the code if I can't understand this. Even a link to an explanation would help. Thank you.
GPIO.output(11, val & (0x01 << 0))? What do the dats represent mathematically?
I can't create an extension exercise or help explain the code if I can't understand this. Even a link to an explanation would help. Thank you.
#!/usr/bin/env python import RPi.GPIO as GPIO import time pins = [11,12,13,15,16,18,22,7] dats = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80] def setup(): GPIO.setmode(GPIO.BOARD) for pin in pins: GPIO.setup(pin, GPIO.OUT) # Set pin mode as output GPIO.output(pin, GPIO.LOW) def writeOneByte(val): GPIO.output(11, val & (0x01 << 0)) GPIO.output(12, val & (0x01 << 1)) GPIO.output(13, val & (0x01 << 2)) GPIO.output(15, val & (0x01 << 3)) GPIO.output(16, val & (0x01 << 4)) GPIO.output(18, val & (0x01 << 5)) GPIO.output(22, val & (0x01 << 6)) GPIO.output(7, val & (0x01 << 7)) def loop(): while True: for dat in dats: writeOneByte(dat) time.sleep(0.5) def destroy(): for pin in pins: GPIO.output(pin, GPIO.LOW) GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy()