Advertisement
baldengineer

Steam OLED Counter

Apr 26th, 2020
4,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.59 KB | None | 0 0
  1. /*********************************************************************
  2. Code for Bald Engineer Live stream, OLED for DIY DMM. Based on this Adafruit example code.
  3.  
  4. This is a library for our Monochrome OLEDs based on SSD1305 drivers
  5.  
  6.   Pick one up today in the adafruit shop!
  7.   ------> https://www.adafruit.com/products/2675
  8.  
  9. These displays use SPI or I2C to communicate, 3-5 pins are required to  
  10. interface
  11.  
  12. Adafruit invests time and resources providing this open source code,
  13. please support Adafruit and open-source hardware by purchasing
  14. products from Adafruit!
  15.  
  16. Written by Limor Fried/Ladyada  for Adafruit Industries.  
  17. BSD license, check license.txt for more information
  18. All text above, and the splash screen below must be included in any redistribution
  19. *********************************************************************/
  20.  
  21. #include <Wire.h>
  22. #include <SPI.h>
  23. #include <Adafruit_GFX.h>
  24. #include <Adafruit_SSD1305.h>
  25. #include <Fonts/FreeSans9pt7b.h>
  26. #include <Fonts/FreeSans18pt7b.h>
  27.  
  28. /* Goals:
  29. 1. Use a different font [CHECK]
  30. 2. Display counting nubmers from 0.00 to 100.99 [CHECK]
  31. 3. Center that horizontally on the display */
  32.  
  33. /*Pin   LCD     Arduino
  34. DC      4       4
  35. CLK     7       7           [DATA0]
  36. Data1   8       8           [MOSI]
  37. CS      15      10
  38. RESET   16      9 */
  39.  
  40. // Used for software SPI
  41. #define OLED_CLK 7
  42. #define OLED_MOSI 8
  43.  
  44. // Used for software or hardware SPI
  45. #define OLED_CS 10
  46. #define OLED_DC 4
  47.  
  48. // Used for I2C or SPI
  49. #define OLED_RESET 9
  50.  
  51. // software SPI
  52. Adafruit_SSD1305 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  53. // hardware SPI
  54. //Adafruit_SSD1305 display(OLED_DC, OLED_RESET, OLED_CS);
  55.  
  56. // I2C
  57. //Adafruit_SSD1305 display(OLED_RESET);
  58.  
  59. #define NUMFLAKES 10
  60. #define XPOS 0
  61. #define YPOS 1
  62. #define DELTAY 2
  63.  
  64. void fill(unsigned char dat1,unsigned char dat2)
  65. {
  66.   unsigned char x,y;
  67.   for(y=0;y<8;y++)
  68.   {
  69.     display.command(0xb0+y);
  70.     display.command(0x00);
  71.     display.command(0x10);
  72.     for(x=0;x<132;x++)
  73.     {
  74.       display.data(dat1);
  75.       display.data(dat2);
  76.     }
  77.   }
  78. }
  79.  
  80.  
  81. void setup()   {                
  82.   SerialUSB.begin(9600);
  83.   //delay(500);
  84.   while(!SerialUSB);
  85.   SerialUSB.println("DMM OLED Test with awesome live stream friends!");
  86.  
  87.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  88.   display.begin();
  89.   // init done
  90.  
  91.   // make sure screen is clear
  92.   display.clearDisplay();
  93.   display.display();
  94.   display.setRotation(0);  // rotation 0 is with pin header on top
  95.  }
  96.  
  97. void alert_display(int count) {
  98.       // invert the display
  99.     for (int x=0; x < count; x++) {
  100.           display.invertDisplay(true);
  101.           delay(250);
  102.           display.invertDisplay(false);
  103.           delay(250);
  104.     }
  105. }
  106.  
  107. // borrowed from https://forum.arduino.cc/index.php?topic=349764.msg2423911#msg2423911
  108. char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
  109.     uint32_t iPart = (uint32_t)val;
  110.     uint32_t dPart = (uint32_t)((val - (double)iPart) * pow(10, prec));
  111.  
  112.     if (dPart < 10)
  113.         sprintf(sout, "%d.0%d", iPart, dPart);  // needs to be zero padded
  114.     else
  115.         sprintf(sout, "%d.%d", iPart, dPart);  // needs to be zero padded
  116.  
  117. }
  118.  
  119. void print_reading(char* msg) {
  120.  
  121.    
  122. //  SerialUSB.print("x1="); SerialUSB.print(x1); SerialUSB.print(", y1="); SerialUSB.println(y1);
  123. //  SerialUSB.print("w=");  SerialUSB.print(w);  SerialUSB.print(", h=");  SerialUSB.println(h);
  124.     display.setTextColor(WHITE);
  125.     display.setTextWrap(false);
  126.  
  127.     // find where the Mode Goes
  128.     int16_t  mode_x1, mode_y1;
  129.     uint16_t mode_w, mode_h;
  130.     display.setFont(&FreeSans9pt7b);
  131.     char mode_msg[] = "DC mV";
  132.     display.getTextBounds(mode_msg, 0, 0, &mode_x1, &mode_y1, &mode_w, &mode_h);
  133.  
  134.     // find where the Value Goes
  135.     int16_t  x1, y1;
  136.     uint16_t w, h;
  137.     display.setFont(&FreeSans18pt7b);
  138.     display.getTextBounds(msg, 0, 0, &x1, &y1, &w, &h);  //x1=3, y1=-25, w=154, h=26
  139.     uint16_t value_centered_width = (128-w) / 2;
  140.     uint16_t value_centered_height = (64-h) - (mode_h/2);
  141.  
  142.     // clear the display, then print the two things where they should be printed
  143.     display.clearDisplay();
  144.     display.setCursor(value_centered_width,value_centered_height); 
  145.     display.setFont(&FreeSans18pt7b);
  146.     display.print(msg); // 10 chars with TextSize(2);
  147.     display.setCursor(0,63);   
  148.     display.setFont(&FreeSans9pt7b);
  149.     display.print(mode_msg);
  150.     display.display();
  151.  
  152. }
  153.  
  154. void loop() {
  155.     char value_as_string[10];  //Should display 000.00"
  156.     char msg[12];
  157.     int counter = 0;
  158.     for(int x=1; x< 101; x++) {
  159.         //char msg[10];
  160.         //sprintf(msg, "%d.00", x);  //%d6.2 limits width to 6 characters, tenths: %d6.1
  161.     //  float temp_number = 99.0 + (x/100.0);
  162.         float temp_number = x + (random(0,100) / 100.0);
  163.         //int temp_whole = temp_number;
  164.         //int temp_mant = (temp_number - temp_whole) * 100;
  165.         //SerialUSB.print("temp_number: "); SerialUSB.println(temp_number);
  166.         //sprintf(msg, "%s", dtostrf(temp_number));
  167.         dtostrf(temp_number, 5, 2, value_as_string);
  168.         sprintf(msg, "%s", value_as_string);
  169.         //dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
  170.         print_reading(msg);
  171.     }
  172.  
  173.     alert_display(10);
  174. }
  175.  
  176.  
  177. void testdrawchar(void) {
  178.   display.setTextSize(1);
  179.   display.setTextWrap(false);
  180.   display.setTextColor(WHITE);
  181.   display.setCursor(0,0);
  182.  
  183.   for (uint8_t i=0; i < 168; i++) {
  184.     if (i == '\n') continue;
  185.     display.write(i);
  186.     if ((i > 0) && (i % 21 == 0))
  187.       display.println();
  188.   }    
  189.   display.display();
  190. }
  191.  
  192. void testdrawcircle(void) {
  193.   for (uint8_t i=0; i<display.height(); i+=2) {
  194.     display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
  195.     display.display();
  196.   }
  197. }
  198.  
  199. void testfillrect(void) {
  200.   uint8_t color = 1;
  201.   for (uint8_t i=0; i<display.height()/2; i+=3) {
  202.     // alternate colors
  203.     display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
  204.     display.display();
  205.     color++;
  206.   }
  207. }
  208.  
  209. void testdrawtriangle(void) {
  210.   for (uint16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
  211.     display.drawTriangle(display.width()/2, display.height()/2-i,
  212.                      display.width()/2-i, display.height()/2+i,
  213.                      display.width()/2+i, display.height()/2+i, WHITE);
  214.     display.display();
  215.   }
  216. }
  217.  
  218. void testfilltriangle(void) {
  219.   uint8_t color = WHITE;
  220.   for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
  221.     display.fillTriangle(display.width()/2, display.height()/2-i,
  222.                      display.width()/2-i, display.height()/2+i,
  223.                      display.width()/2+i, display.height()/2+i, WHITE);
  224.     if (color == WHITE) color = BLACK;
  225.     else color = WHITE;
  226.     display.display();
  227.   }
  228. }
  229.  
  230. void testdrawroundrect(void) {
  231.   for (uint8_t i=0; i<display.height()/3-2; i+=2) {
  232.     display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
  233.     display.display();
  234.   }
  235. }
  236.  
  237. void testfillroundrect(void) {
  238.   uint8_t color = WHITE;
  239.   for (uint8_t i=0; i<display.height()/3-2; i+=2) {
  240.     display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
  241.     if (color == WHITE) color = BLACK;
  242.     else color = WHITE;
  243.     display.display();
  244.   }
  245. }
  246.    
  247. void testdrawrect(void) {
  248.   for (uint8_t i=0; i<display.height()/2; i+=2) {
  249.     display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
  250.     display.display();
  251.   }
  252. }
  253.  
  254. void testdrawline() {  
  255.   for (uint8_t i=0; i<display.width(); i+=4) {
  256.     display.drawLine(0, 0, i, display.height()-1, WHITE);
  257.     display.display();
  258.   }
  259.   for (uint8_t i=0; i<display.height(); i+=4) {
  260.     display.drawLine(0, 0, display.width()-1, i, WHITE);
  261.     display.display();
  262.   }
  263.   delay(250);
  264.  
  265.   display.clearDisplay();
  266.   for (uint8_t i=0; i<display.width(); i+=4) {
  267.     display.drawLine(0, display.height()-1, i, 0, WHITE);
  268.     display.display();
  269.   }
  270.   for (int8_t i=display.height()-1; i>=0; i-=4) {
  271.     display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
  272.     display.display();
  273.   }
  274.   delay(250);
  275.  
  276.   display.clearDisplay();
  277.   for (int8_t i=display.width()-1; i>=0; i-=4) {
  278.     display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
  279.     display.display();
  280.   }
  281.   for (int8_t i=display.height()-1; i>=0; i-=4) {
  282.     display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
  283.     display.display();
  284.   }
  285.   delay(250);
  286.  
  287.   display.clearDisplay();
  288.   for (uint8_t i=0; i<display.height(); i+=4) {
  289.     display.drawLine(display.width()-1, 0, 0, i, WHITE);
  290.     display.display();
  291.   }
  292.   for (uint8_t i=0; i<display.width(); i+=4) {
  293.     display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
  294.     display.display();
  295.   }
  296.   delay(250);
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement