MyArduinoLight (Programming Arduino in Pure C from Xcode)

I got my first Arduino a month ago (an Uno) and have been playing with it every day since, including making a light-up hat from a Lilypad and putting together a Mintduino (breadboard Arduino clone) and a Prototino (soldered version).

In my day job I'm a film producer, but from 1992-1998, I was a computer game designer and programmer working in San Francisco. More recently I've done some hobby iPhone programming.

I love the Arduino but was not crazy about the IDE. I wanted to be able to write and compile programs from Xcode 4.3, and at the same time understand exactly how all the Arduino functions worked and re-build and link them individually. That allows me to keep my programs as small and fast as possible.

Building on the digitalWriteFast and digitalReadFast macros (and the ATmega328 data sheet and many online tutorials for AVR-LibC), I created a single C file called MyArduinoLight which contains all of the Arduino library functions I use. Functions are only linked in as needed, so a simple blinking LEDs is less than 200 bytes of code, as opposed to 1K with the Arudino's built-in overhead. The functions I have so far are: pinMode, digitalWrite, digitalRead (which are all single assembly-line macros whenever the pin is known at compile time); initTimers (for PWM), analogWrite, stopAnalogWrite; analogRead, readAVCC (to determine ADC's reference voltage); mills, micros, delay, delayMicroseconds (which just macro to AVR-LibC's _delay_ms and _delay_us if not using the Timer interrupts); EnableInt0, EnableInt1, DisableInt0, DisableInt1 (for external interrupts); shiftInMSB, shiftInLSB, shiftOutMSB, shiftOutLSB; serialBegin, serialWriteChar, serialReadChar, serialWriteString, serialWriteNumber. (I especially wanted a much smaller serial library that only compiled in for DEBUG builds.)

Although I know C++ and Objective-C very well and think object-oriented programming is great for large projects, I decided to stay away from C++ and the overhead that comes with it. For every function and macro I added, I would check and compare the assembly code the compiler generates in order to make sure the code was as small and as efficient as I could make it. When programming DOS and Apple II games in the 1980s we had to do the whole thing in assembly, so I also love low-level programming and seeing how small and streamlined I can make things.

I'm not sure any of this is of interest or use to anyone, and it's mostly a collection of other people's work just put together, but I'm eager to share the experience if anyone is looking to do something simliar.

Nice project!

Are you using Makefile and external tool-chain or do you rely directly on Xcode built-in compiler?

I'm also trying to use Xcode to develop the sketches for Arduino and chipKIT boards —with Wiring boards on the pipes.

Please refer to the Arduino on Xcode Project — Official Thread.

Main issue isn't Arduino or chipKIT or Wiring or dealing with multiple platforms, but Xcode.

Xcode template keywords are poorly documented, and Xcode inner structure is rather a secret.

So any hint to improve the template would be highly appreciated!

Among other open points: how to improve

  • the template sets the sketch as c++ code and not plain text, for code-sense
  • the template populates the Sources List for code-sense
  • instead of having the serial console in a separate Terminal window, integrate it in Xcode debug pane
  • ...

Best regards,

I setup each Xcode project as an External Build Project, so I can compile just by hitting the run button in Xcode, but really it is just executing make on the Makefile. So it is the same as typing make on the command-line, although at least Xcode is able to interpret any warnings and errors correctly. (i.e. Warnings are highlighted in yellow and errors in red in Xcode and its easy to jump between and fix any problems, rather than reading the line numbers from the command-line and then going jumping to the line in a text editor.)

I'm using the build tools that come with CrossPack-AVR and I just link each program with my version of the Arduino library: MyArduinoLight.c. (I just added Tone and PC change interrupts today.) So I can't compile .ino or .pde sketches directly -> at a minimum, I would have to add in an "int main()" function that calls setup() with a while (1) loop that calls loop(), just as the Arduino IDE automatically adds. (Of course, I don't bother making a setup or loop function for my own projects, but when I want to run someone's Arduino sketch, that's all I add.)

Hi Mark,

Thank you for the detailed answer.

I was just wondering you were using Xcode built-in compiler to address the AVR micro-controller...

It's a real pity Xcode is so closed and poorly documented.


Would you consider sharing the source code for MyArduinoLight, please?

Thanks, Eric