func (p *PCA9685) SetPwm(channel int, onTime, offTime uint16) error {
log.Println("onTime ", onTime, " offTime ", offTime)
// Split the ints into 4 bytes
timeReg := byte(pwm0OnLowReg + (4 * channel))
onTimeLow := byte(onTime & 0xFF)
onTimeHigh := byte(onTime >> 8)
offTimeLow := byte(offTime & 0xFF)
offTimeHigh := byte(offTime >> 8)
//log.Println("onLow ", onTimeLow, " onHigh ", onTimeHigh, " offLow ", offTimeLow, " offHigh ", offTimeHigh)
if err := p.bus.WriteToReg(p.addr, timeReg, []byte{onTimeLow}); err != nil {
return err
}
if err := p.bus.WriteToReg(p.addr, timeReg+1, []byte{onTimeHigh}); err != nil {
return err
}
if err := p.bus.WriteToReg(p.addr, timeReg+2, []byte{offTimeLow}); err != nil {
return err
}
return p.bus.WriteToReg(p.addr, timeReg+3, []byte{offTimeHigh}) // NEED TO REMOVE RETURN at beginning and add something similar to end of function as we don't want to exit yet.
// ----------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------
// MOD TO CHECK PWM VALUE AFTER SENDING
bool f = 0; // flag to exit while loop -- UPDATE FOR GO
short r = 0; // track how many times we retry updating pin, if over 50 consecutive fails we stop trying (this should never happen, just a method to exit so we don't have an infinite loop)
while (f == 0) // check if pca9685 received the signal until we get a match -- UPDATE FOR GO
{
if (GetPwmLevel(channel) == pwmValue) {f = 1;} // check if PWM value just sent to PCA9685 matches what we just read -- UPDATE FOR GO
else { // PWM values didn't match so we try again
// std::cout << "TRY AGAIN - " << std::endl;
log.Println("PCA9685 Didn't Update ", channel) // log if you see fit
f = 0; // NEED TO UPDATE FOR GO
r++; // NEED TO UPDATE FOR GO
timeReg := byte(pwm0OnLowReg + (4 * channel))
onTimeLow := byte(onTime & 0xFF)
onTimeHigh := byte(onTime >> 8)
offTimeLow := byte(offTime & 0xFF)
offTimeHigh := byte(offTime >> 8)
//log.Println("onLow ", onTimeLow, " onHigh ", onTimeHigh, " offLow ", offTimeLow, " offHigh ", offTimeHigh)
if err := p.bus.WriteToReg(p.addr, timeReg, []byte{onTimeLow}); err != nil {
return err
}
if err := p.bus.WriteToReg(p.addr, timeReg+1, []byte{onTimeHigh}); err != nil {
return err
}
if err := p.bus.WriteToReg(p.addr, timeReg+2, []byte{offTimeLow}); err != nil {
return err
}
return p.bus.WriteToReg(p.addr, timeReg+3, []byte{offTimeHigh})
delay(50) // 50 millisecond delay -- UPDATE FOR GO
}
if (r > 48) break // exit loop after 50 tries if it can't update - could add an alert here or logging -- UPDATE FOR GO
}
return what_reef-pi_requires
}