Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- Code for Bald Engineer Live stream, OLED for DIY DMM. Based on this Adafruit example code.
- This is a library for our Monochrome OLEDs based on SSD1305 drivers
- Pick one up today in the adafruit shop!
- ------> https://www.adafruit.com/products/2675
- These displays use SPI or I2C to communicate, 3-5 pins are required to
- interface
- Adafruit invests time and resources providing this open source code,
- please support Adafruit and open-source hardware by purchasing
- products from Adafruit!
- Written by Limor Fried/Ladyada for Adafruit Industries.
- BSD license, check license.txt for more information
- All text above, and the splash screen below must be included in any redistribution
- *********************************************************************/
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1305.h>
- #include <Fonts/FreeSans9pt7b.h>
- #include <Fonts/FreeSans18pt7b.h>
- /* Goals:
- 1. Use a different font [CHECK]
- 2. Display counting nubmers from 0.00 to 100.99 [CHECK]
- 3. Center that horizontally on the display */
- /*Pin LCD Arduino
- DC 4 4
- CLK 7 7 [DATA0]
- Data1 8 8 [MOSI]
- CS 15 10
- RESET 16 9 */
- // Used for software SPI
- #define OLED_CLK 7
- #define OLED_MOSI 8
- // Used for software or hardware SPI
- #define OLED_CS 10
- #define OLED_DC 4
- // Used for I2C or SPI
- #define OLED_RESET 9
- // software SPI
- Adafruit_SSD1305 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- // hardware SPI
- //Adafruit_SSD1305 display(OLED_DC, OLED_RESET, OLED_CS);
- // I2C
- //Adafruit_SSD1305 display(OLED_RESET);
- #define NUMFLAKES 10
- #define XPOS 0
- #define YPOS 1
- #define DELTAY 2
- void fill(unsigned char dat1,unsigned char dat2)
- {
- unsigned char x,y;
- for(y=0;y<8;y++)
- {
- display.command(0xb0+y);
- display.command(0x00);
- display.command(0x10);
- for(x=0;x<132;x++)
- {
- display.data(dat1);
- display.data(dat2);
- }
- }
- }
- void setup() {
- SerialUSB.begin(9600);
- //delay(500);
- while(!SerialUSB);
- SerialUSB.println("DMM OLED Test with awesome live stream friends!");
- // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
- display.begin();
- // init done
- // make sure screen is clear
- display.clearDisplay();
- display.display();
- display.setRotation(0); // rotation 0 is with pin header on top
- }
- void alert_display(int count) {
- // invert the display
- for (int x=0; x < count; x++) {
- display.invertDisplay(true);
- delay(250);
- display.invertDisplay(false);
- delay(250);
- }
- }
- // borrowed from https://forum.arduino.cc/index.php?topic=349764.msg2423911#msg2423911
- char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
- uint32_t iPart = (uint32_t)val;
- uint32_t dPart = (uint32_t)((val - (double)iPart) * pow(10, prec));
- if (dPart < 10)
- sprintf(sout, "%d.0%d", iPart, dPart); // needs to be zero padded
- else
- sprintf(sout, "%d.%d", iPart, dPart); // needs to be zero padded
- }
- void print_reading(char* msg) {
- // SerialUSB.print("x1="); SerialUSB.print(x1); SerialUSB.print(", y1="); SerialUSB.println(y1);
- // SerialUSB.print("w="); SerialUSB.print(w); SerialUSB.print(", h="); SerialUSB.println(h);
- display.setTextColor(WHITE);
- display.setTextWrap(false);
- // find where the Mode Goes
- int16_t mode_x1, mode_y1;
- uint16_t mode_w, mode_h;
- display.setFont(&FreeSans9pt7b);
- char mode_msg[] = "DC mV";
- display.getTextBounds(mode_msg, 0, 0, &mode_x1, &mode_y1, &mode_w, &mode_h);
- // find where the Value Goes
- int16_t x1, y1;
- uint16_t w, h;
- display.setFont(&FreeSans18pt7b);
- display.getTextBounds(msg, 0, 0, &x1, &y1, &w, &h); //x1=3, y1=-25, w=154, h=26
- uint16_t value_centered_width = (128-w) / 2;
- uint16_t value_centered_height = (64-h) - (mode_h/2);
- // clear the display, then print the two things where they should be printed
- display.clearDisplay();
- display.setCursor(value_centered_width,value_centered_height);
- display.setFont(&FreeSans18pt7b);
- display.print(msg); // 10 chars with TextSize(2);
- display.setCursor(0,63);
- display.setFont(&FreeSans9pt7b);
- display.print(mode_msg);
- display.display();
- }
- void loop() {
- char value_as_string[10]; //Should display 000.00"
- char msg[12];
- int counter = 0;
- for(int x=1; x< 101; x++) {
- //char msg[10];
- //sprintf(msg, "%d.00", x); //%d6.2 limits width to 6 characters, tenths: %d6.1
- // float temp_number = 99.0 + (x/100.0);
- float temp_number = x + (random(0,100) / 100.0);
- //int temp_whole = temp_number;
- //int temp_mant = (temp_number - temp_whole) * 100;
- //SerialUSB.print("temp_number: "); SerialUSB.println(temp_number);
- //sprintf(msg, "%s", dtostrf(temp_number));
- dtostrf(temp_number, 5, 2, value_as_string);
- sprintf(msg, "%s", value_as_string);
- //dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
- print_reading(msg);
- }
- alert_display(10);
- }
- void testdrawchar(void) {
- display.setTextSize(1);
- display.setTextWrap(false);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- for (uint8_t i=0; i < 168; i++) {
- if (i == '\n') continue;
- display.write(i);
- if ((i > 0) && (i % 21 == 0))
- display.println();
- }
- display.display();
- }
- void testdrawcircle(void) {
- for (uint8_t i=0; i<display.height(); i+=2) {
- display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
- display.display();
- }
- }
- void testfillrect(void) {
- uint8_t color = 1;
- for (uint8_t i=0; i<display.height()/2; i+=3) {
- // alternate colors
- display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
- display.display();
- color++;
- }
- }
- void testdrawtriangle(void) {
- for (uint16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
- display.drawTriangle(display.width()/2, display.height()/2-i,
- display.width()/2-i, display.height()/2+i,
- display.width()/2+i, display.height()/2+i, WHITE);
- display.display();
- }
- }
- void testfilltriangle(void) {
- uint8_t color = WHITE;
- for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
- display.fillTriangle(display.width()/2, display.height()/2-i,
- display.width()/2-i, display.height()/2+i,
- display.width()/2+i, display.height()/2+i, WHITE);
- if (color == WHITE) color = BLACK;
- else color = WHITE;
- display.display();
- }
- }
- void testdrawroundrect(void) {
- for (uint8_t i=0; i<display.height()/3-2; i+=2) {
- display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
- display.display();
- }
- }
- void testfillroundrect(void) {
- uint8_t color = WHITE;
- for (uint8_t i=0; i<display.height()/3-2; i+=2) {
- display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
- if (color == WHITE) color = BLACK;
- else color = WHITE;
- display.display();
- }
- }
- void testdrawrect(void) {
- for (uint8_t i=0; i<display.height()/2; i+=2) {
- display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
- display.display();
- }
- }
- void testdrawline() {
- for (uint8_t i=0; i<display.width(); i+=4) {
- display.drawLine(0, 0, i, display.height()-1, WHITE);
- display.display();
- }
- for (uint8_t i=0; i<display.height(); i+=4) {
- display.drawLine(0, 0, display.width()-1, i, WHITE);
- display.display();
- }
- delay(250);
- display.clearDisplay();
- for (uint8_t i=0; i<display.width(); i+=4) {
- display.drawLine(0, display.height()-1, i, 0, WHITE);
- display.display();
- }
- for (int8_t i=display.height()-1; i>=0; i-=4) {
- display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
- display.display();
- }
- delay(250);
- display.clearDisplay();
- for (int8_t i=display.width()-1; i>=0; i-=4) {
- display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
- display.display();
- }
- for (int8_t i=display.height()-1; i>=0; i-=4) {
- display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
- display.display();
- }
- delay(250);
- display.clearDisplay();
- for (uint8_t i=0; i<display.height(); i+=4) {
- display.drawLine(display.width()-1, 0, 0, i, WHITE);
- display.display();
- }
- for (uint8_t i=0; i<display.width(); i+=4) {
- display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
- display.display();
- }
- delay(250);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement