Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "SPI.h"
  2. #include "DMD2.h"
  3. #include "TimerOne.h"  
  4. #include "Arial114.h"
  5.  
  6. /*-----------------------------------------------------------------------
  7.  Only 7 of the 16 pins on the DMD are actually used:
  8.  GND - Hopefully obvious
  9.  nOE - Enable / Disable ALL the LEDs on the DMD
  10.  A - A&B pins select which 1/4 of the DMD is selected
  11.  B - A&B pins select which 1/4 of the DMD is selected
  12.  CLK - Used to clock each pixel into the DMD shift registers
  13.  SCLK - Latches the current content of the DMD shift registers
  14.  R - The raw pixel data stream (NB: The 'R' stands for RED. You might notice an unused 'G' pin which is for Green on a HUB12 connector)
  15.  --------
  16.  nOE(D9) |1     2| A(D6)
  17.  Gnd     |3     4| B(D7)
  18.  Gnd     |5     6| C
  19.  Gnd     |6     8| CLK(D13)
  20.  Gnd     |7    10| SCLK(D8)
  21.  Gnd     |11   12| R(D11)
  22.  Gnd     |13   14| G
  23.  Gnd     |15   16| D
  24.  ---------
  25.  -----------------------------------------------------------------------*/
  26. //Fire up the DMD library as dmd
  27. #define DISPLAYS_ACROSS 1
  28. #define DISPLAYS_DOWN 1
  29. DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
  30.  
  31. /*-----------------------------------------------------------------------
  32.  Interrupt handler for Timer1 (TimerOne) driven DMD refresh scanning, this gets
  33.  called at the period set in Timer1.initialize();
  34.  -----------------------------------------------------------------------*/
  35. void ScanDMD()
  36. {
  37.   dmd.scanDisplayBySPI();
  38. }
  39.  
  40. const byte PanelWidth = 32;
  41. const byte MaxStringLength = 5;
  42. char CharBuf[MaxStringLength + 1];
  43.  
  44. void setup() {
  45.   //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
  46.   Timer1.initialize( 5000 );          //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
  47.   Timer1.attachInterrupt( ScanDMD );  //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
  48.   dmd.selectFont(Arial114);
  49. }
  50.  
  51. void loop() {
  52.   String myString;
  53.   myString= "0"; center_theDisplay(myString); delay(1000);
  54.   myString= "12"; center_theDisplay(myString); delay(1000);  
  55.   myString= "345"; center_theDisplay(myString); delay(1000);
  56.   myString= "678."; center_theDisplay(myString); delay(1000);
  57. }
  58.  
  59. void center_theDisplay(String input_Str) {
  60.   byte charCount, total_charWidth, x_position;
  61.   input_Str.toCharArray(CharBuf, MaxStringLength + 1); //string to char array
  62.  
  63.   charCount=  input_Str.length();
  64.   if (charCount==0) exit;
  65.  
  66.   total_charWidth= 0;
  67.   for (byte thisChar = 0; thisChar <charCount; thisChar++) {
  68.     total_charWidth= total_charWidth + dmd.charWidth(CharBuf[thisChar]) +1; //add 1 pixel for space
  69.   }  
  70.  
  71.   total_charWidth= total_charWidth -1; //no space for last letter
  72.   x_position= (PanelWidth - total_charWidth) /2; //position(x) of first letter
  73.   dmd.clearScreen(true);
  74.  
  75.   for (byte thisChar = 0; thisChar <charCount; thisChar++) {
  76.     //dmd.drawChar(x, y,‘@', GRAPHICS_NORMAL)
  77.     dmd.drawChar( x_position,  1, CharBuf[thisChar], GRAPHICS_NORMAL );
  78.     x_position= x_position + dmd.charWidth(CharBuf[thisChar]) + 1; //position for next letter
  79.   }  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement