Has anyone used I2C with a language other than Python?

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
Post Reply
diggy_dude
Posts: 10
Joined: Sat Oct 01, 2016 4:41 pm

Has anyone used I2C with a language other than Python?

Post by diggy_dude »

Hi, folks! I'm totally loving DietPi and wish to use my Raspberry Pi for hardware projects. I'm a PHP user, not too crazy about Python.

I know how to connect to an Arduino serial port by configuring the Arduino as a modem in PHP. I can also use my TRS-80 Model 100 laptop as a terminal for a PHP socket server, linked via FTDI and null modem cables through a serial adapter / socket client, also written in PHP. I wasn't able to get the Model 100 working as a regular terminal for ssh logins, as some people have done using Raspbian, but that's not important as the PHP proxy works well for my purposes. My idea is to use a socket server to control I2C devices. I've done this with an Arduino connected to the Pi, and it works well. However, I'd like to eliminate the Arduino and use the Pi's I2C interfaces.

My only experience with I2C is on an Arduino. Not sure where to start with using the I2C interface in PHP on a Pi. All of the tutorials I've found use the Python libraries. It would be nice to be able to use I2C with any language. Any help would be appreciated. Thanks in advance!
User avatar
Fourdee
Site Admin
Posts: 2782
Joined: Tue Feb 06, 2007 1:36 pm

Re: Has anyone used I2C with a language other than Python?

Post by Fourdee »

diggy_dude wrote: not too crazy about Python.
Your not the only one ;)
No idea why, but I really dont like the layout and syntax doesn't feel right in Python. It feels like i'am doing something naughty, everything looks so "naked" and "wrong" lol.

As for I2C, no idea , although if your after a GPIO library, Wiring Pi is C and supports C++ (yay! :D): http://dietpi.com/phpbb/viewtopic.php?f ... 1066#p1066
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
diggy_dude
Posts: 10
Joined: Sat Oct 01, 2016 4:41 pm

Re: Has anyone used I2C with a language other than Python?

Post by diggy_dude »

Thanks for the reply, Fourdee.

After further research, I concluded that executing i2cset and i2cget as shell commands is probably about as good as it gets for PHP. I found this script by Viacheslav Baczynski on the Web.

https://github.com/VBaczynski/phpi2c/bl ... phpi2c.php

While Mr. Baczynski's script suits 90% of my needs, the main problem with the original script is that the bus address and block ID are global variables, meaning you can operate only one device per script. Repackaging the script as a class will allow me to instantiate an arbitrary number of I2C devices. This hasn't been tested yet, but, assuming the original script works, there's no reason this version won't.

Code: Select all

<?php

  /*

     Object-oriented version of Viacheslav Baczynski's original script at
     https://github.com/VBaczynski/phpi2c/blob/master/phpi2c.php

  */

  class I2cDevice
  {

    const TYPE_BYTE   = 0;
    const TYPE_SHORT  = 1;
    const TYPE_USHORT = 2;
    const TYPE_ULONG  = 3;

    protected

      $blockId,
      $busAddr;

    public function __construct($params)
    {
      $this->blockId = $params->blockId;
      $this->busAddr = $params->busAddr;
    } // __construct

    public function read($register, $type = null)
    {
      switch ($type) {
        case self::TYPE_BYTE:
        default:
          return trim(shell_exec('i2cget -y ' . $this->blockId . ' ' . $this->busAddr . ' ' . $register . ' b'));
        case self::TYPE_SHORT:
          $msb = intval($this->read($register++), 16);
          $lsb = intval($this->read($register), 16);
          $val = ($msb << 8) | $lsb;
          $arr = unpack('s', pack('n', $val));
          $dec = $arr[1];
          return $dec;
        case self::TYPE_USHORT:
          $msb = intval($this->read($register++), 16);
          $lsb = intval($this->read($register), 16);
          $val = ($msb << 8) | $lsb;
          $arr = unpack('S', pack('n', $val));
          $dec = $arr[1];
          return $dec;
        case self::TYPE_ULONG:
          $msb  = intval($this->read($register++), 16);
          $lsb  = intval($this->read($register++), 16);
          $xlsb = intval($this->read($register), 16);
          $val  = ($msb << 16) | ($lsb << 8) | $xlsb;
          $arr  = unpack('l', pack('N', $val));
          $dec  = $arr[1];
          return $dec;
      }
    } // read

    public function write($register, $value, $type = null)
    {
      switch ($type) {
        case self::TYPE_BYTE:
        default:
          shell_exec('i2cset -y ' . $this->blockId . ' ' . $this->busAddr . ' ' . $register . ' ' . $value . ' b');
          return;
        case self::TYPE_SHORT:
          $value = $value & 0xFF;
          $this->write( $register, $value);
          return;
      }
    } // write

  } // I2cDevice

?>
Post Reply