7-segment Display Using Swift Language
Introduction: 7-segment Display Using Swift Language
You could always see 7-segment displays around you used to display digits, like in timer, clock, etc. In this mission, you are going to learn how it works and display numbers on it. Let's start.
Step 1: What You Will Need
The parts you will need are all included in this Maker kit.
* SwiftIO board
* Shield
* 7-segment display
Step 2: 7-segment Display
Why is it called a 7-segment display? Yeah, it's because it contains 7 segments 🤣. They are A, B, C, D, E, F, G, which are actually 7 LEDs. You could control them separately.
The anodes of 7 LEDs are connected together. You could notice there's a pin for the power connection. If you want A to be illuminated, you need to set the pin to low voltage.
The way you set these LEDs decides how the display looks like. 1014
For example, to display the number 3, you will turn on A, B, C, D, G, and turn off E, F. Besides these numbers, you could display many other things, like characters.
Step 3: The Circuit
Place the shield on top of your SwiftIO board.
Connect the 7-segment display to pins reserved for it on the shield.
Step 4: The Code
The file SevenSegment.swift:
// Import the SwiftIO library to use everything in it.
import SwiftIO
// Import the board library to use the Id of the specific board.
import SwiftIOBoard
final class SevenSegment {
// Initialize the seven digital pins which are connected to the segment pins.
static let a = DigitalOut(Id.D8)
static let b = DigitalOut(Id.D7)
static let c = DigitalOut(Id.D6)
static let d = DigitalOut(Id.D5)
static let e = DigitalOut(Id.D4)
static let f = DigitalOut(Id.D2)
static let g = DigitalOut(Id.D3)
let leds = [a, b, c, d, e, f, g]
// Use a binary data to store the status of each segment for the number from 0 to 9.
// For each data, the last bit represents A and the second bit represents G.
let ledState: [UInt8] = [
0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110,
0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111
]
public func print(_ number: Int) {
let num = number % 10
let value = ledState[num]
// Get the value of each bit to determine whether the relevant segment is on or off.
for i in 0..<7{
let state = (value >> i) & 0x01
if state == 0 {
leds[i].write(true)
} else {
leds[i].write(false)
}
}
}
}
The file main.swift:
// Import the SwiftIO library to use everything in it.
import SwiftIO
// Import the board library to use the Id of the specific board.
import SwiftIOBoard
let number = 6
let sevenSeg = SevenSegment()
while true {
sevenSeg.print(number)
}
Step 5: Code Analysis
In this mission, you will not write all code in one file and separate it into two parts. This makes your code more clear and organized.
* The file SevenSegment.swift sets the 7-segment display and provides some easy-to-use functionalities.
* The file main.swift is to show the digit on the 7-segment display.
SevenSegment.swift
Let's look at this file in detail.
import SwiftIO
import SwiftIOBoard
As always, import the necessary libraries: SwiftIO and SwiftIOBoard. SwiftIO is used to control the input and output of the SwiftIO board. SwiftIOBoard defines the pin name of the board.
final class SevenSegment {
...
}
Then you will write the code in a class named SevenSegment. Briefly speaking, the class contains a block of code that allows you to create many instances with similar characteristics. It usually has methods (function in a class) and properties (constant or variable in a class).
The class normally could be inherited, and with the keyword final, it cannot be inherited any longer. 1014