• Great! In this case you should be able to do what you want in normal JavaScript, so actually delving into Espruino itself isn't needed - maybe that link should mention it?

    What you really need is: http://www.espruino.com/Writing+Modules

    The modules that use I2c (http://www.espruino.com/I2C#using-i2c) are a good start - and the code for them is usually here: https://github.com/espruino/EspruinoDocs­/tree/master/devices

    To get you started, just copy/paste this into the Web IDE:

    Modules.addCached("pca9685_servo",functi­on() {
    var C = {
    PCA9685_SUBADR1 : 0x2,
    PCA9685_SUBADR2 : 0x3,
    PCA9685_SUBADR3 : 0x4,
    PCA9685_MODE1 : 0x0,
    PCA9685_PRESCALE : 0xFE,
    LED0_ON_L : 0x6,
    LED0_ON_H : 0x7,
    LED0_OFF_L : 0x8,
    LED0_OFF_H : 0x9,
    ALLLED_ON_L : 0xFA,
    ALLLED_ON_H : 0xFB,
    ALLLED_OFF_L : 0xFC,
    ALLLED_OFF_H : 0xFD
    };
    function PWMServoDriver(i2c) {
      this.i2c = i2c;
      this.addr = 0x40;  
      this.reset(()=>{
        this.setPWMFreq(1000);
      });
    }
    PWMServoDriver.prototype.write = function(r,d) {
      this.i2c.writeTo(this.addr,r,d);
    };
    PWMServoDriver.prototype.read = function(r) {
      this.i2c.writeTo(this.addr,r);
      return this.i2c.readFrom(this.addr,1)[0];
    };
    PWMServoDriver.prototype.reset = function(callback) {
      this.write(C.PCA9685_MODE1, 0x80);
      if (callback) setTimeout(callback,10);
    };
    PWMServoDriver.prototype.setPWM = function(num,on,off) {
      this.i2c.writeTo(this.addr,
                       C.LED0_ON_L+4*num,
                       on, on>>8,
                       off,off>>8 );
    };
    PWMServoDriver.prototype.setPWMFreq = function(freq) {
     // FIXME
    };
    exports.connect = function(i2c) {
      return new PWMServoDriver(i2c);
    };
    });
    
    var i2c = new I2C(); // software I2C
    i2c.setup({sda:SDA_PIN, scl:SCL_PIN});
    var servo = require("pca9685_servo").connect(i2c);
    

    So you need to implement setPWMFreq, but the general outline of what you need should be there for the other stuff. I did setPWM because Espruino can do it a bit more tidily than Arduino can.

    Other thing to note is that servos don't run at 1000Hz (the default) so you have to use Adafruit's example: https://github.com/adafruit/Adafruit-PWM­-Servo-Driver-Library/blob/master/exampl­es/servo/servo.ino

    Part of me thinks it's worth just setting it to 60Hz by default and adding a 'setServo' function as well though. Most people are going to use this board/chip for servos anyway :)

About

Avatar for Gordon @Gordon started