Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- //#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- /**
- * rightJustify
- *
- * Right justify a long integer into a buffer that is size fieldSize + 1.
- * The filler character is used to pad the number to the right.
- *
- * @params
- * buf - a buffer that is big enough to hold the converted number *and* a trailing null byte.
- * fieldSize - the number of characters into which the number is formatted.
- * x - the number to convert.
- * filler - the character used to pad out the converted number.
- *
- * @return
- * a pointer to the buffer.
- *
- * @note
- * The buffer must be at least fieldSize + 1 characters in size.
- *
- * If the fieldSize is too small for the converted integer, then the buffer will not contain
- * all of the digits representing the number.
- *
- */
- char * rightJustify(char *buf, int fieldSize, long x, char filler = ' ', char seperator = '\0') {
- boolean negativeSign = false;
- // Check for a -ve sign and remember this for later.
- // ensure we have |x| for conversion.
- if (negativeSign = x < 0) {
- x = -x;
- }
- // NULL terminate the string.
- char *p = buf + fieldSize;
- *p-- = '\0';
- // Load up the characters representing the number.
- int digitCount = 0;
- do {
- *p-- = x % 10 + '0';
- x /= 10;
- digitCount++;
- if (seperator && digitCount % 3 == 0 && x && p >= buf) {
- *p-- = seperator;
- }
- } while (x && p >= buf);
- // Do we need a negative sign? If so, put it in.
- if (p >= buf && negativeSign) {
- *p-- = '-';
- }
- // Pad out the string.
- while (p >= buf) {
- *p-- = filler;
- }
- return buf;
- }
- void testPad(long x, int w) {
- char buf[20];
- for (int i = 0; i < sizeof(buf); i++) {
- buf[i] = '*';
- }
- Serial.print("Formatting: "); Serial.println(x);
- Serial.print("'"); Serial.print(rightJustify(buf, w, x, ' ', ',')); Serial.println("'");
- Serial.print("Hex: ");
- for (int i = 0; i < sizeof buf; i++) {
- Serial.print((byte) buf[i], HEX);
- Serial.print(' ');
- }
- Serial.println();
- Serial.println();
- }
- /**
- * To make it easier to process, I will use this function as an interface for the display.
- */
- void updateDisplay(int hour, int minute, int second, float lat, float lon, float alt, float tempC1, float tempC2) {
- char buf[10]; // Buffer for padding up to 9 characters including string terminator.
- display.clearDisplay();
- display.setTextSize(1); // Normal font size for better data visibility
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- // Display altitude or no GPS if not available
- display.print("Alt: ");
- display.print(alt); // Display altitude in meters
- display.println("m");
- display.print("IT: ");
- display.print(tempC1);
- display.println("C");
- display.print("XT: ");
- display.print(tempC2);
- display.println("C");
- // Display GPS time if available
- // int hour = gps.time.hour() - 6; // Convert UTC to CST
- hour = hour - 6; // Convert UTC to CST
- if (hour < 0)
- hour += 24;
- display.print("Time: ");
- display.print(rightJustify(buf, 2, hour, ' '));
- display.print(":");
- display.print(rightJustify(buf, 2, minute, '0'));
- display.print(":");
- display.println(rightJustify(buf, 2, second, '0'));
- display.display(); // Update display with all the new info
- }
- void setup() {
- Serial.begin(115200);
- testPad(4, 4);
- testPad(-4, 4);
- testPad(0, 4);
- testPad(1234567, 10);
- testPad(-1234567, 10);
- testPad(1234, 6);
- testPad(-1234, 6);
- pinMode(LED_BUILTIN, OUTPUT);
- randomSeed(analogRead(A0));
- if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
- Serial.println("SSD1306 allocation failed");
- pinMode(LED_BUILTIN, OUTPUT);
- long delayPeriod = 100;
- while(true) {
- // Don't proceed, loop forever and indicate this on the builtin LED.
- digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
- delay(delayPeriod);
- delayPeriod = 1000 - delayPeriod;
- }
- }
- Serial.begin("Starting the display");
- display.display();
- delay(2000);
- display.clearDisplay();
- }
- void loop() {
- digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
- int hour = random(0,24);
- int minute = random(0,60);
- int second = random(0,60);
- float lat = random(0,90L*60L*60L) / 3600.0;
- float lon = random(0,180L*60L*60L) / 3600.0;
- float alt = random(0, 60000L);
- float t1 = random(0,160)/2.0 - 40;
- float t2 = random(0,160)/2.0 - 40;
- updateDisplay(hour, minute, second, lat, lon, alt, t1, t2);
- delay(2000);
- }
Advertisement
Comments
-
Comment was deleted
-
- A test program that contains:
- * rightJustify - right justifies a number in a text field and fills any leading characters with a specified character.
- * updateDisplay - a place holder that updates the display - will be enhanced to support different font sizes - and possibly other annotations.
- 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