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?