Python Forum
Need help understanding the math in the code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help understanding the math in the code
#1
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
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()
Reply


Messages In This Thread
Need help understanding the math in the code - by wansu - Apr-06-2018, 09:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code understanding: Need help in understanding dictionary code jt123 0 541 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 2,030 May-09-2023, 08:47 AM
Last Post: buran
  math.log versus math.log10 stevendaprano 10 2,648 May-23-2022, 08:59 PM
Last Post: jefsummers
  Understanding a piece of code Michael1 4 1,551 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,978 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  "SyntaxError: invalid syntax" running code in Doing Math With Python b saucerdesigner 2 2,830 Nov-03-2020, 04:23 PM
Last Post: saucerdesigner
  Beginner: I need help understanding few lines of a code. hop_090 1 1,765 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,502 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Help Understanding Code Variables 1 1,992 May-02-2019, 05:53 PM
Last Post: micseydel
  Need help understanding simple Array code. Please. stluwa 1 2,303 Apr-13-2019, 07:16 PM
Last Post: loomski

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020