Advertisement
Guest User

Untitled

a guest
Jan 13th, 2022
18
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. TM1638.cpp - Library implementation for TM1638.
  3.  
  4. Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
  5.  
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the version 3 GNU General Public License as
  8. published by the Free Software Foundation.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #if defined(ARDUINO) && ARDUINO >= 100
  20.     #include "Arduino.h"
  21. #else
  22.     #include "WProgram.h"
  23. #endif
  24.  
  25. #include "TM1638QYF.h"
  26. #include "string.h"
  27.  
  28. TM1638QYF::TM1638QYF(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity)
  29.     : TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity)
  30. {
  31.     // nothing else to do - calling super is enough
  32. }
  33.  
  34. void TM1638QYF::setDisplay(const byte values[], unsigned int length)
  35. {
  36.   for (int i = 0; i < displays; i++) {
  37.     int val = 0;
  38.  
  39.     for (int j = 0; j < length; j++) {
  40.       val |= ((values[j] >> i) & 1) << (displays - j - 1);
  41.     }
  42.    
  43.     sendData(i << 1, val);
  44.   }
  45. }
  46.  
  47. void TM1638QYF::clearDisplay()
  48. {
  49.     setDisplay(NULL, 0);
  50. }
  51.  
  52. void TM1638QYF::setDisplayToString(const char* string, const word dots, const byte ignored, const byte font[])
  53. {
  54.     byte values[displays];
  55.     boolean done = false;
  56.  
  57.     memset(values, 0, displays * sizeof(byte));
  58.  
  59.     for (int i = 0; i < displays; i++) {
  60.         if (!done && string[i] != '\0') {
  61.           values[i] = font[string[i] - 32] | (((dots >> (displays - i - 1)) & 1) << 7);
  62.         } else {
  63.           done = true;
  64.           values[i] = (((dots >> (displays - i - 1)) & 1) << 7);
  65.         }
  66.     }
  67.  
  68.     setDisplay(values, displays);
  69. }
  70.  
  71. void TM1638QYF::setDisplayToString(String string, const word dots, const byte ignored, const byte font[])
  72. {
  73.   byte values[displays];
  74.   int stringLength = string.length();
  75.  
  76.   memset(values, 0, displays * sizeof(byte));
  77.  
  78.   for (int i = 0; i < displays; i++) {
  79.     if (i < stringLength) {
  80.       values[i] = font[string.charAt(i) - 32] | (((dots >> (displays - i - 1)) & 1) << 7);
  81.     } else {
  82.       values[i] = (((dots >> (displays - i - 1)) & 1) << 7);
  83.     }
  84.   }
  85.  
  86.   setDisplay(values, displays);
  87. }
  88.  
  89. void TM1638QYF::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
  90. {
  91.     byte values[displays];
  92.  
  93.     memset(values, 0, displays * sizeof(byte));
  94.  
  95.     for (int i = 0; i < displays; i++) {
  96.         values[i] = numberFont[(number >> (displays - i - 1)) & 1] | (((dots >> (displays - i - 1)) & 1) << 7);
  97.     }
  98.  
  99.     setDisplay(values, displays);
  100. }
  101.  
  102. void TM1638QYF::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros,
  103.     const byte numberFont[])
  104. {
  105.     char values[displays + 1];
  106.  
  107.     snprintf(values, displays + 1, leadingZeros ? "%08X" : "%X", number); // ignores display size
  108.  
  109.     setDisplayToString(values, dots, 0, numberFont);
  110. }
  111.  
  112. void TM1638QYF::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros,
  113.     const byte numberFont[])
  114. {
  115.     char values[displays + 1];
  116.  
  117.     snprintf(values, displays + 1, leadingZeros ? "%08ld" : "%ld", number); // ignores display size
  118.  
  119.     Serial.println(values);
  120.    
  121.     setDisplayToString(values, dots, 0, numberFont);
  122. }
  123.  
  124. void TM1638QYF::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros,
  125.         const byte numberFont[])
  126. {
  127.     char values[displays + 1];
  128.  
  129.     snprintf(values, displays + 1, leadingZeros ? "%08d" : "%d", number); // ignores display size
  130.  
  131.     setDisplayToString(values, dots, 0, numberFont);
  132. }
  133.  
  134. word TM1638QYF::getButtons(void)
  135. {
  136.   word keys = 0;
  137.  
  138.   digitalWrite(strobePin, LOW);
  139.   send(0x42); // B01000010 Read the key scan data
  140.   for (int i = 0; i < 4; i++) {
  141.       byte rec = receive();
  142.  
  143.       rec = (((rec & B1000000) >> 3 | (rec & B100)) >> 2) | (rec & B100000) | (rec & B10) << 3;
  144.  
  145.       keys |= ((rec & 0x000F) << (i << 1)) | (((rec & 0x00F0) << 4) << (i << 1));
  146.   }
  147.   digitalWrite(strobePin, HIGH);
  148.  
  149.   return keys;
  150. }
  151.  
  152.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement