0

this is a follow up on: C language, providing function pointer to a class and have the class operate a function above main()

I have a Microchip Fubarino / PIC32MX250F128D

I would like to learn how, since the C language doesn't have classes, what can i do instead? But i was thinking something like this: an object to which i pass the pointer of the peripheral and it's properties registers, like PORTA, LATA, PORTB, LATB, PORTC, LATC and store some timestamps for Buttons, leds.

Also that chip has 2 x UART, 2 X I²C and 2 x SPI.

I'm using a SSD1306 OLED screen, but i have 2 of them on my veroboard each using the their own I²C port. i tried uses "switches" in the code for 2 display's, but somehow that made a line with random pixel appear on the first line of the display and shifting the image one line lower without altering the original code accept using switch statements to address between I²C1 and I²C2, the same code.

To prevent having a multitude of the same code or duplicate codes that's a bit harder to maintain or keep track off, C++ provides neat classes and even struct can be used as classes.

I'm be able to make pointer to peripherals

volatile unsigned short *address_i2c1 = &I2C1STAT; // I²C MCU register / real hardware.

      union {
        unsigned short byte;
        // a structure with 8 single bit bit-field objects, overlapping the union member "byte"
        struct {
          unsigned jumble: 14;
          unsigned TRSTAT: 1;
          unsigned remainder: 1;
        };
      } byte_u;
      

void I2CSend(char data) {
      // code block
      I2C1TRN = data;  
      //while (I2C1STATbits.TRSTAT == 1) // original Microchip code
      //while(I2C1STAT & (1 << 14)); // an alternative aproach / test
      //while(I2C1STAT & 0b100000000000000); // a faster alternative aproach
    
     // OOP method?
      do {
        byte_u.byte = *address_i2c1;
      }
      while (byte_u.TRSTAT);  
      if (I2C1STATbits.ACKSTAT) {
          //Report that we did not receive a NAK. Abort and send STOP.
          //Serial.println("I²C error");
        } else {
          //Serial.println("I²C ok");
        }
}

What options do i have?

NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • 1
    Functions that operate on structures. In Cryptography we generally see "context" structures, where the "context" is simply the first parameter of each function. – Maarten Bodewes Dec 10 '22 at 04:09
  • 1
    You might be looking for this: https://stackoverflow.com/questions/415452/object-orientation-in-c – Harith Dec 10 '22 at 04:11
  • 1
    Or this:. https://stackoverflow.com/questions/351733/how-would-one-write-object-oriented-code-in-c – Harith Dec 10 '22 at 04:12
  • 1
    Side note: there is no direct correlation between stated goal "To prevent having a multitude of the same code or duplicate codes" and desire to write OOP code. Make sure you decide which one is your real goal as it will make your life easier. (It is not hard to "write" C++ 1.0 in C - so called "C with classes", but it is not necessary to be very beneficial to simple project) – Alexei Levenkov Dec 10 '22 at 04:15
  • 1
    duplicates: [Object oriented programming in C](https://stackoverflow.com/q/2181079/995714), [How would one write object-oriented code in C?](https://stackoverflow.com/q/351733/995714), [Pure C that mimics object-oriented programming?](https://stackoverflow.com/q/4896536/995714). This is quite common in Linux drivers – phuclv Dec 10 '22 at 04:16
  • `struct { unsigned jumble: 14; unsigned TRSTAT: 1; unsigned remainder: 1; };` is certainly not a `a structure with 8 single bit bit-field objects`. – chux - Reinstate Monica Dec 10 '22 at 04:24
  • @AlexeiLevenkov, been idle for some time, received the Microchip Fubarino a couple of months ago, i just wanna have the foundation of a firmware right or correct, before it gets complicated to navigate through after you haven't looked in to it for a while or dramatically increased the code in size and later find out it's not quite efficient but it works. – NaturalDemon Dec 10 '22 at 04:25
  • @chux-ReinstateMonica, it's a 16 bit short: volatile unsigned short *address_i2c1 the comment was still around from a C++ project on my disc, forgot to remove it. – NaturalDemon Dec 10 '22 at 04:27

1 Answers1

1

https://www.cs.rit.edu/~ats/books/ooc.pdf is a great resource on how to do OOP in ANSI C. The use of structures, pointers and function pointers is key. The book contains clear examples but the complete code is available on GitHub, here for example: https://github.com/shichao-an/ooc.

I should mention that the question is probably a duplicate of other similar questions already answered on this site, as mentioned in the comments to the post.

picchiolu
  • 1,120
  • 5
  • 20