element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum OO Digital Pin class: single class or separate in and out classes
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 37 replies
  • Subscribers 462 subscribers
  • Views 4498 views
  • Users 0 members are here
Related

OO Digital Pin class: single class or separate in and out classes

Jan Cumps
Jan Cumps over 2 years ago

I'm experimenting with light-weight C++ for microcontrollers. Looking for your opinion for class(es) that represent GPIO pin behaviour

An output pin can

  • accept a value and set it
  • return the value it is set to
  • maybe go high-z

An input can:

  • return the value at its input

I may represent this

  • very simple as a single class.
    Getting the value would perform the "return" action above. The class would decide if it returns output state or input state internally
    Setting would only succeed if the pin is an output pin, else either throw an exception or silently ignore.
    Switching between the two would be simple
  • as separate classes
    out type pin would set the output as indicated, and return the output state if asked
    in type pin would return the value at its input. It would not provide setters.
    Switching between two modes would mean discarding the original object and creating one of the other kind.
    (should both classes inherit from a common parent that defines the actual pin?)

How would you solve this, as OO designer / abstractor that loves simplicity?

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 2 years ago in reply to michaelkellett +3
    The compiler was way better than I thought... I gave it a shot with the GNU compiler for ARM, using the Pi Pico SDK. I created a simple class: Then that class is used in the main() function. The…
  • ggabe
    ggabe over 2 years ago +2
    Take a look at WiringPi, as an example how the single class API can be structured: http://wiringpi.com/reference/ I like how it can abstract MCU GPIO and IO expanders under the same API. if you need…
  • Andrew J
    Andrew J over 2 years ago +2
    On the basis of your points above, what you are essentially describing is a variable: you can set a value and you can read a value. I don't think that would benefit from being encapsulated by a class and…
Parents
  • scottiebabe
    scottiebabe over 2 years ago

    In the pic world microchip provides helper structs defining each pin as a bit field

    #define LATA LATA
    extern volatile uint16_t LATA __attribute__((__sfr__));
    typedef struct tagLATABITS {
      uint16_t LATA0:1;
      uint16_t LATA1:1;
      uint16_t LATA2:1;
      uint16_t LATA3:1;
      uint16_t LATA4:1;
    } LATABITS;
    extern volatile LATABITS LATAbits __attribute__((__sfr__));
    
    
    #define led LATAbits.LATA0
    
    led = 1;
    led ~= led; // etc....

    Not c++, but simple.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • scottiebabe
    scottiebabe over 2 years ago

    In the pic world microchip provides helper structs defining each pin as a bit field

    #define LATA LATA
    extern volatile uint16_t LATA __attribute__((__sfr__));
    typedef struct tagLATABITS {
      uint16_t LATA0:1;
      uint16_t LATA1:1;
      uint16_t LATA2:1;
      uint16_t LATA3:1;
      uint16_t LATA4:1;
    } LATABITS;
    extern volatile LATABITS LATAbits __attribute__((__sfr__));
    
    
    #define led LATAbits.LATA0
    
    led = 1;
    led ~= led; // etc....

    Not c++, but simple.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • Andrew J
    Andrew J over 2 years ago in reply to scottiebabe

    That's what I'm working with - and where my examples came from!  These could be OO-ified (I'm full of new English words today.)

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to scottiebabe

    The controller I'm using does the same:

    Setting a bit: PORTH.PODR.BIT.B2 = 0

    (the construct is a set of structs and unions that 'll get you up to bit level. Same as the microchip.)

    The thing is that it has different paths for reading and writing 

    Reading a bit: PORTH.PIDR.BIT.B2 

    I could solve this by setting:

    #define led_read = PORTH.PIDR.BIT.B2 
    #define led_write = PORTH.PODR.BIT.B2 

    led_write = 1;
    led_write ~= led_read; // etc

    scottiebabe said:
    Not c++, but simple.

    I took this (maybe most) simple example explicitly, because it's constrained enough to get a brainstorm going. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to Jan Cumps

    Is it like a PIC?

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to Jan Cumps

    Microchip hide that read/write distinction if you want:

    PORTA = 0xFF actually redirects (compiles) to LATA=0xFF under the covers, so you can read and write through PORTA.  I'm more explicit in my code to avoid that confusion so it ends up similar to the code you have where the read uses PORTA or RA0 (for a specific bit) and write uses LATA or LATA0 (for a specific bit). 

    The Renesas syntax is a lot more long winded so I'm not surprised you are looking to encapsulate it some way.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to scottiebabe

    It goes further as well (as you probably know) because each of the bits are identifiable directly:

    #define led LATA0

    led = 1;

    etc

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to Andrew J

    Yes! Thanks for reminding me, sometimes there is an underscore in front

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to Jan Cumps

    Lots of others have written long winded explanations on reading from the write latch versus pin state. When using an io pin as an output its customary to reference pin reads to the output drive latch. Capacitive loading on the IO pin can delay when the pin input buffer matches the output latches write value.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to scottiebabe

    That's interesting as the datasheet doesn't reference that.  It limits itself to "Writes to PORTx are actually written to corresponding LATx register.  Reads from PORTx register is return of actual I/O pin values" and also "A write operation to the LATx register has the same effect as a write to the corresponding PORTx register.  A read of the LATx register reads of the values held in the I/O Port latches, while a read of the PORTx register reads the actual I/O pin value".  No mention of a potential mismatch but it's good to know, thanks.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to Andrew J

    I can't remember where I read about it first, but one example

    image

    Credit to them. ww1.microchip.com/.../39711b.pdf

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to Andrew J

    The type of controller and its complexity was not the reason why I'm playing with this. It could have been any other. I'm just using the one the one that I currently have at hand. Could have been a Pico, or something else.

    I'm trying to do some brain flexing to find a good level of abstraction, and talk about ways of doing it with the fellow forum members. And to keep my c++/oo abstraction muscles trained.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube