Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.88 KB | None | 0 0
  1. /*
  2.    --------------------------------------------------------
  3.           Jeti RFID-Battery version 1.5
  4.    --------------------------------------------------------
  5.  
  6.     Tero Salminen RC-Thoughts.com 2016 www.rc-thoughts.com
  7.  
  8.     Anti-metal Mifare 1k tag used - Blocks 4 & 5
  9.  
  10.     - Improved performance from previous solutions
  11.     - Compatibility with Jeti R- and REX-receivers improved
  12.     - RFID-tag Writing via Jetibox from transmitter added
  13.     - Accepts change of RFID-tag without power-cycle
  14.     - Initialize sensor to Transmitter without RFID-tag present
  15.     - Compatible with DC/DS-14/16/24
  16.     - Compatible with Revo Bump and Robbe BID systems
  17.     - Code housekeeping (Poorly...)
  18.  
  19.     Big thanks to RC-Groups forum-members who lent not only
  20.     Revo Bump tags but also an Revo Bump controller!
  21.     Michael and Warren, thank you!
  22.    --------------------------------------------------------
  23.      ALWAYS test functions thoroughly before use!
  24.    --------------------------------------------------------
  25.     Huge thanks to pioneering work of
  26.     Alastair Cormack alastair.cormack@gmail.com
  27.    --------------------------------------------------------
  28.     This is made possible by the original work of:
  29.  
  30.     Mav2Duplex by DevFor8.com, info@devfor8.com
  31.     RFID by Dr.Leong www..b2cqshop.com
  32.     Miguel Balboa www.circuitito.com Jan 2012
  33.     Søren Thing Andersen fall of 2013
  34.     Rudy Schlaf  www.makecourse.com
  35.    --------------------------------------------------------
  36.     Shared under MIT-license by Tero Salminen 2016
  37.    --------------------------------------------------------
  38. */
  39.  
  40. #include <EEPROM.h>
  41. #include <SPI.h>
  42. #include <MFRC522.h>
  43. #include <stdlib.h>
  44. #include <SoftwareSerialJeti.h>
  45. #include <JETI_EX_SENSOR.h>
  46.  
  47. #define SS_PIN 10
  48. #define RST_PIN 5
  49. MFRC522 mfrc522(SS_PIN, RST_PIN);
  50. MFRC522::MIFARE_Key key;
  51.  
  52. #define prog_char char PROGMEM
  53. #define GETCHAR_TIMEOUT_ms 30
  54.  
  55. #ifndef JETI_RX
  56. #define JETI_RX 3
  57. #endif
  58.  
  59. #ifndef JETI_TX
  60. #define JETI_TX 4
  61. #endif
  62.  
  63. unsigned int uBatteryID;
  64. unsigned int uCapacity;
  65. unsigned int uCycles;
  66. unsigned int uCells;
  67. unsigned int uCcount;
  68. unsigned int uPack;
  69. unsigned int wBatteryID;
  70. unsigned int wCapacity;
  71. unsigned int wCycles;
  72. unsigned int wCells;
  73. unsigned int wCcount;
  74. String uName = "N/A";
  75. int uLoopCount = 0;
  76.  
  77. #define ITEMNAME_1 F("ID")
  78. #define ITEMTYPE_1 F("")
  79. #define ITEMVAL_1 &uBatteryID
  80.  
  81. #define ITEMNAME_2 F("Capacity")
  82. #define ITEMTYPE_2 F("mAh")
  83. #define ITEMVAL_2 &uCapacity
  84.  
  85. #define ITEMNAME_3 F("Cycles")
  86. #define ITEMTYPE_3 F("")
  87. #define ITEMVAL_3 &uCycles
  88.  
  89. #define ITEMNAME_4 F("Cells")
  90. #define ITEMTYPE_4 F("")
  91. #define ITEMVAL_4 &uCells
  92.  
  93. #define ITEMNAME_5 F("C-Value")
  94. #define ITEMTYPE_5 F("")
  95. #define ITEMVAL_5 &uCcount
  96.  
  97. #define ABOUT_1 F(" RCT Jeti Tools")
  98. #define ABOUT_2 F("  RFID-Battery")
  99.  
  100. SoftwareSerial JetiSerial(JETI_RX, JETI_TX);
  101.  
  102. void JetiUartInit()
  103. {
  104.   JetiSerial.begin(9700);
  105. }
  106.  
  107. void JetiTransmitByte(unsigned char data, boolean setBit9)
  108. {
  109.   JetiSerial.set9bit = setBit9;
  110.   JetiSerial.write(data);
  111.   JetiSerial.set9bit = 0;
  112. }
  113.  
  114. unsigned char JetiGetChar(void)
  115. {
  116.   unsigned long time = millis();
  117.   while ( JetiSerial.available()  == 0 )
  118.   {
  119.     if (millis() - time >  GETCHAR_TIMEOUT_ms)
  120.       return 0; // return, if timout occures
  121.   }
  122.   int read = -1;
  123.   if (JetiSerial.available() > 0 )
  124.   {
  125.     read = JetiSerial.read();
  126.   }
  127.   long wait = (millis() - time) - GETCHAR_TIMEOUT_ms;
  128.   if (wait > 0)
  129.     delay(wait);
  130.   return read;
  131. }
  132.  
  133. char * floatToString(char * outstr, float value, int places, int minwidth = 0) {
  134.   int digit;
  135.   float tens = 0.1;
  136.   int tenscount = 0;
  137.   int i;
  138.   float tempfloat = value;
  139.   int c = 0;
  140.   int charcount = 1;
  141.   int extra = 0;
  142.   float d = 0.5;
  143.   if (value < 0)
  144.     d *= -1.0;
  145.   for (i = 0; i < places; i++)
  146.     d /= 10.0;
  147.   tempfloat +=  d;
  148.   if (value < 0)
  149.     tempfloat *= -1.0;
  150.   while ((tens * 10.0) <= tempfloat) {
  151.     tens *= 10.0;
  152.     tenscount += 1;
  153.   }
  154.   if (tenscount > 0)
  155.     charcount += tenscount;
  156.   else
  157.     charcount += 1;
  158.   if (value < 0)
  159.     charcount += 1;
  160.   charcount += 1 + places;
  161.   minwidth += 1; // both count the null final character
  162.   if (minwidth > charcount) {
  163.     extra = minwidth - charcount;
  164.     charcount = minwidth;
  165.   }
  166.   if (value < 0)
  167.     outstr[c++] = '-';
  168.   if (tenscount == 0)
  169.     outstr[c++] = '0';
  170.   for (i = 0; i < tenscount; i++) {
  171.     digit = (int) (tempfloat / tens);
  172.     itoa(digit, &outstr[c++], 10);
  173.     tempfloat = tempfloat - ((float)digit * tens);
  174.     tens /= 10.0;
  175.   }
  176.   if (places > 0)
  177.     outstr[c++] = '.';
  178.   for (i = 0; i < places; i++) {
  179.     tempfloat *= 10.0;
  180.     digit = (int) tempfloat;
  181.     itoa(digit, &outstr[c++], 10);
  182.     tempfloat = tempfloat - (float) digit;
  183.   }
  184.   if (extra > 0 ) {
  185.     for (int i = 0; i < extra; i++) {
  186.       outstr[c++] = ' ';
  187.     }
  188.   }
  189.   outstr[c++] = '\0';
  190.   return outstr;
  191. }
  192.  
  193. JETI_Box_class JB;
  194.  
  195. unsigned char SendFrame()
  196. {
  197.   boolean bit9 = false;
  198.   for (int i = 0 ; i < JB.frameSize ; i++ )
  199.   {
  200.     if (i == 0)
  201.       bit9 = false;
  202.     else if (i == JB.frameSize - 1)
  203.       bit9 = false;
  204.     else if (i == JB.middle_bit9)
  205.       bit9 = false;
  206.     else
  207.       bit9 = true;
  208.     JetiTransmitByte(JB.frame[i], bit9);
  209.   }
  210. }
  211. unsigned char DisplayFrame()
  212. {
  213.   for (int i = 0 ; i < JB.frameSize ; i++ )
  214.   {
  215.   }
  216. }
  217.  
  218. uint8_t frame[10];
  219. short value = 27;
  220. #define MAX_SCREEN 9     //Jetibox screens
  221. #define MAX_CONFIG 1     //Jetibox configurations
  222. #define COND_LES_EQUAL 1
  223. #define COND_MORE_EQUAL 2
  224.  
  225. void setup()
  226. {
  227.   Serial.begin(9600);
  228.   SPI.begin();
  229.   mfrc522.PCD_Init();
  230.   //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); // If you have reading distance of 2cm+ use this NOTE Kills on-touch reading!
  231.   Serial.println("Ready!");
  232.   for (byte i = 0; i < 6; i++) {
  233.     key.keyByte[i] = 0xFF;
  234.   }
  235.   analogReference(EXTERNAL);
  236.   pinMode(13, OUTPUT);
  237.   digitalWrite(13, HIGH);
  238.   pinMode(JETI_RX, OUTPUT);
  239.  
  240.   JetiUartInit();
  241.   JB.JetiBox(ABOUT_1, ABOUT_2);
  242.   JB.Init(F("RFID-Sensor"));
  243.   JB.addData(ITEMNAME_1, ITEMTYPE_1);
  244.   JB.addData(ITEMNAME_2, ITEMTYPE_2);
  245.   JB.addData(ITEMNAME_3, ITEMTYPE_3);
  246.   JB.addData(ITEMNAME_4, ITEMTYPE_4);
  247.   JB.addData(ITEMNAME_5, ITEMTYPE_5);
  248.   JB.setValue(1, ITEMVAL_1);
  249.   JB.setValue(2, ITEMVAL_2);
  250.   JB.setValue(3, ITEMVAL_3);
  251.   JB.setValue(4, ITEMVAL_4);
  252.   JB.setValue(5, ITEMVAL_5);
  253.   do {
  254.     JB.createFrame(1);
  255.     SendFrame();
  256.     delay(GETCHAR_TIMEOUT_ms);
  257.   }
  258.   while (sensorFrameName != 0);
  259.   digitalWrite(13, LOW);
  260.   uBatteryID = 0;
  261.   uCapacity = 0;
  262.   uCycles = 0;
  263.   uCells = 0;
  264.   uCcount = 0;
  265.   wBatteryID = 0;
  266.   wCapacity = 0;
  267.   wCycles = 0;
  268.   wCells = 0;
  269. }
  270.  
  271. int block = 4;
  272. int block2 = 5;
  273. byte blockcontent[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  274. byte readbackblock[18];
  275. int header = 0;
  276. int lastbtn = 240;
  277. int current_screen = 0;
  278. int current_config = 0;
  279. char temp[LCDMaxPos / 2];
  280. char msg_line1[LCDMaxPos / 2];
  281. char msg_line2[LCDMaxPos / 2];
  282. boolean bReadCard = false;
  283. boolean tagValues = false;
  284. boolean revo = false;
  285. boolean rct = false;
  286.  
  287. void process_screens()
  288. {
  289.   switch (current_screen)
  290.   {
  291.     case 0 : {
  292.         JB.JetiBox(ABOUT_1, ABOUT_2);
  293.         break;
  294.       }
  295.     case 1 : {
  296.         msg_line1[0] = 0; msg_line2[0] = 0;
  297.  
  298.         strcat_P((char*)&msg_line1, (prog_char*)F("#:"));
  299.         temp[0] = 0;
  300.         floatToString((char*)&temp, wBatteryID, 0);
  301.         strcat((char*)&msg_line1, (char*)&temp);
  302.  
  303.         strcat_P((char*)&msg_line2, (prog_char*)F("Cap:"));
  304.         temp[0] = 0;
  305.         floatToString((char*)&temp, wCapacity, 0);
  306.         strcat((char*)&msg_line2, (char*)&temp);
  307.  
  308.         strcat_P((char*)&msg_line1, (prog_char*)F(" Cy:"));
  309.         temp[0] = 0;
  310.         floatToString((char*)&temp, wCycles, 0);
  311.         strcat((char*)&msg_line1, (char*)&temp);
  312.  
  313.         strcat_P((char*)&msg_line2, (prog_char*)F(" Cel:"));
  314.         temp[0] = 0;
  315.         floatToString((char*)&temp, wCells, 0);
  316.         strcat((char*)&msg_line2, (char*)&temp);
  317.  
  318.         strcat_P((char*)&msg_line1, (prog_char*)F(" C:"));
  319.         temp[0] = 0;
  320.         floatToString((char*)&temp, wCcount, 0);
  321.         strcat((char*)&msg_line1, (char*)&temp);
  322.  
  323.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  324.         break;
  325.       }
  326.     case 2 : {
  327.         msg_line1[0] = 0; msg_line2[0] = 0;
  328.         strcat_P((char*)&msg_line1, (prog_char*)F("Tag ID#: "));
  329.         temp[0] = 0;
  330.         floatToString((char*)&temp, wBatteryID, 0);
  331.         strcat((char*)&msg_line1, (char*)&temp);
  332.         strcat_P((char*)&msg_line2, (prog_char*)F("Set Up/Dn Next>"));
  333.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  334.         break;
  335.       }
  336.     case 3 : {
  337.         msg_line1[0] = 0; msg_line2[0] = 0;
  338.         strcat_P((char*)&msg_line1, (prog_char*)F("Capacity: "));
  339.         temp[0] = 0;
  340.         floatToString((char*)&temp, wCapacity, 0);
  341.         strcat((char*)&msg_line1, (char*)&temp);
  342.         strcat_P((char*)&msg_line2, (prog_char*)F("Set Up/Dn Next>"));
  343.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  344.         break;
  345.       }
  346.     case 4 : {
  347.         msg_line1[0] = 0; msg_line2[0] = 0;
  348.         strcat_P((char*)&msg_line1, (prog_char*)F("Cycles: "));
  349.         temp[0] = 0;
  350.         floatToString((char*)&temp, wCycles, 0);
  351.         strcat((char*)&msg_line1, (char*)&temp);
  352.         strcat_P((char*)&msg_line2, (prog_char*)F("Set Up/Dn Next>"));
  353.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  354.         break;
  355.       }
  356.     case 5 : {
  357.         msg_line1[0] = 0; msg_line2[0] = 0;
  358.         strcat_P((char*)&msg_line1, (prog_char*)F("C-Value: "));
  359.         temp[0] = 0;
  360.         floatToString((char*)&temp, wCcount, 0);
  361.         strcat((char*)&msg_line1, (char*)&temp);
  362.         strcat_P((char*)&msg_line2, (prog_char*)F("Set Up/Dn Next>"));
  363.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  364.         break;
  365.       }
  366.     case 6 : {
  367.         msg_line1[0] = 0; msg_line2[0] = 0;
  368.         strcat_P((char*)&msg_line1, (prog_char*)F("Cells: "));
  369.         temp[0] = 0;
  370.         floatToString((char*)&temp, wCells, 0);
  371.         strcat((char*)&msg_line1, (char*)&temp);
  372.         strcat_P((char*)&msg_line2, (prog_char*)F("Set Up+Dn Next>"));
  373.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  374.         break;
  375.       }
  376.     case 7 : {
  377.         msg_line1[0] = 0; msg_line2[0] = 0;
  378.         strcat_P((char*)&msg_line1, (prog_char*)F("Save: Up and Dn"));
  379.         strcat_P((char*)&msg_line2, (prog_char*)F("Back: <"));
  380.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  381.         break;
  382.       }
  383.     case 98 : {
  384.         msg_line1[0] = 0; msg_line2[0] = 0;
  385.         strcat_P((char*)&msg_line1, (prog_char*)F("I'm not writing"));
  386.         strcat_P((char*)&msg_line2, (prog_char*)F("Revo! < to exit"));
  387.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  388.         break;
  389.       }
  390.     case 99 : {
  391.         msg_line1[0] = 0; msg_line2[0] = 0;
  392.         strcat_P((char*)&msg_line1, (prog_char*)F("Tag Written!"));
  393.         strcat_P((char*)&msg_line2, (prog_char*)F("Press < to exit"));
  394.         JB.JetiBox((char*)&msg_line1, (char*)&msg_line2);
  395.         break;
  396.       }
  397.     case MAX_SCREEN : {
  398.         JB.JetiBox(ABOUT_1, ABOUT_2);
  399.         break;
  400.       }
  401.   }
  402. }
  403.  
  404. void loop()
  405. {
  406.   if ( bReadCard && revo ) {
  407.     mfrc522.PICC_IsNewCardPresent();
  408.     if (! mfrc522.PICC_IsNewCardPresent()) {
  409.       revo = false;
  410.       bReadCard = false;
  411.     }
  412.   }
  413.  
  414.   if ( bReadCard && rct ) {
  415.     mfrc522.PCD_StopCrypto1();
  416.     mfrc522.PICC_IsNewCardPresent();
  417.     if (! mfrc522.PICC_IsNewCardPresent()) {
  418.       rct = false;
  419.       bReadCard = false;
  420.       uLoopCount = 0;
  421.     }
  422.   }
  423.  
  424.   //
  425.   if (! bReadCard) {
  426.     if ( ! mfrc522.PICC_IsNewCardPresent()) {
  427.       return;
  428.     }
  429.  
  430.     if ( ! mfrc522.PICC_ReadCardSerial()) {
  431.       return;
  432.     }
  433.  
  434.     // Determine tag used
  435.     // Check if we are using RC-Thoughts tag
  436.     MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  437.     if (piccType == MFRC522::PICC_TYPE_MIFARE_1K) {
  438.       rct = true;
  439.       revo = false;
  440.       uLoopCount = 0;
  441.     }
  442.     // Check if we are using Revo Bump-tag
  443.     if (piccType == MFRC522::PICC_TYPE_MIFARE_UL) {
  444.       rct = false;
  445.       revo = true;
  446.     }
  447.   }
  448.  
  449.   // If we are on RC-Thoughts Tag increase cycle-count after some time
  450.   if ((uLoopCount == 40) && bReadCard && rct) {
  451.     mfrc522.PICC_ReadCardSerial();
  452.     Serial.println("Writing out new cycle count");
  453.     uCycles = uCycles + 1;
  454.     unsigned char high = (byte)(uBatteryID >> 8);
  455.     unsigned char low  = (byte)uBatteryID;
  456.     blockcontent[0] = high; blockcontent[1] = low;
  457.     high = (byte)(uCapacity >> 8);
  458.     low  = (byte)uCapacity ;
  459.     blockcontent[2] = high; blockcontent[3] = low;
  460.     high = (byte)(uCycles >> 8);
  461.     low  = (byte)uCycles;
  462.     blockcontent[4] = high; blockcontent[5] = low;
  463.     high = (byte)(uCells >> 8);
  464.     low  = (byte)uCells;
  465.     blockcontent[6] = high; blockcontent[7] = low;
  466.     writeBlock(block, blockcontent);
  467.     high = (byte)(uCcount >> 8);
  468.     low  = (byte)uCcount;
  469.     blockcontent[0] = high; blockcontent[1] = low;
  470.     writeBlock(block2, blockcontent);
  471.     Serial.println("Battery name: N/A");
  472.     Serial.print("ID: "); Serial.println(uBatteryID);
  473.     Serial.print("Capacity: "); Serial.println(uCapacity);
  474.     Serial.print("Cycles: "); Serial.println(uCycles);
  475.     Serial.print("Cells: "); Serial.println(uCells);
  476.     Serial.print("C-Value: "); Serial.println(uCcount);
  477.   }
  478.  
  479.   if ((uLoopCount < 41) && bReadCard && rct) {
  480.     uLoopCount++;
  481.   }
  482.  
  483.   // RC-Thoughts Tag Process START
  484.   if (! bReadCard && rct) {
  485.     readBlock(block, readbackblock);
  486.     uBatteryID = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  487.     uCapacity = ((readbackblock[2] & 0xff) << 8) | readbackblock[3];
  488.     uCycles = ((readbackblock[4] & 0xff) << 8) | readbackblock[5];
  489.     uCells = ((readbackblock[6] & 0xff) << 8) | readbackblock[7];
  490.     readBlock(block2, readbackblock);
  491.     uCcount = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  492.     Serial.println("Battery name: N/A");
  493.     Serial.print("ID: "); Serial.println(uBatteryID);
  494.     Serial.print("Capacity: "); Serial.println(uCapacity);
  495.     Serial.print("Cycles: "); Serial.println(uCycles);
  496.     Serial.print("Cells: "); Serial.println(uCells);
  497.     Serial.print("C-Value: "); Serial.println(uCcount);
  498.     bReadCard = true;
  499.  
  500.     if (! tagValues)
  501.     {
  502.       wBatteryID = uBatteryID;
  503.       wCapacity = uCapacity;
  504.       wCycles = uCycles;
  505.       wCells = uCells;
  506.       wCcount = uCcount;
  507.     }
  508.     tagValues = true;
  509.   }
  510.   // RC-Thoughts Tag Process END
  511.  
  512.   // Revo Process START
  513.   if (! bReadCard && revo) {
  514.     // RFID-buffer definition
  515.     byte buffer[18];
  516.     byte size = sizeof(buffer);
  517.  
  518.     // Process Revo - Tag Id
  519.     mfrc522.MIFARE_Read(17, buffer, &size);
  520.     String id1 = String(buffer[4], HEX);
  521.     String id2 = String(buffer[3], HEX);
  522.     String ID = String(id1 + id2);
  523.     char idArray[5];
  524.     ID.toCharArray(idArray, sizeof(idArray));
  525.     int Id = atoi(idArray);
  526.     uBatteryID = Id;
  527.  
  528.     // Process Revo - Battery name - Not used
  529.     uName = "";
  530.     mfrc522.MIFARE_Read(22, buffer, &size);
  531.     char name1 = char(buffer[2]); uName.concat(name1);
  532.     char name2 = char(buffer[3]); uName.concat(name2);
  533.     char name3 = char(buffer[4]); uName.concat(name3);
  534.     char name4 = char(buffer[5]); uName.concat(name4);
  535.     char name5 = char(buffer[6]); uName.concat(name5);
  536.     char name6 = char(buffer[7]); uName.concat(name6);
  537.     char name7 = char(buffer[8]); uName.concat(name7);
  538.     char name8 = char(buffer[9]); uName.concat(name8);
  539.     char name9 = char(buffer[10]); uName.concat(name9);
  540.     char name10 = char(buffer[11]); uName.concat(name10);
  541.     mfrc522.MIFARE_Read(25, buffer, &size);
  542.     char name11 = char(buffer[0]); uName.concat(name11);
  543.     char name12 = char(buffer[1]); uName.concat(name12);
  544.     char name13 = char(buffer[2]); uName.concat(name13);
  545.     char name14 = char(buffer[3]); uName.concat(name14);
  546.     char name15 = char(buffer[4]); uName.concat(name15);
  547.     char name16 = char(buffer[5]); uName.concat(name16);
  548.     uName.concat(" ");
  549.     mfrc522.MIFARE_Read(20, buffer, &size);
  550.     uCcount = ((buffer[3] & 0xff) << 8) | buffer[2];
  551.     uName.concat(uCcount); uName.concat("C");
  552.  
  553.     // Process Revo - Capacity and Cells
  554.     mfrc522.MIFARE_Read(21, buffer, &size);
  555.     uCapacity = ((buffer[3] & 0xff) << 8) | buffer[2];
  556.     uCells = buffer[5];
  557.  
  558.     // Process Revo pack-count for correct cell-count - Not used
  559.     mfrc522.MIFARE_Read(27, buffer, &size);
  560.     uPack = buffer[2];
  561.     if (uPack == 1) {
  562.       uCells = uCells * 2;
  563.     }
  564.     if (uPack == 2) {
  565.       uCells = uCells * 3;
  566.     }
  567.     if (uPack == 3) {
  568.       uCells = uCells * 4;
  569.     }
  570.     if (uPack == 4) {
  571.       uCells = uCells * 5;
  572.     }
  573.     if (uPack == 5) {
  574.       uCells = uCells * 6;
  575.     }
  576.  
  577.     // Process Revo - Cycles
  578.     mfrc522.MIFARE_Read(16, buffer, &size);
  579.     uCycles = ((buffer[2] & 0xff) << 8) | buffer[1];
  580.  
  581.     Serial.print("Battery name: "); Serial.println(uName);
  582.     Serial.print("ID: "); Serial.println(uBatteryID);
  583.     Serial.print("Capacity: "); Serial.println(uCapacity);
  584.     Serial.print("Cycles: "); Serial.println(uCycles);
  585.     Serial.print("Cells: "); Serial.println(uCells);
  586.     Serial.print("C-Value: "); Serial.println(uCcount);
  587.     bReadCard = true;
  588.   }
  589.   // Revo Process END
  590.  
  591.   unsigned long time = millis();
  592.   SendFrame();
  593.   time = millis();
  594.   int read = 0;
  595.   pinMode(JETI_RX, INPUT);
  596.   pinMode(JETI_TX, INPUT_PULLUP);
  597.   JetiSerial.listen();
  598.   JetiSerial.flush();
  599.  
  600.   while ( JetiSerial.available()  == 0 )
  601.   {
  602.     if (millis() - time >  5) //5ms to waiting
  603.       break; // return, if timout occures
  604.   }
  605.   if (JetiSerial.available() > 0 )
  606.   { read = JetiSerial.read();
  607.     //
  608.     //No buttons 240
  609.     //LEFT    112
  610.     //RIGHT   224
  611.     //DOWN    176
  612.     //UP      208
  613.     //LEFT+RIGHT  96
  614.     //RIGHT+DOWN  160
  615.     //DOWN+UP   144
  616.     //
  617.     if (lastbtn != read)
  618.     {
  619.       lastbtn = read;
  620.       switch (read)
  621.       {
  622.         case 224 : // RIGHT
  623.           if (current_screen  != MAX_SCREEN)
  624.           {
  625.             current_screen++;
  626.             if (current_screen == 8) current_screen = 0;
  627.           }
  628.           break;
  629.         case 112 : // LEFT
  630.           if (current_screen  != MAX_SCREEN)
  631.             if (current_screen == 99) current_screen = 1;
  632.           if (current_screen == 98) current_screen = 1;
  633.           else
  634.           {
  635.             current_screen--;
  636.             if (current_screen > MAX_SCREEN) current_screen = 0;
  637.           }
  638.           break;
  639.         case 208 : // UP
  640.           if (current_screen == 2) {
  641.             wBatteryID++;
  642.             current_screen = 2;
  643.           }
  644.           if (current_screen == 3) {
  645.             wCapacity = (wCapacity + 100);
  646.             current_screen = 3;
  647.           }
  648.           if (current_screen == 4) {
  649.             wCycles++;
  650.             current_screen = 4;
  651.           }
  652.           if (current_screen == 5) {
  653.             wCcount++;
  654.             current_screen = 5;
  655.           }
  656.           if (current_screen == 6) {
  657.             wCells++;
  658.             current_screen = 6;
  659.           }
  660.           break;
  661.         case 176 : // DOWN
  662.           if (current_screen == 2) {
  663.             wBatteryID = (wBatteryID + 10);
  664.             current_screen = 2;
  665.           }
  666.           if (current_screen == 3) {
  667.             wCapacity = (wCapacity + 1000);
  668.             current_screen = 3;
  669.           }
  670.           if (current_screen == 4) {
  671.             wCycles = (wCycles + 10);
  672.             current_screen = 4;
  673.           }
  674.           if (current_screen == 5) {
  675.             wCcount = (wCcount + 10);
  676.             current_screen = 5;
  677.           }
  678.           if (current_screen == 6) {
  679.             wCells = (wCells + 10);
  680.             current_screen = 6;
  681.           }
  682.           break;
  683.         case 144 : // UP+DOWN
  684.           {
  685.             if (current_screen == 3) {
  686.               wCapacity = (wCapacity + 50);
  687.               current_screen = 3;
  688.             }
  689.             if (current_screen == 7) {
  690.               current_screen = 99;
  691.               if (bReadCard && rct) {
  692.                 unsigned char high = (byte)(wBatteryID >> 8);
  693.                 unsigned char low  = (byte)wBatteryID;
  694.                 blockcontent[0] = high; blockcontent[1] = low;
  695.                 high = (byte)(wCapacity >> 8);
  696.                 low  = (byte)wCapacity ;
  697.                 blockcontent[2] = high; blockcontent[3] = low;
  698.                 high = (byte)(wCycles >> 8);
  699.                 low  = (byte)wCycles;
  700.                 blockcontent[4] = high; blockcontent[5] = low;
  701.                 high = (byte)(wCells >> 8);
  702.                 low  = (byte)wCells;
  703.                 blockcontent[6] = high; blockcontent[7] = low;
  704.                 writeBlock(block, blockcontent);
  705.                 high = (byte)(wCcount >> 8);
  706.                 low  = (byte)wCcount;
  707.                 blockcontent[0] = high; blockcontent[1] = low;
  708.                 writeBlock(block2, blockcontent);
  709.                 tagValues = false;
  710.                 readBlock(block, readbackblock);
  711.                 uBatteryID = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  712.                 uCapacity = ((readbackblock[2] & 0xff) << 8) | readbackblock[3];
  713.                 uCycles = ((readbackblock[4] & 0xff) << 8) | readbackblock[5];
  714.                 uCells = ((readbackblock[6] & 0xff) << 8) | readbackblock[7];
  715.                 readBlock(block2, readbackblock);
  716.                 uCcount = ((readbackblock[0] & 0xff) << 8) | readbackblock[1];
  717.                 bReadCard = true;
  718.                 if (! tagValues)
  719.                 {
  720.                   wBatteryID = uBatteryID;
  721.                   wCapacity = uCapacity;
  722.                   wCycles = uCycles;
  723.                   wCells = uCells;
  724.                   wCcount = uCcount;
  725.                 }
  726.                 tagValues = true;
  727.               }
  728.               // Do not allow writing if Revo Bump is used, show a reason
  729.               if (bReadCard && revo) {
  730.                 current_screen = 98;
  731.               }
  732.             }
  733.           }
  734.           break;
  735.         case 96 : // LEFT+RIGHT
  736.           if (current_screen == 2) {
  737.             wBatteryID = 0;
  738.             current_screen = 2;
  739.           }
  740.           if (current_screen == 3) {
  741.             wCapacity = 0;
  742.             current_screen = 3;
  743.           }
  744.           if (current_screen == 4) {
  745.             wCycles = 0;
  746.             current_screen = 4;
  747.           }
  748.           if (current_screen == 5) {
  749.             wCcount = 0;
  750.             current_screen = 5;
  751.           }
  752.           if (current_screen == 6) {
  753.             wCells = 0;
  754.             current_screen = 6;
  755.           }
  756.           break;
  757.       }
  758.     }
  759.   }
  760.   if (current_screen != MAX_SCREEN)
  761.     current_config = 0;
  762.   process_screens();
  763.   header++;
  764.   if (header >= 5)
  765.   {
  766.     JB.createFrame(1);
  767.     header = 0;
  768.   }
  769.   else
  770.   {
  771.     JB.createFrame(0);
  772.   }
  773.   long wait = GETCHAR_TIMEOUT_ms;
  774.   long milli = millis() - time;
  775.   if (milli > wait)
  776.     wait = 0;
  777.   else
  778.     wait = wait - milli;
  779.   pinMode(JETI_TX, OUTPUT);
  780.   Serial.print("ID: "); Serial.println(uBatteryID);
  781. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement