2015-11-12 22:58:42 [显示全部楼层]
10862浏览
查看: 10862|回复: 6

[项目] 【Hack上海】Intelligent Wardrobe 智能衣橱

[复制链接]
本帖最后由 ShaolinZhang 于 2015-11-12 23:11 编辑

作为本次获得主办方特许的两支高!中!生!参赛队伍之一,能获得评委的认可是很不容易的呢!于是我们做了一个硬件!智能硬件!还在24小时内做了Android和iOS端的Demo软件!
0x01 硬件部分

【Hack上海】Intelligent Wardrobe 智能衣橱图1


我们使用了DFRobot赞助的Arduino Bluno兼容板和手机进行蓝牙通讯,然后通过RFID读卡器(RC522)来读取被动式非接触芯片,也就是植入于衣服的芯片,在demo我们在衣架上黏了个id卡。然后通过简单的拿取衣服,我们的arduino就会给app上位机发送之前录入的衣服的信息,在通过手机app把信息共享给社区。



【Hack上海】Intelligent Wardrobe 智能衣橱图2


这里不得不说说这块RC522,我们周六的时候发现手头上唯一的一块RC522坏掉了...于是啊,连夜顺丰速递送,总算是在周日一大早拿到了崭新的芯片与复旦卡。


【Hack上海】Intelligent Wardrobe 智能衣橱图3


  1. /*
  2. * this is beta 3.0
  3. * old features: communication with android app all done
  4. *              *optimise the loop
  5. * new features: added get_cloth()
  6. */
  7. #include <SPI.h>
  8. #define uchar unsigned char
  9. #define uint unsigned int
  10. //data array maxium length
  11. #define MAX_LEN 16
  12. #define PCD_IDLE 0x00 //NO action; cancel current commands
  13. #define PCD_AUTHENT 0x0E //verify password key
  14. #define PCD_RECEIVE 0x08 //receive data
  15. #define PCD_TRANSMIT 0x04 //send data
  16. #define PCD_TRANSCEIVE 0x0C //send and receive data
  17. #define PCD_RESETPHASE 0x0F //reset
  18. #define PCD_CALCCRC 0x03 //CRC check and caculation
  19. //Mifare_One card command bits
  20. #define PICC_REQIDL 0x26 //Search the cards that not into sleep mode in the antenna area
  21. #define PICC_REQALL 0x52 //Search all the cards in the antenna area
  22. #define PICC_ANTICOLL 0x93 //prevent conflict
  23. #define PICC_SElECTTAG 0x93 //select card
  24. #define PICC_AUTHENT1A 0x60 //verify A password key
  25. #define PICC_AUTHENT1B 0x61 //verify B password key
  26. #define PICC_READ 0x30 //read
  27. #define PICC_WRITE 0xA0 //write
  28. #define PICC_DECREMENT 0xC0 //deduct value
  29. #define PICC_INCREMENT 0xC1 //charge up value
  30. #define PICC_RESTORE 0xC2 //Restore data into buffer
  31. #define PICC_TRANSFER 0xB0 //Save data into buffer
  32. #define PICC_HALT 0x50 //sleep mode
  33. //THe mistake code that return when communicate with MF522
  34. #define MI_OK 0
  35. #define MI_NOTAGERR 1
  36. #define MI_ERR 2
  37. //------------------MFRC522 register ---------------
  38. //Page 0:Command and Status
  39. #define Reserved00 0x00
  40. #define CommandReg 0x01
  41. #define CommIEnReg 0x02
  42. #define DivlEnReg 0x03
  43. #define CommIrqReg 0x04
  44. #define DivIrqReg 0x05
  45. #define ErrorReg 0x06
  46. #define Status1Reg 0x07
  47. #define Status2Reg 0x08
  48. #define FIFODataReg 0x09
  49. #define FIFOLevelReg 0x0A
  50. #define WaterLevelReg 0x0B
  51. #define ControlReg 0x0C
  52. #define BitFramingReg 0x0D
  53. #define CollReg 0x0E
  54. #define Reserved01 0x0F
  55. //Page 1:Command
  56. #define Reserved10 0x10
  57. #define ModeReg 0x11
  58. #define TxModeReg 0x12
  59. #define RxModeReg 0x13
  60. #define TxControlReg 0x14
  61. #define TxAutoReg 0x15
  62. #define TxSelReg 0x16
  63. #define RxSelReg 0x17
  64. #define RxThresholdReg 0x18
  65. #define DemodReg 0x19
  66. #define Reserved11 0x1A
  67. #define Reserved12 0x1B
  68. #define MifareReg 0x1C
  69. #define Reserved13 0x1D
  70. #define Reserved14 0x1E
  71. #define SerialSpeedReg 0x1F
  72. //Page 2:CFG
  73. #define Reserved20 0x20
  74. #define CRCResultRegM 0x21
  75. #define CRCResultRegL 0x22
  76. #define Reserved21 0x23
  77. #define ModWidthReg 0x24
  78. #define Reserved22 0x25
  79. #define RFCfgReg 0x26
  80. #define GsNReg 0x27
  81. #define CWGsPReg 0x28
  82. #define ModGsPReg 0x29
  83. #define TModeReg 0x2A
  84. #define TPrescalerReg 0x2B
  85. #define TReloadRegH 0x2C
  86. #define TReloadRegL 0x2D
  87. #define TCounterValueRegH 0x2E
  88. #define TCounterValueRegL 0x2F
  89. //Page 3:TestRegister
  90. #define Reserved30 0x30
  91. #define TestSel1Reg 0x31
  92. #define TestSel2Reg 0x32
  93. #define TestPinEnReg 0x33
  94. #define TestPinValueReg 0x34
  95. #define TestBusReg 0x35
  96. #define AutoTestReg 0x36
  97. #define VersionReg 0x37
  98. #define AnalogTestReg 0x38
  99. #define TestDAC1Reg 0x39
  100. #define TestDAC2Reg 0x3A
  101. #define TestADCReg 0x3B
  102. #define Reserved31 0x3C
  103. #define Reserved32 0x3D
  104. #define Reserved33 0x3E
  105. #define Reserved34 0x3F
  106. //-----------------------------------------------
  107. //4 bytes Serial number of card, the 5 bytes is verfiy bytes
  108. uchar serNum[5];
  109. String cloth1 = "";
  110. String cloth2 = "";
  111. String cloth3 = "";
  112. int num_cloth = 1;
  113. String comdata = "";
  114. String numData="";
  115. String charData="";
  116. int communication_flag = 0;
  117. const int chipSelectPin = 10;
  118. const int NRSTPD = 5;
  119. char clothid1[] = "";
  120. char clothid2[] = "";
  121. char clothid3[] = "";
  122. int count_cloth = 0;
  123. void send_info(){}
  124. void setup()
  125. {
  126.     Serial.begin(115200);                    
  127.     Serial.print("Arduino ready!");  //连接上电脑时发送一个字符串
  128.     SPI.begin();
  129.     pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
  130.     digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
  131.     pinMode(NRSTPD,OUTPUT); // Set digital pin 5 , Not Reset and Power-down
  132.    
  133.     MFRC522_Init();
  134. }
  135. void loop()
  136. {
  137.   
  138.     get_cloth(cloth1,cloth2,cloth3);
  139.     while (Serial.available() > 0)  
  140.     {
  141.       get_cloth(cloth1,cloth2,cloth3);
  142.       send_info();     
  143.       get_reading();
  144.     }
  145.       if(comdata.length() > 0){
  146.          comm_new_cloth(comdata);
  147.        comdata = "";
  148.       }
  149.   
  150. }
  151. // send_info(){
  152. //  if
  153.    
  154. //}
  155. String comm_new_cloth(String data){
  156.       Serial.println("comdata:");      
  157.       for(int i=0;i<data.length();i++){
  158.         switch(num_cloth){
  159.           case 1: cloth1 += (char)data[i];break;
  160.           case 2: cloth2 += (char)data[i];break;
  161.           case 3: cloth3 += (char)data[i];break;
  162.         }
  163.       }
  164.       Serial.println("cloth one is: ");
  165.       Serial.println(cloth1);
  166.       Serial.println("cloth two is: ");      
  167.       Serial.println(cloth2);
  168.       Serial.println("cloth three is: ");
  169.       Serial.println(cloth3);
  170.       num_cloth++;
  171.       data = "";
  172.      
  173.       return(cloth1);
  174.       return(cloth2);
  175.       return(cloth3);
  176.    
  177. }
  178. String get_reading(){
  179.   comdata += char(Serial.read());
  180.   delay(2);
  181.   return(comdata);
  182. }
  183. String get_cloth(String c1, String c2, String c3){
  184.    
  185.     uchar status;
  186.     uchar str[MAX_LEN];   
  187.     // Search card, return card types
  188.     status = MFRC522_Request(PICC_REQIDL, str);
  189.     if (status != MI_OK)
  190.     {
  191.         return("");
  192.     }   
  193.     // Show card type
  194.     ShowCardType(str);                  //Prevent conflict, return the 4 bytes Serial number of the card
  195.     status = MFRC522_Anticoll(str);    // str[0..3]: serial number of the card
  196.     if (status == MI_OK)
  197.     {
  198.         Serial.print("The card's number is: ");
  199.         memcpy(serNum, str, 5);
  200.         ShowCardID(serNum);          // Check people associated with card ID
  201.         uchar* id = serNum;
  202.         if( id[0]==0xDE && id[1]==0xCA && id[2]==0x4A && id[3]==0x2B ) {
  203.           count_cloth++;
  204.           switch(count_cloth){
  205.             case 1:   for(int i=0; i<4; i++){
  206.                       clothid1[i] = 0x0F & (id[i]>>4);
  207.                       Serial.print(c1);
  208.                       }break;
  209.                      
  210.                      // 0x0F & id[i];
  211.             case 2:   for(int i=0; i<4; i++){
  212.                       clothid2[i] = 0x0F & (id[i]>>4);
  213.                       Serial.print(c2);
  214.                       }break;
  215.             case 3:  for(int i=0; i<4; i++){
  216.                       clothid3[i] = 0x0F & (id[i]>>4);
  217.                       Serial.print(c3);
  218.                       }break;         
  219.               }
  220.             
  221.         } else if(id[0]==0x99 && id[1]==0x0F && id[2]==0x82 && id[3]==0x4A) {
  222.                  count_cloth++;
  223.           switch(count_cloth){
  224.             case 1:   for(int i=0; i<4; i++){
  225.                       clothid1[i] = 0x0F & (id[i]>>4);
  226.                       Serial.print(c1);
  227.                       }break;
  228.                      
  229.                      // 0x0F & id[i];
  230.             case 2:   for(int i=0; i<4; i++){
  231.                       clothid2[i] = 0x0F & (id[i]>>4);
  232.                       Serial.print(c2);
  233.                       }break;
  234.             case 3:  for(int i=0; i<4; i++){
  235.                       clothid3[i] = 0x0F & (id[i]>>4);
  236.                       Serial.print(c3);
  237.                       }break;         
  238.               }
  239.         }
  240.         else if(id[0]==0x93 && id[1]==0xBD && id[2]==0x4A && id[3]==0x2B) {
  241.                        count_cloth++;
  242.           switch(count_cloth){
  243.             case 1:   for(int i=0; i<4; i++){
  244.                       clothid1[i] = 0x0F & (id[i]>>4);
  245.                       Serial.print(c1);
  246.                       }break;
  247.                      
  248.                      // 0x0F & id[i];
  249.             case 2:   for(int i=0; i<4; i++){
  250.                       clothid2[i] = 0x0F & (id[i]>>4);
  251.                       Serial.print(c2);
  252.                       }break;
  253.             case 3:  for(int i=0; i<4; i++){
  254.                       clothid3[i] = 0x0F & (id[i]>>4);
  255.                       Serial.print(c3);
  256.                       }break;         
  257.               }
  258.         }   
  259.   delay(300);
  260. }
  261.     MFRC522_Halt(); //command the card into sleep mode   
  262.   
  263.   
  264. }
  265. /*
  266.   texture
  267.   long/short
  268.   thick
  269.   color
  270.   price
  271.   brand
  272.   type
  273.   
  274. */
  275. /*
  276. * Function:ShowCardID
  277. * Description:Show Card ID
  278. * Input parameter:ID string
  279. * Return:Null
  280. */
  281. void ShowCardID(uchar *id)
  282. {
  283.     int IDlen=4;
  284.     for(int i=0; i<IDlen; i++){
  285.         Serial.print(0x0F & (id[i]>>4), HEX);
  286.         Serial.print(0x0F & id[i],HEX);
  287.    
  288.     }
  289.     Serial.println("");
  290.    
  291. }
  292. /*
  293. * Function:ShowCardType
  294. * Description:Show Card type
  295. * Input parameter:Type string
  296. * Return:Null
  297. */
  298. void ShowCardType(uchar* type)
  299. {
  300.     Serial.print("Card type: ");
  301.     if(type[0]==0x04&&type[1]==0x00)
  302.         Serial.println("MFOne-S50");
  303.     else if(type[0]==0x02&&type[1]==0x00)
  304.         Serial.println("MFOne-S70");
  305.     else if(type[0]==0x44&&type[1]==0x00)
  306.         Serial.println("MF-UltraLight");
  307.     else if(type[0]==0x08&&type[1]==0x00)
  308.         Serial.println("MF-Pro");
  309.     else if(type[0]==0x44&&type[1]==0x03)
  310.         Serial.println("MF Desire");
  311.     else
  312.         Serial.println("Unknown");
  313. }
  314. /*
  315. * Function:Write_MFRC5200
  316. * Description:write a byte data into one register of MR RC522
  317. * Input parameter:addr--register address;val--the value that need to write in
  318. * Return:Null
  319. */
  320. void Write_MFRC522(uchar addr, uchar val)
  321. {
  322.     digitalWrite(chipSelectPin, LOW);
  323.     //address format:0XXXXXX0
  324.     SPI.transfer((addr<<1)&0x7E);
  325.     SPI.transfer(val);
  326.    
  327.     digitalWrite(chipSelectPin, HIGH);
  328. }
  329. /*
  330. * Function:Read_MFRC522
  331. * Description:read a byte data into one register of MR RC522
  332. * Input parameter:addr--register address
  333. * Return:return the read value
  334. */
  335. uchar Read_MFRC522(uchar addr)
  336. {
  337.     uchar val;
  338.     digitalWrite(chipSelectPin, LOW);
  339.     //address format:1XXXXXX0
  340.     SPI.transfer(((addr<<1)&0x7E) | 0x80);
  341.     val =SPI.transfer(0x00);
  342.    
  343.     digitalWrite(chipSelectPin, HIGH);
  344.    
  345.     return val;
  346. }
  347. /*
  348. * Function:SetBitMask
  349. * Description:set RC522 register bit
  350. * Input parameter:reg--register address;mask--value
  351. * Return:null
  352. */
  353. void SetBitMask(uchar reg, uchar mask)
  354. {
  355.     uchar tmp;
  356.     tmp = Read_MFRC522(reg);
  357.     Write_MFRC522(reg, tmp | mask); // set bit mask
  358. }
  359. /*
  360. * Function:ClearBitMask
  361. * Description:clear RC522 register bit
  362. * Input parameter:reg--register address;mask--value
  363. * Return:null
  364. */
  365. void ClearBitMask(uchar reg, uchar mask)
  366. {
  367.     uchar tmp;
  368.     tmp = Read_MFRC522(reg);
  369.     Write_MFRC522(reg, tmp & (~mask)); // clear bit mask
  370. }
  371. /*
  372. * Function:AntennaOn
  373. * Description:Turn on antenna, every time turn on or shut down antenna need at least 1ms delay
  374. * Input parameter:null
  375. * Return:null
  376. */
  377. void AntennaOn(void)
  378. {
  379.     uchar temp;
  380.     temp = Read_MFRC522(TxControlReg);
  381.     if (!(temp & 0x03))
  382.     {
  383.         SetBitMask(TxControlReg, 0x03);
  384.     }
  385. }
  386. /*
  387. * Function:AntennaOff
  388. * Description:Turn off antenna, every time turn on or shut down antenna need at least 1ms delay
  389. * Input parameter:null
  390. * Return:null
  391. */
  392. void AntennaOff(void)
  393. {
  394.     ClearBitMask(TxControlReg, 0x03);
  395. }
  396. /*
  397. * Function:ResetMFRC522
  398. * Description: reset RC522
  399. * Input parameter:null
  400. * Return:null
  401. */
  402. void MFRC522_Reset(void)
  403. {
  404.     Write_MFRC522(CommandReg, PCD_RESETPHASE);
  405. }
  406. /*
  407. * Function:InitMFRC522
  408. * Description:initilize RC522
  409. * Input parameter:null
  410. * Return:null
  411. */
  412. void MFRC522_Init(void)
  413. {
  414.     digitalWrite(NRSTPD,HIGH);
  415.     MFRC522_Reset();
  416.          
  417.     //Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
  418.     Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
  419.     Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
  420.     Write_MFRC522(TReloadRegL, 30);
  421.     Write_MFRC522(TReloadRegH, 0);
  422.    
  423.     Write_MFRC522(TxAutoReg, 0x40); //100%ASK
  424.     Write_MFRC522(ModeReg, 0x3D); //CRC initilizate value 0x6363 ???
  425.     //ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
  426.     //Write_MFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0]
  427.     //Write_MFRC522(RFCfgReg, 0x7F); //RxGain = 48dB
  428.     AntennaOn(); //turn on antenna
  429. }
  430. /*
  431. * Function:MFRC522_Request
  432. * Description:Searching card, read card type
  433. * Input parameter:reqMode--search methods,
  434. * TagType--return card types
  435. * 0x4400 = Mifare_UltraLight
  436. * 0x0400 = Mifare_One(S50)
  437. * 0x0200 = Mifare_One(S70)
  438. * 0x0800 = Mifare_Pro(X)
  439. * 0x4403 = Mifare_DESFire
  440. * return:return MI_OK if successed
  441. */
  442. uchar MFRC522_Request(uchar reqMode, uchar *TagType)
  443. {
  444.     uchar status;
  445.     uint backBits; //the data bits that received
  446.     Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
  447.    
  448.     TagType[0] = reqMode;
  449.     status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
  450.     if ((status != MI_OK) || (backBits != 0x10))
  451.     {
  452.         status = MI_ERR;
  453.     }
  454.    
  455.     return status;
  456. }
  457. /*
  458. * Function:MFRC522_ToCard
  459. * Description:communicate between RC522 and ISO14443
  460. * Input parameter:command--MF522 command bits
  461. * sendData--send data to card via rc522
  462. * sendLen--send data length
  463. * backData--the return data from card
  464. * backLen--the length of return data
  465. * return:return MI_OK if successed
  466. */
  467. uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
  468. {
  469.     uchar status = MI_ERR;
  470.     uchar irqEn = 0x00;
  471.     uchar waitIRq = 0x00;
  472.     uchar lastBits;
  473.     uchar n;
  474.     uint i;
  475.     switch (command)
  476.     {
  477.         case PCD_AUTHENT: //verify card password
  478.         {
  479.             irqEn = 0x12;
  480.             waitIRq = 0x10;
  481.             break;
  482.         }
  483.         case PCD_TRANSCEIVE: //send data in the FIFO
  484.         {
  485.             irqEn = 0x77;
  486.             waitIRq = 0x30;
  487.             break;
  488.         }
  489.         default:
  490.             break;
  491.     }
  492.    
  493.     Write_MFRC522(CommIEnReg, irqEn|0x80); //Allow interruption
  494.     ClearBitMask(CommIrqReg, 0x80); //Clear all the interrupt bits
  495.     SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO initilizate
  496.    
  497.     Write_MFRC522(CommandReg, PCD_IDLE); //NO action;cancel current command ???
  498.     //write data into FIFO
  499.     for (i=0; i<sendLen; i++)
  500.     {
  501.         Write_MFRC522(FIFODataReg, sendData[i]);
  502.     }
  503.     //procceed it
  504.     Write_MFRC522(CommandReg, command);
  505.     if (command == PCD_TRANSCEIVE)
  506.     {
  507.         SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
  508.     }
  509.    
  510.     //waite receive data is finished
  511.     i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
  512.     do
  513.     {
  514.         //CommIrqReg[7..0]
  515.         //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
  516.         n = Read_MFRC522(CommIrqReg);
  517.         i--;
  518.     }
  519.     while ((i!=0) && !(n&0x01) && !(n&waitIRq));
  520.     ClearBitMask(BitFramingReg, 0x80); //StartSend=0
  521.    
  522.     if (i != 0)
  523.     {
  524.         if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
  525.         {
  526.             status = MI_OK;
  527.             if (n & irqEn & 0x01)
  528.             {
  529.                 status = MI_NOTAGERR; //??
  530.             }
  531.             
  532.             if (command == PCD_TRANSCEIVE)
  533.             {
  534.                 n = Read_MFRC522(FIFOLevelReg);
  535.                 lastBits = Read_MFRC522(ControlReg) & 0x07;
  536.                 if (lastBits)
  537.                 {
  538.                     *backLen = (n-1)*8 + lastBits;
  539.                 }
  540.                 else
  541.                 {
  542.                     *backLen = n*8;
  543.                 }
  544.                
  545.                 if (n == 0)
  546.                 {
  547.                     n = 1;
  548.                 }
  549.                 if (n > MAX_LEN)
  550.                 {
  551.                     n = MAX_LEN;
  552.                 }
  553.                
  554.                 //read the data from FIFO
  555.                 for (i=0; i<n; i++)
  556.                 {
  557.                     backData[i] = Read_MFRC522(FIFODataReg);
  558.                 }
  559.             }
  560.         }
  561.         else
  562.         {
  563.             status = MI_ERR;
  564.         }
  565.         
  566.     }
  567.    
  568.     //SetBitMask(ControlReg,0x80); //timer stops
  569.     //Write_MFRC522(CommandReg, PCD_IDLE);
  570.     return status;
  571. }
  572. /*
  573. * Function:MFRC522_Anticoll
  574. * Description:Prevent conflict, read the card serial number
  575. * Input parameter:serNum--return the 4 bytes card serial number, the 5th byte is recheck byte
  576. * return:return MI_OK if successed
  577. */
  578. uchar MFRC522_Anticoll(uchar *serNum)
  579. {
  580.     uchar status;
  581.     uchar i;
  582.     uchar serNumCheck=0;
  583.     uint unLen;
  584.    
  585.     //ClearBitMask(Status2Reg, 0x08); //strSensclear
  586.     //ClearBitMask(CollReg,0x80); //ValuesAfterColl
  587.     Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
  588.     serNum[0] = PICC_ANTICOLL;
  589.     serNum[1] = 0x20;
  590.     status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
  591.     if (status == MI_OK)
  592.     {
  593.         //Verify card serial number
  594.         for (i=0; i<4; i++)
  595.         {
  596.             serNumCheck ^= serNum[i];
  597.         }
  598.         if (serNumCheck != serNum[i])
  599.         {
  600.             status = MI_ERR;
  601.         }
  602.     }
  603.     //SetBitMask(CollReg, 0x80); //ValuesAfterColl=1
  604.     return status;
  605. }
  606. /*
  607. * Function:CalulateCRC
  608. * Description:Use MF522 to caculate CRC
  609. * Input parameter:pIndata--the CRC data need to be read,len--data length,pOutData-- the caculated result of CRC
  610. * return:Null
  611. */
  612. void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
  613. {
  614.     uchar i, n;
  615.     ClearBitMask(DivIrqReg, 0x04); //CRCIrq = 0
  616.     SetBitMask(FIFOLevelReg, 0x80); //Clear FIFO pointer
  617.     //Write_MFRC522(CommandReg, PCD_IDLE);
  618.     //Write data into FIFO
  619.     for (i=0; i<len; i++)
  620.     {
  621.         Write_MFRC522(FIFODataReg, *(pIndata+i));
  622.     }
  623.     Write_MFRC522(CommandReg, PCD_CALCCRC);
  624.     //waite CRC caculation to finish
  625.     i = 0xFF;
  626.     do
  627.     {
  628.         n = Read_MFRC522(DivIrqReg);
  629.         i--;
  630.     }
  631.     while ((i!=0) && !(n&0x04)); //CRCIrq = 1
  632.     //read CRC caculation result
  633.     pOutData[0] = Read_MFRC522(CRCResultRegL);
  634.     pOutData[1] = Read_MFRC522(CRCResultRegM);
  635. }
  636. /*
  637. * Function:MFRC522_Write
  638. * Description:write block data
  639. * Input parameters:blockAddr--block address;writeData--Write 16 bytes data into block
  640. * return:return MI_OK if successed
  641. */
  642. uchar MFRC522_Write(uchar blockAddr, uchar *writeData)
  643. {
  644.     uchar status;
  645.     uint recvBits;
  646.     uchar i;
  647.     uchar buff[18];
  648.    
  649.     buff[0] = PICC_WRITE;
  650.     buff[1] = blockAddr;
  651.     CalulateCRC(buff, 2, &buff[2]);
  652.     status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits);
  653.     if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
  654.     {
  655.         status = MI_ERR;
  656.     }
  657.         
  658.     if (status == MI_OK)
  659.     {
  660.         for (i=0; i<16; i++) //Write 16 bytes data into FIFO
  661.         {
  662.             buff[i] = *(writeData+i);
  663.         }
  664.         CalulateCRC(buff, 16, &buff[16]);
  665.         status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits);
  666.         
  667.         if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
  668.         {
  669.             status = MI_ERR;
  670.         }
  671.     }
  672.    
  673.     return status;
  674. }
  675. /*
  676. * Function:MFRC522_Halt
  677. * Description:Command the cards into sleep mode
  678. * Input parameters:null
  679. * return:null
  680. */
  681. void MFRC522_Halt(void)
  682. {
  683.     uchar status;
  684.     uint unLen;
  685.     uchar buff[4];
  686.     buff[0] = PICC_HALT;
  687.     buff[1] = 0;
  688.     CalulateCRC(buff, 2, &buff[2]);
  689.     status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
  690. }
