Advertisement
gm310509

HAB OLED Utility Functions

May 12th, 2024 (edited)
553
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5.  
  6. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  7. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  8.  
  9. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  10. //#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  11. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  12.  
  13. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  14.  
  15.  
  16.  
  17. /**
  18.  * rightJustify
  19.  *
  20.  * Right justify a long integer into a buffer that is size fieldSize + 1.
  21.  * The filler character is used to pad the number to the right.
  22.  *
  23.  * @params
  24.  *   buf - a buffer that is big enough to hold the converted number *and* a trailing null byte.
  25.  *   fieldSize - the number of characters into which the number is formatted.
  26.  *   x - the number to convert.
  27.  *   filler - the character used to pad out the converted number.
  28.  *
  29.  * @return
  30.  *   a pointer to the buffer.
  31.  *
  32.  * @note
  33.  * The buffer must be at least fieldSize + 1 characters in size.
  34.  *
  35.  * If the fieldSize is too small for the converted integer, then the buffer will not contain
  36.  * all of the digits representing the number.
  37.  *
  38.  */
  39. char * rightJustify(char *buf, int fieldSize, long x, char filler = ' ', char seperator = '\0') {
  40.   boolean negativeSign = false;
  41.  
  42.   // Check for a -ve sign and remember this for later.
  43.   // ensure we have |x| for conversion.
  44.   if (negativeSign = x < 0) {
  45.     x = -x;
  46.   }
  47.  
  48.   // NULL terminate the string.
  49.   char *p = buf + fieldSize;
  50.   *p-- = '\0';
  51.  
  52.   // Load up the characters representing the number.
  53.   int digitCount = 0;
  54.   do {
  55.     *p-- = x % 10 + '0';
  56.     x /= 10;
  57.     digitCount++;
  58.     if (seperator && digitCount % 3 == 0 && x && p >= buf) {
  59.       *p-- = seperator;
  60.     }
  61.   } while (x && p >= buf);
  62.  
  63.   // Do we need a negative sign? If so, put it in.
  64.   if (p >= buf && negativeSign) {
  65.     *p-- = '-';
  66.   }
  67.  
  68.   // Pad out the string.
  69.   while (p >= buf) {
  70.     *p-- = filler;
  71.   }
  72.   return buf;
  73. }
  74.  
  75.  
  76.  
  77. void testPad(long x, int w) {
  78.   char buf[20];
  79.   for (int i = 0; i < sizeof(buf); i++) {
  80.     buf[i] = '*';
  81.   }
  82.   Serial.print("Formatting: "); Serial.println(x);
  83.   Serial.print("'"); Serial.print(rightJustify(buf, w, x, ' ', ',')); Serial.println("'");
  84.   Serial.print("Hex: ");
  85.   for (int i = 0; i < sizeof buf; i++) {
  86.     Serial.print((byte) buf[i], HEX);
  87.     Serial.print(' ');
  88.   }
  89.   Serial.println();
  90.   Serial.println();
  91. }
  92.  
  93. /**
  94.  * To make it easier to process, I will use this function as an interface for the display.
  95.  */
  96. void updateDisplay(int hour, int minute, int second, float lat, float lon, float alt, float tempC1, float tempC2) {
  97.   char buf[10];  // Buffer for padding up to 9 characters including string terminator.
  98.  
  99.   display.clearDisplay();
  100.   display.setTextSize(1); // Normal font size for better data visibility
  101.   display.setTextColor(WHITE);
  102.   display.setCursor(0,0);
  103.  
  104.   // Display altitude or no GPS if not available
  105.     display.print("Alt: ");
  106.     display.print(alt); // Display altitude in meters
  107.     display.println("m");
  108.  
  109.   display.print("IT: ");
  110.   display.print(tempC1);
  111.   display.println("C");
  112.  
  113.   display.print("XT: ");
  114.   display.print(tempC2);
  115.   display.println("C");
  116.  
  117.   // Display GPS time if available
  118. //    int hour = gps.time.hour() - 6; // Convert UTC to CST
  119.     hour = hour - 6; // Convert UTC to CST
  120.     if (hour < 0)
  121.       hour += 24;
  122.     display.print("Time: ");
  123.     display.print(rightJustify(buf, 2, hour, ' '));
  124.     display.print(":");
  125.     display.print(rightJustify(buf, 2, minute, '0'));
  126.     display.print(":");
  127.     display.println(rightJustify(buf, 2, second, '0'));
  128.  
  129.   display.display(); // Update display with all the new info
  130. }
  131.  
  132.  
  133. void setup() {
  134.   Serial.begin(115200);
  135.  
  136.   testPad(4, 4);
  137.   testPad(-4, 4);
  138.   testPad(0, 4);
  139.   testPad(1234567, 10);
  140.   testPad(-1234567, 10);
  141.   testPad(1234, 6);
  142.   testPad(-1234, 6);
  143.  
  144.   pinMode(LED_BUILTIN, OUTPUT);
  145.   randomSeed(analogRead(A0));
  146.   if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  147.     Serial.println("SSD1306 allocation failed");
  148.     pinMode(LED_BUILTIN, OUTPUT);
  149.     long delayPeriod = 100;
  150.     while(true) {
  151.       // Don't proceed, loop forever and indicate this on the builtin LED.
  152.       digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
  153.       delay(delayPeriod);
  154.       delayPeriod = 1000 - delayPeriod;
  155.     }
  156.   }
  157.   Serial.begin("Starting the display");
  158.   display.display();
  159.   delay(2000);
  160.   display.clearDisplay();
  161.  
  162. }
  163.  
  164. void loop() {
  165.  
  166.   digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
  167.   int hour = random(0,24);
  168.   int minute = random(0,60);
  169.   int second = random(0,60);
  170.   float lat = random(0,90L*60L*60L) / 3600.0;
  171.   float lon = random(0,180L*60L*60L) / 3600.0;
  172.   float alt = random(0, 60000L);
  173.   float t1 = random(0,160)/2.0 - 40;
  174.   float t2 = random(0,160)/2.0 - 40;
  175.   updateDisplay(hour, minute, second, lat, lon, alt, t1, t2);
  176.   delay(2000);
  177. }
  178.  
  179.  
Tags: Hab
Advertisement
Comments
  • gm310509
    11 days
    Comment was deleted
  • gm310509
    11 days
    # text 0.42 KB | 0 0
    1. A test program that contains:
    2. * rightJustify - right justifies a number in a text field and fills any leading characters with a specified character.
    3. * updateDisplay - a place holder that updates the display - will be enhanced to support different font sizes - and possibly other annotations.
    4.  
    5. The right Justify function can also optionally insert a comma (or any character) separator into the thousands position for larger numbers.
Add Comment
Please, Sign In to add comment
Advertisement