11/* !
22 @file examples/ch1115/framerate_test/main.cpp
3- @brief Example file for ER_OLEDM1_CH1115 display, measuring Frame rate per second)FPS, HW SPI
3+ @brief Example file for ER_OLEDM1_CH1115 display, measuring Frame rate per second (FPS)
4+ Configurable HW or SW SPI at compile time.
5+ @author Gavin Lyons.
46 @test
5- -# Test 601 FPS HW SPI
7+ -# Test 601 hardware SPI
8+ -# Test 602 software SPI
69*/
710
811#include < ctime>
1114
1215// / @cond
1316
14- // GPIO
15- const uint8_t RES = 25 ; // GPIO pin number pick any you want
16- const uint8_t DC = 24 ; // GPIO pin number pick any you want
17- int GPIO_CHIP_DEVICE = 0 ; // GPIO chip device number usually 0
17+ // -------------------------------------------------------------------------
18+ // CONFIGURATION: Comment out for software SPI, leave in for hardware SPI
19+ // -------------------------------------------------------------------------
20+ # define hardwareSPI
1821
19- // Screen
22+ // ==== Globals & Common Settings ====
2023const uint8_t MY_OLED_WIDTH = 128 ;
2124const uint8_t MY_OLED_HEIGHT = 64 ;
22- #define FULLSCREEN (MY_OLED_WIDTH * (MY_OLED_HEIGHT /8 )) // 1024 bytes = 128 * 64/8
23- const uint8_t OLEDcontrast = 0x80 ; // Constrast 00 to FF , 0x80 is default.
24-
25- // SPi
26- // Hardware SPI setup
27- int HWSPI_DEVICE = 0 ; // A SPI device, >= 0. which SPI interface to use
28- int HWSPI_CHANNEL = 0 ; // A SPI channel, >= 0. Which Chip enable pin to use
29- int HWSPI_SPEED = 8000000 ; // The speed of serial communication in bits per second.
30- int HWSPI_FLAGS = 0 ; // last 2 LSB bits define SPI mode, see readme, mode 0 for this device
25+ const uint8_t OLEDcontrast = 0x80 ; // Contrast 00 to FF, 0x80 is default.
26+ #define myScreenSize (MY_OLED_WIDTH * (MY_OLED_HEIGHT /8 ))
3127
32- ERMCH1115 myOLED (MY_OLED_WIDTH ,MY_OLED_HEIGHT , RES , DC ) ; // instantiate an object
28+ // Common GPIO
29+ const uint8_t RES = 25 ; // GPIO pin number pick any you want
30+ const uint8_t DC = 24 ; // GPIO pin number pick any you want
31+ int GPIO_CHIP_DEVICE = 0 ; // GPIO chip device number usually 0
3332
34- // vars for the test
33+ // Test variables
3534uint16_t count = 0 ;
3635bool colour = 1 ;
37- uint64_t previousCounter =0 ;
38- uint16_t countLimit = 1500 ;
39-
40- // =============== Function prototype ================
36+ uint64_t previousCounter = 0 ;
37+
38+ // =========================================================================
39+ // SPI-Specific Parameters & Object Initialization
40+ // =========================================================================
41+ #ifdef hardwareSPI
42+ uint16_t countLimit = 1500 ; // HW SPI limit
43+ // Hardware SPI setup parameters
44+ int SPI_DEVICE = 0 ; // A SPI device, >= 0. which SPI interface to use
45+ int SPI_CHANNEL = 0 ; // A SPI channel, >= 0. Which Chip enable pin to use
46+ int SPI_SPEED = 1000000 ; // The speed of serial communication in bits per second.
47+ int SPI_FLAGS = 0 ; // last 2 LSB bits define SPI mode, mode 0 for this device
48+ // Constructor variant for Hardware SPI
49+ ERMCH1115 myOLED (MY_OLED_WIDTH , MY_OLED_HEIGHT , RES , DC );
50+ #else // Software SPI Mode active
51+ uint16_t countLimit = 1500 ; // SW SPI
52+ // Software SPI pin mappings
53+ const uint8_t DIN = 5 ;
54+ const uint8_t SCLK = 6 ;
55+ const uint8_t CS = 21 ;
56+ // Constructor variant for Software SPI
57+ ERMCH1115 myOLED (MY_OLED_WIDTH , MY_OLED_HEIGHT , RES , DC , CS , SCLK , DIN );
58+ #endif
59+ // ===
60+
61+ // === Function prototypes ===
4162bool Setup (void );
4263void myTest (void );
4364void EndTest (void );
4465void display (long , int );
4566static uint64_t counter ( void );
4667
47- // ======================= Main ================ ===
68+ // === Main ===
4869int main ()
4970{
5071 if (!Setup ()) return -1 ;
5172 myTest ();
5273 EndTest ();
5374 return 0 ;
5475}
55- // ======================= End of main ================ ===
76+ // === End of main ===
5677
5778
5879bool Setup (void )
5980{
6081 printf (" OLED Begin\r\n " );
61- printf (" lgpio library Version Number :: %i\r\n " ,lguVersion ());
82+ printf (" lgpio library Version Number :: %i\r\n " , lguVersion ());
6283 printf (" Display_LIB_RPI Library version number :: %u\r\n " , rdlib::LibraryVersion ());
6384 delayMilliSecRDL (50 );
6485
65- if (myOLED.OLEDbegin (OLEDcontrast, HWSPI_DEVICE , HWSPI_CHANNEL , HWSPI_SPEED , HWSPI_FLAGS , GPIO_CHIP_DEVICE ) != rdlib::Success) // initialize the OLED
86+ #ifdef hardwareSPI
87+ printf (" Running in HARDWARE SPI Mode...\r\n " );
88+ // Initialize using Hardware SPI parameters
89+ if (myOLED.OLEDbegin (OLEDcontrast, SPI_DEVICE , SPI_CHANNEL , SPI_SPEED , SPI_FLAGS , GPIO_CHIP_DEVICE ) != rdlib::Success)
90+ {
91+ printf (" Error 1201: Setup GPIO/HW SPI failed, \r\n " );
92+ return false ;
93+ }
94+ #else
95+ printf (" Running in SOFTWARE SPI Mode...\r\n " );
96+ // Initialize using Software SPI parameters
97+ if (myOLED.OLEDbegin (OLEDcontrast, GPIO_CHIP_DEVICE ) != rdlib::Success)
6698 {
67- printf (" Error 1202: Setup : Cannot start spi , \r\n " );
99+ printf (" Error 1202: Setup GPIO/SW SPI failed , \r\n " );
68100 return false ;
69101 }
70102 delayMilliSecRDL (50 );
71- myOLED.OLEDFillScreen (0x0F ); // splash screen bars
103+ printf (" Software SPI Freq delay is set to %u uS \n " , myOLED.OLEDHighFreqDelayGet ());
104+ #endif
105+
106+ delayMilliSecRDL (50 );
107+ myOLED.OLEDFillScreen (0x0F ); // splash screen bars
72108 delayMilliSecRDL (1500 );
73109 return true ;
74110}
@@ -82,12 +118,18 @@ void EndTest(void)
82118
83119void myTest () {
84120
121+ #ifdef hardwareSPI
122+ printf (" FPS HW SPI:: test ends at %u\r\n " , countLimit);
123+ #else
124+ printf (" FPS SW SPI:: test ends at %u\r\n " , countLimit);
125+ #endif
126+
85127 // Buffer setup, Define a buffer to cover whole screen
86- uint8_t screenBuffer[FULLSCREEN ]; // 1024 bytes = 128 * 64/8
87- if (myOLED.OLEDSetBufferPtr (MY_OLED_WIDTH ,MY_OLED_HEIGHT , screenBuffer) != rdlib::Success) return ;
128+ uint8_t screenBuffer[myScreenSize ]; // 1024 bytes = 128 * 64/8
129+ if (myOLED.OLEDSetBufferPtr (MY_OLED_WIDTH , MY_OLED_HEIGHT , screenBuffer) != rdlib::Success) return ;
88130
89131 myOLED.OLEDclearBuffer (); // Clear buffer
90- printf ( " FPS HW SPI:: test ends at %u \r\n " ,countLimit );
132+
91133 while (count < countLimit)
92134 {
93135 static long framerate = 0 ;
@@ -96,10 +138,8 @@ void myTest() {
96138 count++;
97139 delayMilliSecRDL (1 );
98140 }
99-
100141}
101142
102-
103143// Function to display left hand side buffer
104144void display (long currentFramerate, int count)
105145{
@@ -113,6 +153,7 @@ void display(long currentFramerate, int count)
113153 myOLED.print (count);
114154
115155 // Values to count frame rate per second
156+ // cppcheck-suppress variableScope
116157 static long lastFramerate = 0 ;
117158 static uint16_t fps;
118159 uint64_t currentCounter = counter ();
@@ -140,14 +181,12 @@ void display(long currentFramerate, int count)
140181 myOLED.OLEDupdate ();
141182}
142183
143- // This returns nano-seconds as a 64-bit unsigned number, monotonically increasing,
144- // probably since system boot.
145- // The actual resolution looks like microseconds. returns nanoseconds
184+ // This returns nano-seconds as a 64-bit unsigned number.
146185static uint64_t counter ( void )
147186{
148- struct timespec now;
149- clock_gettime ( CLOCK_MONOTONIC , &now );
150- return ((uint64_t )now.tv_sec * 1000000000U ) + (uint64_t )now.tv_nsec ;
187+ struct timespec now;
188+ clock_gettime ( CLOCK_MONOTONIC , &now );
189+ return ((uint64_t )now.tv_sec * 1000000000U ) + (uint64_t )now.tv_nsec ;
151190}
152191
153192// / @endcond
0 commit comments