Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Mapping port and pin in C++ CTOR - best practice

2013-12-31 by ron@teahousestudio.com

I'm modifying and extending the OneButton library (which relies on Arduino environment) for generic AVR use.

Briefly, my Button class (yeah - I don't really like adding a capital C to a class name) maintains a state model to determine if changes on an input pin represent a click event, double click event, and so on, and call a C function to handle the appropriate event handler (onClick, onDoubleClick).

In the original code (OneButton) the CTOR takes a pin number - this is the Arduino pin and behind the scenes, the Arduino environment maps this into a port/pin combination during operations such as DigitalRead().

I want to do something similar - only I don't have Arduino pin numbers to play with (currently the code is targetted at ATTiny85 but it should work on any ATTiny or ATMega).

If I forget about specifying a port (after all, I can only use PORTB on an ATTiny85) I could pass just the port bit (for example, PB1) and hard code setting port direction (using the DDRB macro) and testing the input bit state - and this is what I will do for now.

I can define some constants representing the port number (and there are already macros for the bit numbers) and use a switch statement to determine which macro or inline assembly instruction to use - but this will require conditional code to determine whether the appropriate macro is available for the target platform.

I wondered if there is a generic solution to this problem - preferably one that doesn't have the code overhead of the Arduino.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.