Last active
August 29, 2015 14:22
-
-
Save shuhei/4a1e8e8c39e8c94597c9 to your computer and use it in GitHub Desktop.
7 segment LED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/stianeikeland/go-rpio" | |
"log" | |
"time" | |
) | |
func main() { | |
// TODO: Fix IO pins. Convert physical to gpio num. | |
// https://github.com/stianeikeland/go-rpio/blob/master/rpio.go#L28 | |
// http://www.instructables.com/id/Controlling-a-7-segment-4-digit-display-with-a-Ras/ | |
// https://www.sengoku.co.jp/mod/sgk_cart/detail.php?code=EEHD-4KZB | |
segments := []rpio.Pin{ | |
rpio.Pin(8), // physical 24, CE0 | |
rpio.Pin(18), // physical 12, GPIO18 | |
rpio.Pin(10), // physical 19, MOSI | |
rpio.Pin(9), // physical 21, MISO | |
rpio.Pin(11), // physical 23, SCLK | |
rpio.Pin(25), // physical 22, GPIO25 | |
rpio.Pin(22), // physical 15, GPIO22 | |
} | |
digits := []rpio.Pin{ | |
rpio.Pin(7), // physical 26, CE1 | |
rpio.Pin(24), // physical 18, GPIO24 | |
rpio.Pin(23), // physical 16, GPIO23 | |
rpio.Pin(27), // physical 13, GPIO21 ?? Not connected! Should be GPIO27? | |
} | |
// Clickwise from top + middle | |
// top, top right, bottom right, bottom, bottom left, top left, middle | |
num := map[rune][]int{ | |
' ': {0, 0, 0, 0, 0, 0, 0}, | |
'0': {1, 1, 1, 1, 1, 1, 0}, | |
'1': {0, 1, 1, 0, 0, 0, 0}, | |
'2': {1, 1, 0, 1, 1, 0, 1}, | |
'3': {1, 1, 1, 1, 0, 0, 1}, | |
'4': {0, 1, 1, 0, 0, 1, 1}, | |
'5': {1, 0, 1, 1, 0, 1, 1}, | |
'6': {1, 0, 1, 1, 1, 1, 1}, | |
'7': {1, 1, 1, 0, 0, 0, 0}, | |
'8': {1, 1, 1, 1, 1, 1, 1}, | |
'9': {1, 1, 1, 1, 0, 1, 1}, | |
} | |
dot := rpio.Pin(17) // physical 11, GPIO17 | |
dot.Output() | |
dot.Low() | |
err := rpio.Open() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// TODO: What is this PullDown for? | |
inPin := rpio.Pin(7) | |
inPin.Input() | |
inPin.PullDown() | |
for _, segment := range segments { | |
segment.Output() | |
segment.Low() | |
} | |
for _, digit := range digits { | |
digit.Output() | |
digit.High() | |
} | |
defer rpio.Close() | |
for { | |
// TODO: Get string representation of number. | |
s := "1234" | |
// TODO: Loop through `digits`. | |
// Draw each digit. | |
for digitPos, digit := range s { | |
for i, bit := range num[digit] { | |
if bit == 0 { | |
segments[i].Low() | |
} else { | |
segments[i].High() | |
} | |
} | |
if digitPos == 1 { | |
dot.High() | |
} else { | |
dot.Low() | |
} | |
digitPin := digits[digitPos] | |
digitPin.Low() | |
<-time.After(1 * time.Millisecond) | |
digitPin.High() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment