SHARE
TWEET

Untitled

a guest Apr 17th, 2017 14 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <OneButton.h>
  4. #include <MFRC522.h>
  5. #include <stdlib.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8.  
  9. // RFID stuff
  10. #define prog_char char PROGMEM
  11. #define SS_PIN 10
  12. #define RST_PIN 5
  13. MFRC522 mfrc522(SS_PIN, RST_PIN);
  14. MFRC522::MIFARE_Key key;
  15.  
  16. int block = 60;
  17. int block2 = 61;
  18. byte blockcontent[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  19. byte readbackblock[18];
  20. boolean bReadCard = false;
  21. boolean tagValues = false;
  22. boolean rct = false;
  23.  
  24. // OLED stuff
  25. #define OLED_RESET 4
  26. Adafruit_SSD1306 display(OLED_RESET);
  27.  
  28. // control stuff
  29. OneButton actionButton(A1, true); //between A1 and ND
  30. OneButton valueButton(A2, true); //between A2 and ND
  31. int multi = 1;
  32. int screen = 0;
  33.  
  34. // Values
  35. unsigned int uBatteryID;
  36. unsigned int uCapacity;
  37. unsigned int uCycles;
  38. unsigned int uCells;
  39. unsigned int uCcount;
  40.  
  41. static const unsigned char PROGMEM RCThoughts[] = {
  42.   0x00, 0x1F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.   0x00, 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.   0x00, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.   0x01, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46.   0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47.   0x07, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.   0x0F, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.   0x0E, 0x03, 0xC0, 0xF0, 0x07, 0xFB, 0x0C, 0x7C, 0x61, 0x8F, 0xC6, 0x1B, 0xFC, 0xFC, 0x07, 0xF0,
  50.   0x1E, 0x01, 0xC0, 0x78, 0x07, 0xFB, 0x0C, 0xFE, 0x61, 0x9F, 0xE6, 0x1B, 0xFD, 0xFC, 0x04, 0x10,
  51.   0x1E, 0x31, 0x8E, 0x78, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x61, 0x8E, 0x04, 0x10,
  52.   0x3E, 0x39, 0x8E, 0x78, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x61, 0x86, 0x06, 0x30,
  53.   0x3E, 0x39, 0x8E, 0x7C, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x61, 0xC0, 0x00, 0x00,
  54.   0x3E, 0x31, 0x8F, 0xFC, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x06, 0x18, 0x60, 0xE0, 0x07, 0xF0,
  55.   0x3E, 0x03, 0x8F, 0xFC, 0xF8, 0xC3, 0xFC, 0xC6, 0x61, 0x98, 0x07, 0xF8, 0x60, 0x70, 0x04, 0x10,
  56.   0x3E, 0x07, 0x8F, 0xFC, 0xF8, 0xC3, 0xFC, 0xC6, 0x61, 0x99, 0xE7, 0xF8, 0x60, 0x38, 0x04, 0x10,
  57.   0x3E, 0x23, 0x8F, 0xFC, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x99, 0xE6, 0x18, 0x60, 0x1C, 0x07, 0xE0,
  58.   0x3E, 0x23, 0x8E, 0x7C, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x60, 0x0E, 0x00, 0x00,
  59.   0x3E, 0x33, 0x8E, 0x78, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x61, 0x86, 0x07, 0xF0,
  60.   0x3E, 0x31, 0x8E, 0x78, 0x00, 0xC3, 0x0C, 0xC6, 0x61, 0x98, 0x66, 0x18, 0x61, 0xC6, 0x01, 0xC0,
  61.   0x1E, 0x31, 0x8E, 0x78, 0x00, 0xC3, 0x0C, 0xC6, 0x73, 0x98, 0xE6, 0x18, 0x61, 0xC6, 0x07, 0x00,
  62.   0x1E, 0x38, 0xC0, 0x70, 0x00, 0xC3, 0x0C, 0xFE, 0x3F, 0x1F, 0xE6, 0x18, 0x60, 0xFE, 0x61, 0xC0,
  63.   0x0E, 0x38, 0xE0, 0xF0, 0x00, 0xC3, 0x0C, 0x7C, 0x3F, 0x0F, 0x66, 0x18, 0x60, 0x7C, 0x67, 0xF0,
  64.   0x0F, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65.   0x07, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66.   0x03, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67.   0x01, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68.   0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69.   0x00, 0x1F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  70. };
  71.  
  72. void setup()   {
  73.   Serial.begin(9600);
  74.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  75.  
  76.   // Show RC-Thoughts logo in boot
  77.   showScreenLogo();
  78.  
  79.   // RFID
  80.   SPI.begin();
  81.   mfrc522.PCD_Init();
  82.   for (byte i = 0; i < 6; i++) {
  83.     key.keyByte[i] = 0xFF;
  84.   }
  85.   uBatteryID = 0;
  86.   uCapacity = 0;
  87.   uCycles = 0;
  88.   uCells = 0;
  89.   uCcount = 0;
  90.  
  91.   // Action button
  92.   actionButton.attachClick(clickAction);
  93.   actionButton.attachLongPressStart(longPressStartAction);
  94.  
  95.   // Value button
  96.   valueButton.attachClick(clickValue);
  97.   valueButton.attachLongPressStart(longPressStartValue);
  98. }
  99.  
  100. void loop() {
  101.  
  102.   // Monitor buttons:
  103.   actionButton.tick();
  104.   valueButton.tick();
  105.  
  106.   // RFID Check for tags
  107.   if (! bReadCard) {
  108.     if ( ! mfrc522.PICC_IsNewCardPresent()) {
  109.       return;
  110.     }
  111.  
  112.     if ( ! mfrc522.PICC_ReadCardSerial()) {
  113.       return;
  114.     }
  115.  
  116.     // Check if we are using RC-Thoughts tag
  117.     byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  118.     if (piccType == MFRC522::PICC_TYPE_MIFARE_1K) {
  119.       rct = true;
  120.     }
  121.   }
  122.  
  123.   if ( bReadCard && rct ) {
  124.     mfrc522.PCD_StopCrypto1();
  125.     mfrc522.PICC_IsNewCardPresent();
  126.     if (! mfrc522.PICC_IsNewCardPresent()) {
  127.       rct = false;
  128.       tagValues = false;
  129.       bReadCard = false;
  130.       uBatteryID = 0;
  131.       uCapacity = 0;
  132.       uCycles = 0;
  133.       uCells = 0;
  134.       uCcount = 0;
  135.       showScreenLogo();
  136.       screen = 0;
  137.       multi = 1;
  138.     }
  139.   }
  140.  
  141.   // RC-Thoughts Tag Process START
  142.   if (! bReadCard && rct) {
  143.     readBlock(block, readbackblock);
  144.     uBatteryID = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  145.     uCapacity = ((readbackblock[2] & 0xff) << 8) | readbackblock[3];
  146.     uCycles = ((readbackblock[4] & 0xff) << 8) | readbackblock[5];
  147.     uCells = ((readbackblock[6] & 0xff) << 8) | readbackblock[7];
  148.     readBlock(block2, readbackblock);
  149.     uCcount = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  150.     Serial.println("RC-Thoughts Info");
  151.     Serial.print("ID: "); Serial.println(uBatteryID);
  152.     Serial.print("Capacity: "); Serial.println(uCapacity);
  153.     Serial.print("Cycles: "); Serial.println(uCycles);
  154.     Serial.print("Cells: "); Serial.println(uCells);
  155.     Serial.print("C-Value: "); Serial.println(uCcount);
  156.     Serial.println("");
  157.     bReadCard = true;
  158.     showScreenData();
  159.     screen = 0;
  160.  
  161.     if (! tagValues)
  162.     {
  163.       tagValues = true;
  164.     }
  165.   } // RC-Thoughts Tag Process END
  166. } // Loop END
  167.  
  168. void clickAction() {
  169.   if (multi == 1) {
  170.     multi = 10;
  171.     updateScreen();
  172.   } else if (multi == 10) {
  173.     multi = 100;
  174.     updateScreen();
  175.   } else if (multi == 100) {
  176.     multi = 1000;
  177.     updateScreen();
  178.   } else if (multi == 1000) {
  179.     multi = 1;
  180.     updateScreen();
  181.   }
  182. }
  183.  
  184. void longPressStartAction() {
  185.   // if long press go to next settings screen
  186.   screen++;
  187.   if (screen == 6) {
  188.     writeCard();
  189.   } else {
  190.     updateScreen();
  191.   }
  192. }
  193.  
  194. void clickValue() {
  195.   if (screen == 1) {
  196.     uBatteryID = uBatteryID + (1 * multi);
  197.     if (uBatteryID > 9999) {
  198.       uBatteryID = 9999;
  199.     }
  200.     showScreenID();
  201.   }
  202.   if (screen == 2) {
  203.     uCells = uCells + (1 * multi);
  204.     if (uCells > 99) {
  205.       uCells = 99;
  206.     }
  207.     showScreenCells();
  208.   }
  209.   if (screen == 3) {
  210.     uCapacity = uCapacity + (1 * multi);
  211.     if (uCapacity > 65534) {
  212.       uCapacity = 65534;
  213.     }
  214.     showScreenCapacity();
  215.   }
  216.   if (screen == 4) {
  217.     uCycles = uCycles + (1 * multi);
  218.     if (uCycles > 999) {
  219.       uCycles = 999;
  220.     }
  221.     showScreenCycles();
  222.   }
  223.   if (screen == 5) {
  224.     uCcount = uCcount + (1 * multi);
  225.     if (uCcount > 999) {
  226.       uCcount = 999;
  227.     }
  228.     showScreenCValue();
  229.   }
  230. }
  231.  
  232. void longPressStartValue() {
  233.   if (screen == 1) {
  234.     uBatteryID = 0;
  235.     showScreenID();
  236.   }
  237.   if (screen == 2) {
  238.     uCells = 0;
  239.     showScreenCells();
  240.   }
  241.   if (screen == 3) {
  242.     uCapacity = 0;
  243.     showScreenCapacity();
  244.   }
  245.   if (screen == 4) {
  246.     uCycles = 0;
  247.     showScreenCycles();
  248.   }
  249.   if (screen == 5) {
  250.     uCcount = 0;
  251.     showScreenCValue();
  252.   }
  253. }
  254.  
  255. void updateScreen() {
  256.   if (screen == 0) {
  257.     showScreenData();
  258.   }
  259.   if (screen == 1) {
  260.     showScreenID();
  261.   }
  262.   if (screen == 2) {
  263.     showScreenCells();
  264.   }
  265.   if (screen == 3) {
  266.     showScreenCapacity();
  267.   }
  268.   if (screen == 4) {
  269.     showScreenCycles();
  270.   }
  271.   if (screen == 5) {
  272.     showScreenCValue();
  273.   }
  274. }
  275.  
  276. void showScreenLogo() {
  277.   display.clearDisplay();
  278.   display.drawBitmap(0, 2, RCThoughts, 128, 28, 1);
  279.   display.display();
  280. }
  281.  
  282. // Screen 0
  283. void showScreenData() {
  284.   display.clearDisplay();
  285.   display.setTextSize(1);
  286.   display.setCursor(0, 0);
  287.   display.setTextColor(WHITE);
  288.   display.print("ID: "); display.print(uBatteryID);
  289.   display.print(" Cells: "); display.print(uCells); display.println("S");
  290.   display.print("Capacity: "); display.print(uCapacity); display.println("mAh");
  291.   display.print("Cycles: "); display.println(uCycles);
  292.   display.print("C-Value: "); display.print(uCcount); display.println("C");
  293.   display.display();
  294.   screen = 0;
  295.   }
  296.  
  297. // Screen 1
  298. void showScreenID() {
  299.   display.clearDisplay();
  300.   display.setTextSize(1);
  301.   display.setCursor(34, 0);
  302.   display.setTextColor(WHITE);
  303.   display.print("Battery ID");
  304.   display.setTextSize(2);
  305.   if (uBatteryID < 10) {
  306.     display.setCursor(60, 9);
  307.   } else {
  308.     if (9 < uBatteryID < 99) {
  309.       display.setCursor(53, 9);
  310.     } else {
  311.       if (9 < uBatteryID < 999) {
  312.         display.setCursor(34, 9);
  313.       }
  314.     }
  315.   }
  316.   display.print(uBatteryID);
  317.   display.setTextSize(1);
  318.   display.setCursor(0, 25);
  319.   if (multi == 1) {
  320.     display.println("x1");
  321.   }
  322.   if (multi == 10) {
  323.     display.println("x10");
  324.   }
  325.   if (multi == 100) {
  326.     display.println("x100");
  327.   }
  328.   if (multi == 1000) {
  329.     display.println("x1000");
  330.   }
  331.   display.display();
  332. }
  333.  
  334. // Screen 2
  335. void showScreenCells() {
  336.   display.clearDisplay();
  337.   display.setTextSize(1);
  338.   display.setCursor(20, 0);
  339.   display.setTextColor(WHITE);
  340.   display.print("Battery Cells");
  341.   display.setTextSize(2);
  342.   if (uCells < 10) {
  343.     display.setCursor(52, 9);
  344.   } else {
  345.     if (9 < uCells < 99) {
  346.       display.setCursor(47, 9);
  347.     }
  348.   }
  349.   display.print(uCells);
  350.   display.print("S");
  351.   display.setTextSize(1);
  352.   display.setCursor(0, 25);
  353.   if (multi == 1) {
  354.     display.println("x1");
  355.   }
  356.   if (multi == 10) {
  357.     display.println("x10");
  358.   }
  359.   if (multi == 100) {
  360.     display.println("x100");
  361.   }
  362.   if (multi == 1000) {
  363.     display.println("x1000");
  364.   }
  365.   display.display();
  366. }
  367.  
  368. // Screen 3
  369. void showScreenCapacity() {
  370.   display.clearDisplay();
  371.   display.setTextSize(1);
  372.   display.setCursor(20, 0);
  373.   display.setTextColor(WHITE);
  374.   display.print("Battery Capacity");
  375.   display.setTextSize(2);
  376.   if (uCapacity < 10) {
  377.     display.setCursor(48, 9);
  378.   } else {
  379.     if (uCapacity > 9 && uCapacity < 99) {
  380.       display.setCursor(41, 9);
  381.     } else {
  382.       if (uCapacity > 99 && uCapacity < 999) {
  383.         display.setCursor(36, 9);
  384.       } else {
  385.         if (uCapacity > 999 && uCapacity < 9999) {
  386.           display.setCursor(28, 9);
  387.         } else {
  388.           if (uCapacity > 9999 && uCapacity < 99999) {
  389.             display.setCursor(20, 9);
  390.           }
  391.         }
  392.       }
  393.     }
  394.   }
  395.   display.print(uCapacity);
  396.   display.print("mAh");
  397.   display.setTextSize(1);
  398.   display.setCursor(0, 25);
  399.   if (multi == 1) {
  400.     display.println("x1");
  401.   }
  402.   if (multi == 10) {
  403.     display.println("x10");
  404.   }
  405.   if (multi == 100) {
  406.     display.println("x100");
  407.   }
  408.   if (multi == 1000) {
  409.     display.println("x1000");
  410.   }
  411.   display.display();
  412. }
  413.  
  414. // Screen 4
  415. void showScreenCycles() {
  416.   display.clearDisplay();
  417.   display.setTextSize(1);
  418.   display.setCursor(20, 0);
  419.   display.setTextColor(WHITE);
  420.   display.print("Battery Cycles");
  421.   display.setTextSize(2);
  422.   if (uCycles < 10) {
  423.     display.setCursor(60, 9);
  424.   } else {
  425.     if (9 < uCycles < 99) {
  426.       display.setCursor(54, 9);
  427.     } else {
  428.       if (99 < uCycles < 999) {
  429.         display.setCursor(40, 9);
  430.       }
  431.     }
  432.   }
  433.   display.print(uCycles);
  434.   display.setTextSize(2);
  435.   display.setTextSize(1);
  436.   display.setCursor(0, 25);
  437.   if (multi == 1) {
  438.     display.println("x1");
  439.   }
  440.   if (multi == 10) {
  441.     display.println("x10");
  442.   }
  443.   if (multi == 100) {
  444.     display.println("x100");
  445.   }
  446.   if (multi == 1000) {
  447.     display.println("x1000");
  448.   }
  449.   display.display();
  450. }
  451.  
  452. // Screen 5
  453. void showScreenCValue() {
  454.   display.clearDisplay();
  455.   display.setTextSize(1);
  456.   display.setCursor(20, 0);
  457.   display.setTextColor(WHITE);
  458.   display.print("Battery C-Value");
  459.   display.setTextSize(2);
  460.   if (uCcount < 10) {
  461.     display.setCursor(55, 9);
  462.   } else {
  463.     if (9 < uCcount < 99) {
  464.       display.setCursor(48, 9);
  465.     }
  466.   }
  467.   display.print(uCcount);
  468.   display.print("C");
  469.   display.setTextSize(2);
  470.   display.setTextSize(1);
  471.   display.setCursor(0, 25);
  472.   if (multi == 1) {
  473.     display.println("x1");
  474.   }
  475.   if (multi == 10) {
  476.     display.println("x10");
  477.   }
  478.   if (multi == 100) {
  479.     display.println("x100");
  480.   }
  481.   if (multi == 1000) {
  482.     display.println("x1000");
  483.   }
  484.   display.display();
  485. }
  486.  
  487. // Screen 6
  488. void showScreenWritten() {
  489.   display.clearDisplay();
  490.   display.setTextSize(2);
  491.   display.setTextColor(WHITE);
  492.   display.setCursor(26, 8);
  493.   display.print("Tag OK!");
  494.   display.display();
  495.   delay(1000);
  496.   screen = 0;
  497.   showScreenData();
  498. }
  499.  
  500. void writeCard() {
  501.   // write tag
  502.   mfrc522.PICC_ReadCardSerial();
  503.   unsigned char high = (byte)(uBatteryID >> 8);
  504.   unsigned char low  = (byte)uBatteryID;
  505.   blockcontent[0] = high; blockcontent[1] = low;
  506.   high = (byte)(uCapacity >> 8);
  507.   low  = (byte)uCapacity ;
  508.   blockcontent[2] = high; blockcontent[3] = low;
  509.   high = (byte)(uCycles >> 8);
  510.   low  = (byte)uCycles;
  511.   blockcontent[4] = high; blockcontent[5] = low;
  512.   high = (byte)(uCells >> 8);
  513.   low  = (byte)uCells;
  514.   blockcontent[6] = high; blockcontent[7] = low;
  515.   writeBlock(block, blockcontent);
  516.   high = (byte)(uCcount >> 8);
  517.   low  = (byte)uCcount;
  518.   blockcontent[0] = high; blockcontent[1] = low;
  519.   writeBlock(block2, blockcontent);
  520.   tagValues = false;
  521.   readBlock(block, readbackblock);
  522.   uBatteryID = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  523.   uCapacity = ((readbackblock[2] & 0xff) << 8) | readbackblock[3];
  524.   uCycles = ((readbackblock[4] & 0xff) << 8) | readbackblock[5];
  525.   uCells = ((readbackblock[6] & 0xff) << 8) | readbackblock[7];
  526.   readBlock(block2, readbackblock);
  527.   uCcount = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  528.   bReadCard = true;
  529.   if (! tagValues)
  530.   {
  531.     tagValues = true;
  532.   }
  533.   showScreenWritten();
  534. }
RAW Paste Data
Top