Friday, May 29, 2020

A DigitalIO class to ease Arduino I/O programming #Arduino #Education

A new library to efficiently perform digital I/O on AVR microcontrollers: DigitalIO by dntruong.

Find the DigitalIO library on GitHub, along with its documentation, which is  meant to support the multitude of sensors that report their state with a LOW/HIGH signal: push-buttons, knock sensors, tilt switch, mercury switch, hall sensors, reed switches, rotary encoders, ultrasonic sensors…

The Arduino API provides easy methods (digitalRead() or digitalWrite()) to access I/O pins so why bother you think? Well…

  • These are slow functions, and worse, waste program memory on AVR boards. The DigitalIO library abstracts their use and allows me to replace them with AVR primitives. One of the main issues if you dig inside, is they take a pin ID and convert it to a port/bit using a table, stored in PROGMEM, which means even when you do static references (most of the time), gcc can’t optimize away all the extra code and data, and makes you access slow memory (I tried). Note the DigitalIO class uses 1 byte of DRAM.
  • They are raw access routines which do not support the different protocols of the multitude of sensors. Instead developers rely on a multitude of small libraries or code segments people reuse: A switch will set a state maybe with a noisy signal level, a knock sensor will send a brief noisy pulse, a rotary encoder a sequence of pulses on 2 lines, an ultrasonic sensor a pulse of a given duration to measure, etc. All these clutter the main code base of beginner programmer and do not help them code clear, clean programs, confusing them.

See the blog post for all the details.

Buy an Arduino compatible in the Adafruit shop.

No comments:

Post a Comment