复制代码



0x02 iOS端


【Hack上海】Intelligent Wardrobe 智能衣橱图6【Hack上海】Intelligent Wardrobe 智能衣橱图7


我们的iOS端首先参考了DF官网上已有的Demo,然而发现是Objective-C写的,于是我们亲爱的Ian同学一怒之下全部改成Swift实现了!


由于代码包太大,我们只好发送到百度云上:

链接: http://pan.baidu.com/s/1dDyQmQX 密码: dek5

Known Issue:!!!在改写为Swift语言时,Bluno自带API中DFBlunoDelegate协议中@required的didReceiveData函数中的第二个参数Device应改为device 否则导致报未实现协议方法的错误。!!!

这个Demo应用可以在链接上Bluno之后通过手机界面上的选择向Bluno发送衣服的特征信息,并让RC522写入被动RFID芯片。


0x03 Android端

【Hack上海】Intelligent Wardrobe 智能衣橱图4


安卓端用了API Level 23作为Target API,请升级Android Studio中的SDK到最新版本。在这个包中我们加入了BlunoLibrary,是DF官方的Demo中的,使用起来非常方便。在MainActivity中,onCreate函数下的内容为应用初始时所执行的代码。


由于代码包太大,我们也只好发到了百度云上:

链接: http://pan.baidu.com/s/1qWsyfne 密码: mdv5


0x04 服务端


【Hack上海】Intelligent Wardrobe 智能衣橱图5


我们使用了LeanCloud作为远端服务器,iOS中也包含了相关的支持库。如果有需要的话,请大家更改成自己的api key哦!



hardware.jpg

Juice  高级技师 来自手机

发表于 2015-11-12 23:13:37

哈哈~~皂片不错~
回复

使用道具 举报

hnyzcj  版主

发表于 2015-11-13 12:19:55

厉害,佩服,高中就玩这么好
回复

使用道具 举报

dsweiliang  初级技神

发表于 2015-11-14 00:45:53

这个有什么用的?
回复

使用道具 举报

吹口琴的钢铁侠  初级技匠

发表于 2015-11-14 09:38:15

现在的高中生都这么厉害吗
回复

使用道具 举报

virtualwiz  中级技匠

发表于 2015-11-14 13:12:37

还有移动端app~~真的好厉害:lol
回复

使用道具 举报

20060606  高级技匠

发表于 2020-8-21 06:29:10

一个初三的学生表示app的开发过程很佩服
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail