IoT - Modular - SSD1306.cpp

From The TinkerNet Wiki
Jump to navigation Jump to search

#includes, Defines, etc...

 1 #include "libraries.h"
 2 #include "functions.h"
 3 #include "externs.h"
 4 
 5 #include "StickDude.h"
 6 
 7 #include "d-SSD1306_128x64.h"
 8 
 9 extern bool WalkingDudeIndicator;
10 extern bool WavingDudeIndicator;
11 
12 extern int DS18B20_Count;
13 extern int BME280_Count;

SSD1306_Exists

 1 bool SSD1306_Exists()
 2 {
 3   Wire.beginTransmission(0x3C);
 4 
 5   // See if something acknowledged the transmission
 6   int response = Wire.endTransmission();
 7   if (response == 0)
 8   {
 9     return (true);
10   }
11   else if (response == 4) // unknown error
12   {
13     debug_Trouble("Unknown response at hexAddress 0x3C");
14     return (false);
15   }
16   else
17   {
18     return (false);
19   }
20 }

SSD1306_init

 1 // Initialize the display
 2 void SSD1306_init()
 3 {
 4   {
 5     // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
 6     // the 'true' means reset the display on begin.  Let's see if this stops the intermittant freeze...
 7     if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, true))
 8     {
 9       debug_Trouble("SSD1306 allocation failed");
10       debug_Trouble("++++++++++++++++ STOPPING!!! +++++++++++++++++");
11       while (true) // Slam on da brakes!
12         blinkLED(100);
13       //  Does this ever actually happen?
14       //  Or even NEED to???
15     }
16     else
17     {
18       display.clearDisplay();
19 
20       display.dim(true); // These displays are excessively bright
21 
22       // Set up the active (changing) parts of the display
23       Dcanvas6.setFont(&FreeMonoBold9pt7b);
24 
25       // Set up the static (non-changing) parts of the display
26       Scanvas3.setFont(&FreeMonoBold9pt7b);
27 
28       display.setTextColor(WHITE, BLACK);
29       display.setTextSize(1);
30       { ////////////////////////////////  Why do I have to check this here
31         ////////////////////////////////  & can't avoid the setup if it's missing???
32         if (SSD1306_Exists())
33           debug_Success("SSD1306 initialized");
34         else
35           debug_Trouble("No SSD1306 found");
36       }
37     }
38   }
39 }

SSD1306_title

 1 // Place static text on the yellow part of the OLED display
 2 // Accepts the string (max 9 characters)
 3 void SSD1306_title(const char *Title)
 4 { // Max 9 characters...
 5   Tcanvas0.setFont(&FreeMonoBold12pt7b);
 6   Tcanvas0.fillScreen(BLACK);
 7   Tcanvas0.setCursor(0, 14);
 8   Tcanvas0.print(Title);
 9   // Tcanvas0.drawBitmap(4, row_Y[Row - 1] - 9, Tcanvas0.getBuffer(), 120, 15, WHITE, BLACK);
10   display.drawBitmap(4, 0, Tcanvas0.getBuffer(), 128, 15, WHITE, BLACK);
11   display.display();
12 }

SSD1306_Static

 1 // Place static text on the blue part of the OLED display
 2 // Accepts the string (max 11 characters) and the row number (1-3)
 3 void SSD1306_Static(const char *Line, int Row)
 4 { // Max 11 characters
 5   Scanvas3.fillScreen(BLACK);
 6   Scanvas3.setCursor(0, 11);
 7   Scanvas3.print(Line);
 8   if (strlen(Line) > 0)
 9     Scanvas3.print(":"); // Add a colon if there's actual content on the line...
10   display.drawBitmap(4, row_Y[Row - 1], Scanvas3.getBuffer(), 120, 15, WHITE, BLACK);
11 
12   display.display();
13 }

SSD1306_Display_Sensor_Data

 1 void SSD1306_Display_Sensor_Data(
 2     const char TitleText[10],
 3     const char L1Text[6], float L1Data,
 4     const char L2Text[6], float L2Data,
 5     const char L3Text[6], float L3Data)
 6 {
 7   SSD1306_title(TitleText);
 8 
 9   SSD1306_Static(L1Text, 1);
10   SSD1306_Static(L2Text, 2);
11   SSD1306_Static(L3Text, 3);
12 
13   char TempText[6];
14   char HumiText[6];
15   char PresText[6];
16 
17   if (strlen(L1Text) > 0)
18   {
19     sprintf(TempText, "%6.1f", L1Data);
20     Dcanvas6.fillScreen(BLACK);
21     Dcanvas6.setCursor(0, 11);
22     Dcanvas6.print(TempText);
23     display.drawBitmap(62, row_Y[0], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
24     display.display();
25   }
26 
27   if (strlen(L2Text) > 0)
28   {
29     sprintf(HumiText, "%6.1f", L2Data);
30     Dcanvas6.fillScreen(BLACK);
31     Dcanvas6.setCursor(0, 11);
32     Dcanvas6.print(HumiText);
33     display.drawBitmap(62, row_Y[1], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
34     display.display();
35   }
36 
37   if (strlen(L3Text) > 0)
38   {
39     sprintf(PresText, "%06.1f", L3Data);
40     Dcanvas6.fillScreen(BLACK);
41     Dcanvas6.setCursor(0, 11);
42     Dcanvas6.print(PresText);
43     display.drawBitmap(62, row_Y[2], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
44   }
45   display.display();
46 }

SSD1306_Display_Sensor_Data2

 1 void SSD1306_Display_Sensor_Data2(
 2     const char TitleText[10],
 3     const char L1Text[6], float L1Data,
 4     const char L2Text[6], float L2Data,
 5     const char L3Text[6], float L3Data)
 6 {
 7   SSD1306_title(TitleText);
 8 
 9   SSD1306_Static(L1Text, 1);
10   SSD1306_Static(L2Text, 2);
11   SSD1306_Static(L3Text, 3);
12 
13   char TempText[6];
14   char HumiText[6];
15   char PresText[6];
16 
17   if (strlen(L1Text) > 0)
18   {
19     sprintf(TempText, "%6.1f", L1Data);
20     Dcanvas6.fillScreen(BLACK);
21     Dcanvas6.setCursor(0, 11);
22     Dcanvas6.print(TempText);
23     display.drawBitmap(62, row_Y[0], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
24     display.display();
25   }
26 
27   if (strlen(L2Text) > 0)
28   {
29     sprintf(HumiText, "%6.2f", L2Data);
30     Dcanvas6.fillScreen(BLACK);
31     Dcanvas6.setCursor(0, 11);
32     Dcanvas6.print(HumiText);
33     display.drawBitmap(62, row_Y[1], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
34     display.display();
35   }
36 
37   if (strlen(L3Text) > 0)
38   {
39     sprintf(PresText, "%06.2f", L3Data);
40     Dcanvas6.fillScreen(BLACK);
41     Dcanvas6.setCursor(0, 11);
42     Dcanvas6.print(PresText);
43     display.drawBitmap(62, row_Y[2], Dcanvas6.getBuffer(), 65, 12, WHITE, BLACK);
44   }
45   display.display();
46 }

SSD1306_Indicate_Action

 1 // Create an activity indicator ////////////////////////////////////////////////
 2 ////////////////////////////////////////////////////////////////////////////////
 3 void SSD1306_Indicate_Action()
 4 {
 5   if (WalkingDudeIndicator)
 6     beerStickDude(DottyX, 64 - 12);
 7 
 8   if (WavingDudeIndicator)
 9     beerStickDude(120, 0);
10 }

WavingDude

 1 // StickDude waving in the corner...
 2 int DudeNumber = 0;
 3 void WavingDude()
 4 {
 5   if (DudeNumber >= StickDude_wave_count)
 6     DudeNumber = 0;
 7 
 8   StickDudePIC.fillScreen(BLACK);
 9   StickDudePIC.drawBitmap(0, 0, StickDude_wave[DudeNumber], DUDE_WIDTH, DUDE_HEIGHT, 1);
10 
11   DudeNumber++;
12 
13   display.drawBitmap(120, 0, StickDudePIC.getBuffer(), 8, 12, WHITE, BLACK);
14   display.display();
15 }

WalkingDude

 1 // Just a little dude that walks across the bottom edge of the display
 2 int DudeStep = 0;
 3 void WalkingDude()
 4 {
 5   DottyX++;
 6   if (DottyX > SCREEN_WIDTH)
 7     DottyX = 0;
 8   if (DudeStep >= StickDude_walk_count)
 9     DudeStep = 0;
10 
11   StickDudePIC.fillScreen(BLACK);
12   StickDudePIC.drawBitmap(0, 0, StickDude_walk[DudeStep], DUDE_WIDTH, DUDE_HEIGHT, 1);
13 
14   DudeStep++;
15 
16   display.drawBitmap(DottyX, 64 - 12, StickDudePIC.getBuffer(), 8, 12, WHITE, BLACK);
17   display.display();
18 }

beerStickDude

 1 // Dude stops for a beer
 2 void beerStickDude(int LEFT, int TOP)
 3 {
 4   int DudeNum;
 5 
 6   for (DudeNum = 0; DudeNum < StickDude_beer_count; DudeNum++)
 7   {
 8     {
 9       StickDudePIC.fillScreen(BLACK);
10       StickDudePIC.drawBitmap(0, 0, StickDude_beer[DudeNum], DUDE_WIDTH, DUDE_HEIGHT, 1);
11       display.drawBitmap(LEFT, TOP, StickDudePIC.getBuffer(), 8, 12, WHITE, BLACK);
12       display.display();
13       delay(60);
14     }
15   }
16   DudeNum--;
17   for (; DudeNum >= 0; DudeNum--)
18   {
19     {
20       StickDudePIC.fillScreen(BLACK);
21       StickDudePIC.drawBitmap(0, 0, StickDude_beer[DudeNum], DUDE_WIDTH, DUDE_HEIGHT, 1);
22       display.drawBitmap(LEFT, TOP, StickDudePIC.getBuffer(), 8, 12, WHITE, BLACK);
23       display.display();
24       delay(30);
25     }
26   }
27 }

SSD1306_off

1 // Turn display off /////////////////////////////////////////////////////////
2 void SSD1306_off()
3 {
4   display.ssd1306_command(SSD1306_DISPLAYOFF);
5   debug_Action("Screen OFF");
6 }

SSD1306_on

1 // Turn display on /////////////////////////////////////////////////////////
2 void SSD1306_on()
3 {
4   display.ssd1306_command(SSD1306_DISPLAYON);
5   debug_Action("Screen ON");
6 }

SSD1306_Dim

1 // Adjust display intensity ////////////////////////////////////////////////////
2 void SSD1306_Dim()
3 {
4   display.dim(true); // These displays are excessively bright
5   debug_Action("Screen DIM");
6 }

SSD1306_Bright

1 // Adjust display intensity ////////////////////////////////////////////////////
2 void SSD1306_Bright()
3 {
4   display.dim(false); // These displays are excessively bright
5   debug_Action("Screen BRIGHT");
6 }