clearwater / SwitecX25

Arduino library for Switec X25.168 and friends
http://clearwater.github.com/gaugette/
Other
128 stars 44 forks source link

Port to Particle (particle.io) platform. #5

Closed timkettering closed 9 years ago

timkettering commented 9 years ago

I had an existing X27 gauge project that originally used Ardiuno, which I wanted to replace with a Particle Photon.

So I forked your library (https://github.com/timkettering/SwitecX25) and after changing the Arduino imports, the code compiled fine. But the needle movement did not work as expected. It took me the better part of the day but I tracked down the issue. It seems that in SwitecX25.h, that the dir variable is defined as char. This works fine on Ardiuno, but on Particle platform, when attempting to cast to int for comparison purposes, it does not work as expected.

Changing the type of dir to int resolved all issues. I have committed changes and updated the wording for my fork and will maintain it as a Particle specific port of this library with all attribution to you as original developer.

Cheers! -tim

timkettering commented 9 years ago

Please do let me know if you have questions or any concerns. :)

guyc commented 9 years ago

Great to hear that you've ported to support Particle Photon, and it looks pretty straight forward. I haven't had a chance to play with the Photon yet. So many great platforms to choose from now.

Interesting issue you found... Just had a skim through the code looking at the use of dir, and it isn't obvious to me why dir shouldn't be a char type; it's not even used in any arithmetic operations.

timkettering commented 9 years ago

I'm not certain why char is an issue. I had to bring in a friend of mine who's more familiar with this lower level stuff than I am, and he says he suspects it is the cast of -1 that is the issue.

Here's a line of code he wrote to test his theory.

char foo = -1; Serial.println((int)foo); // what is output?

output: 255.

He thinks that it is architecture dependent. On arduino, that may cast correctly to -1, but on Photon's ARM, it ends up as 255.

And in the X25 library, you are doing comparisons to determine whether the motor needs to move forward or back, and this seems to align with the issues I encountered at first.. needle would only move forward, but never back. (With the exception of the zero() function call which worked fine, because it doesn't use the acceleration functions.)

Cheers, -tim

guyc commented 9 years ago

Ahh of course, that's the root of it. It appears that char is unsigned on the compiler for Particle Photon.
It is signed on Arduino: https://www.arduino.cc/en/Reference/Char