Add backoff to reduce CPU usage (fixes #141) (#151)

* Add backoff to reduce CPU usage

* Update driver version for CPU fix
This commit is contained in:
Terence Boldt 2023-10-31 23:04:15 -04:00 committed by GitHub
parent 0dbb55fda1
commit 8d5aa26b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 5 deletions

View File

@ -160,11 +160,18 @@ func (a2 A2Gpio) ReadByte() (byte, error) {
// wait for the Apple II to write
startTime := time.Now()
lastSleepTime := time.Now()
sleepDuration := 10
for inWrite.Read() == 1 {
if time.Since(startTime) > edgeTimeout {
outRead.High()
return 0, errors.New("timed out reading byte -- write stuck high")
}
if time.Since(lastSleepTime) > edgeTimeout/10 {
sleepDuration *= 3;
time.Sleep(time.Millisecond * time.Duration(sleepDuration));
lastSleepTime = time.Now()
}
}
// get a nibble of data
@ -211,10 +218,17 @@ func (a2 A2Gpio) ReadByte() (byte, error) {
// wait for the Apple II to finish writing
//fmt.Printf("wait for the Apple II to finish writing\n")
startTime = time.Now()
lastSleepTime = time.Now()
sleepDuration = 10
for inWrite.Read() == 0 {
if time.Since(startTime) > edgeTimeout {
return 0, errors.New("timed out reading byte -- write stuck low")
}
if time.Since(lastSleepTime) > edgeTimeout/10 {
sleepDuration *= 3;
time.Sleep(time.Millisecond * time.Duration(sleepDuration));
lastSleepTime = time.Now()
}
}
return data, nil
@ -230,11 +244,18 @@ func (a2 A2Gpio) WriteByte(data byte) error {
// wait for the Apple II to be ready to read
startTime := time.Now()
lastSleepTime := time.Now()
sleepDuration := 10
for inRead.Read() == 1 {
if time.Since(startTime) > edgeTimeout {
outWrite.High()
return errors.New("timed out writing byte -- read stuck high")
}
if time.Since(lastSleepTime) > edgeTimeout/10 {
sleepDuration *= 3;
time.Sleep(time.Millisecond * time.Duration(sleepDuration));
lastSleepTime = time.Now()
}
}
if ((data & 128) >> 7) == 1 {
@ -291,11 +312,18 @@ func (a2 A2Gpio) WriteByte(data byte) error {
// wait for the Apple II to finsih reading
//fmt.Printf("wait for the Apple II to finsih reading\n")
startTime = time.Now()
lastSleepTime = time.Now()
sleepDuration = 10
for inRead.Read() == 0 {
if time.Since(startTime) > edgeTimeout {
outWrite.High()
return errors.New("timed out writing byte -- read stuck low")
}
if time.Since(lastSleepTime) > edgeTimeout/10 {
sleepDuration *= 3;
time.Sleep(time.Millisecond * time.Duration(sleepDuration));
lastSleepTime = time.Now()
}
}
// let the Apple II know we are done writing

View File

@ -49,15 +49,12 @@ func main() {
handlers.SetCommunication(comm)
comm.Init()
lastCommandTime := time.Now()
// In case Apple II is waiting, send 0 byte to start
comm.WriteByte(0)
for {
command, err := comm.ReadByte()
if err == nil {
lastCommandTime = time.Now()
switch command {
case resetCommand:
handlers.ResetCommand()
@ -77,7 +74,7 @@ func main() {
handlers.ShellCommand()
}
// temporary workaround for busy wait loop heating up the RPi
} else if time.Since(lastCommandTime) > time.Millisecond*100 {
} else {
time.Sleep(time.Millisecond * 100)
}
}

View File

@ -8,4 +8,4 @@ package info
// Version is the hexadecimal version number that
// should be incremented with each driver update
const Version = "0029"
const Version = "002A"