Help converting C code (i2c-dev)

Ok. I’ll try it in the morning! Maybe - I could just return length, values. It would be a small change to the i2c-dev way of doing things, but wouldn’t require the memcpy.
This is exciting - a Julia way to do i2c. Thanks once again for your expertise!

I would like to give a huge thanks to @yuyichao for all of the help he’s given on this endeavor. Here is the results of this labor of love.

This is the output of a Julia program using a pure Julia version of i2c-dev.h (used for doing i2c communications on linux devices). No Python at all. The program is running with 32-bit Julia 0.6.0 on a NanoPi Duo (similar to a Raspberry Pi) with linux (Linux NODE-NANOPIDUO 4.11.2 #1 SMP Mon Jul 10 11:06:36 CST 2017 armv7l armv7l armv7l GNU/Linux). The i2c device is an MMA8451 accelerometer from Adafruit.

julia-user@NODE-NANOPIDUO:~/julia-0.6.0/bin$ ./julia mma8451_i2c-dev.jl
Opening device...
Got device_id...
Reset sensor...
set resolution...
data ready int1...
turn on orientation...
activate at max rate, low noise mode...
first standby...
set the data rate for the sensor....
deactivate....
starting to read data....
read_bytes: 6
raw: 2054
raw: 1228
raw: 7824
raw: 88
raw: 0
raw: 0
************************************************************
Position = 0
x: 2054
y: 1228
z: 7824
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 0
x: 9.835
y: 5.88
z: 37.464
*****************************************************
read_bytes: 6
raw: 3078
raw: 58604
raw: 2556
raw: 136
raw: 0
raw: 0
************************************************************
Position = 0
x: 3078
y: 58604
z: 2556
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 0
x: 14.739
y: 280.62
z: 12.239
*****************************************************
read_bytes: 6
raw: 1798
raw: 57652
raw: 62520
raw: 128
raw: 0
raw: 0
************************************************************
Position = 1
x: 1798
y: 57652
z: 62520
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 1
x: 8.61
y: 276.061
z: 299.371
*****************************************************
read_bytes: 6
raw: 64262
raw: 59608
raw: 60352
raw: 120
raw: 0
raw: 0
************************************************************
Position = 1
x: 64262
y: 59608
z: 60352
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 1
x: 307.712
y: 285.427
z: 288.99
*****************************************************
read_bytes: 6
raw: 1286
raw: 57892
raw: 2092
raw: 128
raw: 0
raw: 0
************************************************************
Position = 0
x: 1286
y: 57892
z: 2092
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 0
x: 6.158
y: 277.21
z: 10.017
*****************************************************
read_bytes: 6
raw: 518
raw: 58104
raw: 3392
raw: 136
raw: 0
raw: 0
************************************************************
Position = 0
x: 518
y: 58104
z: 3392
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 0
x: 2.48
y: 278.225
z: 16.242
*****************************************************
read_bytes: 6
raw: 63238
raw: 59188
raw: 60516
raw: 248
raw: 0
raw: 0
************************************************************
Position = 1
x: 63238
y: 59188
z: 60516
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 1
x: 302.809
y: 283.416
z: 289.775
*****************************************************
read_bytes: 6
raw: 61702
raw: 60456
raw: 60904
raw: 96
raw: 0
raw: 0
************************************************************
Position = 1
x: 61702
y: 60456
z: 60904
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 1
x: 295.454
y: 289.488
z: 291.633
*****************************************************
read_bytes: 6
raw: 57862
raw: 64040
raw: 784
raw: 164
raw: 0
raw: 0
************************************************************
Position = 7
x: 57862
y: 64040
z: 784
************************************************************
max_val: 8191
signed_max: 16384
*****************************************************
Position = 7
x: 277.067
y: 306.649
z: 3.754
*****************************************************

2 Likes

If you have time and possibility, I am sure a blog post, showing this and perhaps some of the stuff you have learned calling C-code from Julia, would be very well received.

3 Likes

OK. Let me spend the day cleaning up the code (and example) and doing some commenting (not that i2c-dev.h had much in the way of documentation).

1 Like

@kristoffer.carlsson - I’m waiting on a few items to come in through the mail, so I can test this code with more than one device. I want to say upfront - this is the first time I’ve done anything with i2c. So, I found someone’s python code that was using an MMA8451 accelerometer, stripped it out, and translated to Julia - to test against the Julia module I was working on. So, before I do a blog - I’d like to test what I have written against a few more devices. If you want to see the code (and other ideas I’m working on) - go to:

https://github.com/fapplin/Julia_On_Development_Boards

1 Like

If you have time and possibility, I am sure a blog post, showing this and perhaps some of the stuff you have learned calling C-code from Julia, would be very well received.

+1 plz! :smiley: I think at least a small something about how you ended up dealing with the C union would be really helpful! :slight_smile:
Thanks!