diff --git a/JPigpio/src/jpigpio/JPigpio.java b/JPigpio/src/jpigpio/JPigpio.java index 4ab7ec1..60a19cc 100644 --- a/JPigpio/src/jpigpio/JPigpio.java +++ b/JPigpio/src/jpigpio/JPigpio.java @@ -91,15 +91,54 @@ public interface JPigpio { /** - * Set the pulse width of a specific GPIO. The pulse width is in microseconds with a value between 500 and 2500 or a value of 0 to switch the servo off. - * + * Starts (500-2500) or stops (0) servo pulses on the GPIO. + * + * The selected pulsewidth will continue to be transmitted until + * changed by a subsequent call to set_servo_pulsewidth. + * + * The pulsewidths supported by servos varies and should probably + * be determined by experiment. A value of 1500 should always be + * safe and represents the mid-point of rotation. + * + * You can DAMAGE a servo if you command it to move beyond its + * limits. + * + * ... + * pi.set_servo_pulsewidth(17, 0) # off + * pi.set_servo_pulsewidth(17, 1000) # safe anti-clockwise + * pi.set_servo_pulsewidth(17, 1500) # centre + * pi.set_servo_pulsewidth(17, 2000) # safe clockwise + * ... * * @param gpio - * The pin to use to control the servo. + * GPIO to control the servo. 0-31 * @param pulseWidth * The pulse width of the pulse (500-2500). + * 0 = off + * 500 = most anti-clockwise + * 2500 = most clock-wise */ public void gpioServo(int gpio, int pulseWidth) throws PigpioException; + // setServoPulseWidth is just a wrapper for gpioServo for naming compatibility reasons + public void setServoPulseWidth(int gpio, int pulseWidth) throws PigpioException; + + /** + * Returns the servo pulsewidth being used on the GPIO. + * + * ... + * pi.set_servo_pulsewidth(4, 525) + * print(pi.get_servo_pulsewidth(4)) + * 525 + * + * pi.set_servo_pulsewidth(4, 2130) + * print(pi.get_servo_pulsewidth(4)) + * 2130 + * ... + * @param gpio user gpio 0-31 + * @return Returns the servo pulsewidth. + */ + public int getServoPulseWidth(int gpio) throws PigpioException;; + /** * Delay for the specified number of microseconds * @@ -734,6 +773,159 @@ public interface JPigpio { // ################ SERIAL + // ################ PWM + + /** + * Starts (non-zero dutycycle) or stops (0) PWM pulses on the GPIO. + * + * The [*setPWMRange*] function can change the default range of 255. + * + * ... + * pi.setPWMDutycycle(4, 0) # PWM off + * pi.setPWMDutycycle(4, 64) # PWM 1/4 on + * pi.setPWMDutycycle(4, 128) # PWM 1/2 on + * pi.setPWMDutycycle(4, 192) # PWM 3/4 on + * pi.setPWMDutycycle(4, 255) # PWM full on + * ... + * + * @param gpio user gpio 0-31 + * @param dutycycle 0-range (range defaults to 255). + * @throws PigpioException + */ + public void setPWMDutycycle(int gpio, int dutycycle) throws PigpioException; + + /** + * Returns the PWM dutycycle being used on the GPIO. + * + * For normal PWM the dutycycle will be out of the defined range + * for the GPIO (see [*getPWMRange*]). + * + * If a hardware clock is active on the GPIO the reported + * dutycycle will be 500000 (500k) out of 1000000 (1M). + * + * If hardware PWM is active on the GPIO the reported dutycycle + * will be out of a 1000000 (1M). + * + * ... + * pi.setPWMDutycycle(4, 25) + * print(pi.getPWMDutycycle(4)) + * 25 + * + * pi.setPWMDutycycle(4, 203) + * print(pi.getPWMDutycycle(4)) + * 203 + * ... + * @param gpio user gpio 0-31 + * @return PWM dutycycle + * @throws PigpioException + */ + public int getPWMDutycycle(int gpio) throws PigpioException; + + + /** + * Sets the range of PWM values to be used on the GPIO. + * + * ... + * pi.set_PWM_range(9, 100) # now 25 1/4, 50 1/2, 75 3/4 on + * pi.set_PWM_range(9, 500) # now 125 1/4, 250 1/2, 375 3/4 on + * pi.set_PWM_range(9, 3000) # now 750 1/4, 1500 1/2, 2250 3/4 on + * ... + * + * @param gpio user gpio 0-31 + * @param range range 25-40000 + * @throws PigpioException + */ + public void setPWMRange(int gpio, int range) throws PigpioException; + + /** + * Returns the range of PWM values being used on the GPIO. + * + * If a hardware clock or hardware PWM is active on the GPIO + * the reported range will be 1000000 (1M). + * + * ... + * pi.set_PWM_range(9, 500) + * print(pi.get_PWM_range(9)) + * 500 + * ... + * @param gpio user gpio 0-31 + * @return range of PWM values + * @throws PigpioException + */ + public int getPWMRange(int gpio) throws PigpioException; + + /** + * Returns the real (underlying) range of PWM values being + * used on the GPIO. + * + * If a hardware clock is active on the GPIO the reported + * real range will be 1000000 (1M). + * + * If hardware PWM is active on the GPIO the reported real range + * will be approximately 250M divided by the set PWM frequency. + * + * ... + * pi.setPWMFrequency(4, 800) + * System.out.println(pi.getPWMRealRange(4)) + * 250 + * ... + * @param gpio user gpio 0-31 + * @return the real (underlying) range of PWM values + * @throws PigpioException + */ + public int getPWMRealRange(int gpio) throws PigpioException; + + /** + * Sets the frequency (in Hz) of the PWM to be used on the GPIO. + * + * Returns the frequency actually set. + * + * ... + * pi.setPWMFrequency(4,0) + * print(pi.getPWMFrequency(4)) + * 10 + * + * pi.set_PWMFrequency(4,100000) + * print(pi.getPWMFrequency(4)) + * 8000 + * ... + * @param gpio user gpio 0-31 + * @param frequency >= 0 Hz + * @throws PigpioException + */ + public int setPWMFrequency(int gpio, int frequency) throws PigpioException; + + /** + * Returns the frequency of PWM being used on the GPIO. + * + * Returns the frequency (in Hz) used for the GPIO. + * + * For normal PWM the frequency will be that defined for the GPIO + * by [*setPWMFrequency*]. + * + * If a hardware clock is active on the GPIO the reported frequency + * will be that set by [*hardwareClock*]. + * + * If hardware PWM is active on the GPIO the reported frequency + * will be that set by [*hardwarePWM*]. + * + * ... + * pi.setPWMFrequency(4,0) + * print(pi.getPWMFrequency(4)) + * 10 + * + * pi.setPWMFrequency(4, 800) + * print(pi.getPWMFrequency(4)) + * 800 + * ... + * @param gpio user gpio 0-31 + * @return frequency (in Hz) used for the GPIO. + * @throws PigpioException + */ + public int getPWMFrequency(int gpio) throws PigpioException; + + // ################ + public void gpioSetAlertFunc(int gpio, Alert alert) throws PigpioException; public void setDebug(boolean flag) throws PigpioException; diff --git a/JPigpio/src/jpigpio/Pigpio.java b/JPigpio/src/jpigpio/Pigpio.java index 835c26d..d4901a9 100644 --- a/JPigpio/src/jpigpio/Pigpio.java +++ b/JPigpio/src/jpigpio/Pigpio.java @@ -161,6 +161,57 @@ public long getCurrentTick() throws PigpioException{ @Override public native void gpioServo(int gpio, int pulseWidth) throws PigpioException; + @Override + public void setServoPulseWidth(int gpio, int pulseWidth) throws PigpioException { + gpioServo(gpio, pulseWidth); + } + + @Override + public int getServoPulseWidth(int gpio) throws PigpioException{ + throw new NotImplementedException(); + } + + // ############### PWM + + @Override + public void setPWMDutycycle(int gpio, int dutycycle) throws PigpioException { + throw new NotImplementedException(); + }; + + @Override + public int getPWMDutycycle(int gpio) throws PigpioException { + throw new NotImplementedException(); + } + + @Override + public void setPWMRange(int gpio, int range) throws PigpioException { + throw new NotImplementedException(); + } + + @Override + public int getPWMRange(int gpio) throws PigpioException { + throw new NotImplementedException(); + } + + @Override + public int getPWMRealRange(int gpio) throws PigpioException { + throw new NotImplementedException(); + } + + @Override + public int setPWMFrequency(int gpio, int frequency) throws PigpioException { + throw new NotImplementedException(); + } + + @Override + public int getPWMFrequency(int gpio) throws PigpioException { + throw new NotImplementedException(); + } + + + // ############### + + @Override public native void gpioSetAlertFunc(int pin, Alert alert) throws PigpioException; diff --git a/JPigpio/src/jpigpio/PigpioSocket.java b/JPigpio/src/jpigpio/PigpioSocket.java index ba2474d..9490a92 100644 --- a/JPigpio/src/jpigpio/PigpioSocket.java +++ b/JPigpio/src/jpigpio/PigpioSocket.java @@ -35,9 +35,9 @@ public class PigpioSocket extends CommonPigpio { private final int CMD_READ = 3; //3 gpio 0 0 - private final int CMD_WRITE = 4; //4 gpio level 0 - - // private final int CMD_PWM = 5; //5 gpio dutycycle 0 - - // private final int CMD_PRS = 6; //6 gpio range 0 - - // private final int CMD_PFS = 7; //7 gpio frequency 0 - + private final int CMD_PWM = 5; //5 gpio dutycycle 0 - + private final int CMD_PRS = 6; //6 gpio range 0 - + private final int CMD_PFS = 7; //7 gpio frequency 0 - private final int CMD_SERVO = 8; //8 gpio pulsewidth 0 - private final int CMD_WDOG = 9; //9 gpio timeout 0 - @@ -56,9 +56,9 @@ public class PigpioSocket extends CommonPigpio { private final int CMD_NP = 20; //20 handle 0 0 - private final int CMD_NC = 21; //21 handle 0 0 - - // private final int CMD_PRG = 22; //22 gpio 0 0 - - // private final int CMD_PFG = 23; //23 gpio 0 0 - - // private final int CMD_PRRG = 24; //24 gpio 0 0 - + private final int CMD_PRG = 22; //22 gpio 0 0 - + private final int CMD_PFG = 23; //23 gpio 0 0 - + private final int CMD_PRRG = 24; //24 gpio 0 0 - // private final int CMD_HELP = 25; //25 N/A N/A N/A N/A // private final int CMD_PIGPV = 26; //26 0 0 0 - @@ -125,7 +125,8 @@ public class PigpioSocket extends CommonPigpio { // CMD_SERR 80 handle count 0 - // CMD_SERW 81 handle 0 X uint8_t data[X] // CMD_SERDA 82 handle 0 0 - - // CMD_GDC 83 gpio 0 0 - + + private final int CMD_GDC = 83; // 83 gpio 0 0 - // CMD_GPW 84 gpio 0 0 - // CMD_HC 85 gpio frequency 0 - // CMD_HP 86 gpio frequency 4 uint32_t dutycycle @@ -791,6 +792,14 @@ public void gpioServo(int gpio, int pulseWidth) throws PigpioException { } } // End of gpioServo + public void setServoPulseWidth(int gpio, int pulseWidth) throws PigpioException { + gpioServo(gpio, pulseWidth); + } + + public int getServoPulseWidth(int gpio) throws PigpioException { + throw new NotImplementedException(); + } + /** * * @param pin @@ -925,6 +934,111 @@ else if (rc < 0) return rc; } + // ######################## PWM + + // ############### PWM + + public void setPWMDutycycle(int gpio, int dutycycle) throws PigpioException { + try { + slCmd.sendCmd(CMD_PWM, gpio, dutycycle); + } catch (IOException e) { + throw new PigpioException("setPWMDutycycle failed",e); + } + + } + + + public int getPWMDutycycle(int gpio) throws PigpioException { + int rc = 0; + try { + rc = slCmd.sendCmd(CMD_GDC, gpio, 0); + if (rc < 0) { + throw new PigpioException(rc); + } + } catch (IOException e) { + throw new PigpioException("getPWMDutycycle failed",e); + } + + return rc; + + } + + + public void setPWMRange(int gpio, int range) throws PigpioException { + try { + slCmd.sendCmd(CMD_PRS, gpio, range); + } catch (IOException e) { + throw new PigpioException("setPWMRange failed",e); + } + + } + + + public int getPWMRange(int gpio) throws PigpioException { + int rc = 0; + try { + rc = slCmd.sendCmd(CMD_PRG, gpio, 0); + if (rc < 0) { + throw new PigpioException(rc); + } + } catch (IOException e) { + throw new PigpioException("getPWMRange failed",e); + } + + return rc; + } + + + public int getPWMRealRange(int gpio) throws PigpioException { + int rc = 0; + try { + rc = slCmd.sendCmd(CMD_PRRG, gpio, 0); + if (rc < 0) { + throw new PigpioException(rc); + } + } catch (IOException e) { + throw new PigpioException("getPWMRealRange failed",e); + } + + return rc; + } + + + public int setPWMFrequency(int gpio, int frequency) throws PigpioException { + + int rc = 0; + try { + rc = slCmd.sendCmd(CMD_PFS, gpio, frequency); + if (rc < 0) { + throw new PigpioException(rc); + } + } catch (IOException e) { + throw new PigpioException("setPWMFrequency failed",e); + } + + return rc; + + } + + + public int getPWMFrequency(int gpio) throws PigpioException { + int rc = 0; + try { + rc = slCmd.sendCmd(CMD_PFG, gpio, 0); + if (rc < 0) { + throw new PigpioException(rc); + } + } catch (IOException e) { + throw new PigpioException("getPWMFrequency failed",e); + } + + return rc; + } + + + // ######################## + + @Override public void setDebug(boolean flag) throws PigpioException { // TODO Auto-generated method stub