Я не могу получить прикрепленный код Javascript для правильного управления моим 7-сегментным драйвером больших цифр от Sparkfun. Однако почти идентичный код в Python прекрасно работает. Похоже, это должно быть как-то связано с тем, как библиотека GPIO (onoff) в Node управляет портами GPIO, потому что изменение настроек меняет поведение, но не успешно.
// GUIDE FOR RASPBERRY PI
// https://medium.com/@jonah.lefkoff/how-to-hook-up-the-sparkfun-7-segment-display-to-a-raspberry-pi-577591ba94b5
console.log("STARTING CLOCK")
// Ability to shut down
const child_process = require("child_process")
process.on('SIGINT', () => {
clock.unexport()
latch.unexport()
data.unexport()
})
// Load GPIO library onoff
let GPIO = require('onoff').Gpio
// Number of display digits that are physically wired up
const DIGITS = 1
// Set everything to LOW
let latch = new GPIO(17, 'low', 'rising', { activeLow: false })
let clock = new GPIO(27, 'low', 'rising', { activeLow: false })
let data = new GPIO(22, 'low', 'rising', { activeLow: false })
function postNumber(num, decimal) {
let a = 1 << 0
let b = 1 << 6
let c = 1 << 5
let d = 1 << 4
let e = 1 << 3
let f = 1 << 1
let g = 1 << 2
let dp = 1 << 7
let segments
switch (num) {
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = b | c | f | g; break;
case 5: segments = a | c | d | f | g; break;
case 6: segments = a | c | d | e | f | g; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case 'c': segments = d | e | g; break;
case '-': segments = g; break;
case ' ': segments = 0; break;
default: segments = 0; break;
}
//if (decimal) segments = segments | dp
console.log(num, segments.toString(2), segments)
for (let y = 0; y < 8; y++) {
clock.writeSync(GPIO.LOW)
data.writeSync(segments & 1 << (7 - y))
clock.writeSync(GPIO.HIGH)
}
}
function showNumber(val) {
let num = Math.abs(val) // remove any signs
for (let x = 1; x <= DIGITS; x++) {
let remainder = parseInt(num % 10) //the remainder is what needs to be posted to each digit
postNumber(remainder, false)
num /= 10
}
latch.writeSync(GPIO.LOW)
latch.writeSync(GPIO.HIGH)
}
for (let x = 0; x < 10; x++) {
showNumber(x)
child_process.execSync("sleep .5")
}
FWIW, работает следующий код Python:
#!/usr/bin/env python
#By Jonah
import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BOARD)
segmentLatch=11
segmentClock=13
segmentData=15
GPIO.setup(segmentClock,GPIO.OUT)
GPIO.setup(segmentData,GPIO.OUT)
GPIO.setup(segmentLatch,GPIO.OUT)
GPIO.output(segmentClock,GPIO.LOW)
GPIO.output(segmentData,GPIO.LOW)
GPIO.output(segmentLatch,GPIO.LOW)
number=0
DIGITS = 1
#Takes a number and displays 2 numbers. Display absolute value (no negatives)
#look here maybe bug between value+number
def showNumber(value):
number = abs(value) #Remove negative signs and any decimals
x=0
while(x < DIGITS):
remainder=number % 10
postNumber(remainder)
number /= 10
x += 1
#Latch the current segment data
GPIO.output(segmentLatch,GPIO.LOW)
GPIO.output(segmentLatch,GPIO.HIGH) #Register moves storage register on the rising edge of RCK
#Given a number, or - shifts it out to the display
def postNumber(number):
a=1<<0
b=1<<6
c=1<<5
d=1<<4
e=1<<3
f=1<<1
g=1<<2
dp=1<<7
if number == 1: segments = b | c
elif number == 2: segments = a | b | d | e | g
elif number == 3: segments = a | b | c | d | g
elif number == 4: segments = b | c | f | g
elif number == 5: segments = a | c | d | f | g
elif number == 6: segments = a | c | d | e | f | g
elif number == 7: segments = a | b | c
elif number == 8: segments = a | b | c | d | e | f | g
elif number == 9: segments = a | b | c | d | f | g
elif number == 0: segments = a | b | c | d | e | f
elif number == ' ': segments = 0
elif number == 'c': segments = g | e | d
elif number == '-': segments = g
else : segments = False
#if (segments != dp):
print (number, "{0:b}".format(segments), segments)
y=0
while(y<8):
GPIO.output(segmentClock,GPIO.LOW)
GPIO.output(segmentData,segments & 1 << (7-y))
GPIO.output(segmentClock,GPIO.HIGH)
y += 1
x=0
while(x < 10):
showNumber(x)
x += 1
sleep(0.2)
GPIO.cleanup()