Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Cubietruck, I2C Python

Featured Replies

Hy,
I use Cubietruck / Armbian
I have a problem with I2C connecting to an Arduino.
my Python program returns me an error when I use the SMBus module :

read_byte_data the function (address, reg) bug while writing functions work well.

here is my Arduino code :

#include <Wire.h>
#include <LiquidCrystal.h>
#include <string.h>
#include <ctype.h>

const int RS = 12;
const int E = 11;
const int D4 = 5;
const int D5 = 4;
const int D6 = 3;
const int D7 = 2;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);



#define SLAVE_ADDRESS 0x12


byte regs[3];
int regIndex=0;
byte lastExecReq = 0x00;
char message[20];

void setup (void){
  regs[0] = 0x00;
  regs[1] = 0x00;
  regs[2] = 0x00;
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
  lcd.begin(20,4);
  lcd.print ("LCD OK");
  sprintf(message,"Bus I2C 0x%02x Ready",SLAVE_ADDRESS);
  Serial.println(message);
  lcd.setCursor(0,1);
  lcd.print(message);
}

void loop(void){
  if (regs[0]==0x00){
    delay(100);
    return;
  }
  switch(regs[0]){
    case 0x01 : break;
    case 0x02 : break;
    case 0x03 : 
      if (regs[1]>0){
        lcd.setCursor(0,2);
        lcd.print("Start ...   ");
      }else{
        lcd.setCursor(0,2);
        lcd.print("Stop ...   ");
      }
      break;
  }
}
void receiveEvent(int byteCount){
  int byteCounter = 0;
  while (byteCounter<byteCount){
    byte b = Wire.read();
    byteCounter +=1;
    if (byteCounter == 1){
      regIndex = b;
    }
    else{
      switch(regIndex){
        case 0 :
          regs[0] = b;
          lastExecReq = b;
          break;
        case 1:
          regs[1]=b;
          break;
        case 2:
          regs[2]=b;
          break;
      }
    }
  }
}

void requestEvent(){
  switch(regIndex){
    case 0x00:
      switch (lastExecReq){
        case 0x01:
          Wire.write(0x03);
          break;
        case 0x02:
          Wire.write(regs[1]+regs[2]);
          break;
        default : Wire.write(0xFF);
      }
      break;
    default: Wire.write(0xFF);
  }
}

and here Python

#!/usr/bin/python
import re
import smbus
import time

address = 0x12

class Bus_I2C(object):
  def __init__(self,address,busnum=1,debug=False):
    self.address = address
    self.bus = smbus.SMBus(busnum)
    self.debug = debug
  def errMsg(self):
    print("access error 0x%02X: check your I2C address")%self.address
    return -1
  def write8(self,reg,value):
    try:
      self.bus.write_byte_data(self.address,reg,value)
      if self.debug :
        print("I2C : write 0x%02X in the reg 0x%02X")%(value,reg)
    except IOError,err:
        return self.errMsg()
  def writeList(self,reg,list):
    try:
      if self.debug:
        print("I2C : write a list in the reg 0x%02X:")%reg
        print(list)
      self.bus.write_i2c_block_data(self.address,reg,list)
    except IOError,err:
      return self.errMsg()
  def readU8(self,reg):
    try:
      result=self.bus.read_byte_data(self.address,reg)
      #result=self.bus.read_byte(self.address,reg)
      if self.debug:
        print("I2C : device 0x%02X returned 0x%02X from reg 0x%02X")%(self.address,result,reg)
      return result
    except IOError,err:
      return self.errMsg()

i2c = Bus_I2C(address,1,True)

print("///////////// 0x01 in reg 0")
i2c.write8(0,0x01)
time.sleep(0.200)
print("///////////// read reg ...")
print(i2c.readU8(0x00))
time.sleep(0.100)
print("///////////// start LCD ...")
i2c.write8(0x01,1)
time.sleep(0.02)
i2c.write8(0x00,0x03)
time.sleep(0.100)
time.sleep(5)
print("///////////// stop LCD ...")
i2c.write8(0x01,0)
time.sleep(0.02)
i2c.write8(0x00,0x03)
time.sleep(0.100)

where is the bug ?

 

thank for your help

 

geka

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.