スマートコンセント PS-5204PSをmbedで
Dependencies: FatFileSystemCpp mbed TextLCD
Fork of BlueUSB by
Fusjitsu スマートコンセント FX-5204PSを利用できます。
BlueUSBを利用していますが、Bluetoothは兼用できません。
複数台利用するには、USBハブを使用してください。ただし相性があります。
・良ければ10台まで利用できるかも。
・稼働中に追加はできますが、外すことはできません。
LED1-3, スマートコンセント接続状態(3台まで表示)
LED4、点滅で正常動作
Revision 2:32a2d06f4fe2, committed 2012-09-27
- Comitter:
- yueee_yt
- Date:
- Thu Sep 27 13:04:36 2012 +0000
- Parent:
- 1:3f2890d103fb
- Commit message:
- V1.0
Changed in this revision
diff -r 3f2890d103fb -r 32a2d06f4fe2 AutoEvents.cpp --- a/AutoEvents.cpp Wed Sep 26 06:29:47 2012 +0000 +++ b/AutoEvents.cpp Thu Sep 27 13:04:36 2012 +0000 @@ -141,9 +141,10 @@ void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) { + int kk=0; printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); char s[128]; - for (int i = 1; i < 3; i++) + for (int i = 1; i < 3; i++) { if (GetString(device,i,s,sizeof(s)) < 0) break; @@ -163,7 +164,8 @@ // Added by Y.Tauchi ------------------------------------ case CLASS_VENDOR_SPECIFIC: if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x02) - OnUspsInsert(device); // it's USPS! (FUJITSU FX-5204PS) + kk = OnUspsInsert(device); // it's USPS! (FUJITSU FX-5204PS) + if(kk!=0)printf("OnUspsInsert Error %d \n\r",kk); break; // ---------------------------------------------------------- default:
diff -r 3f2890d103fb -r 32a2d06f4fe2 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Thu Sep 27 13:04:36 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 3f2890d103fb -r 32a2d06f4fe2 main.cpp --- a/main.cpp Wed Sep 26 06:29:47 2012 +0000 +++ b/main.cpp Thu Sep 27 13:04:36 2012 +0000 @@ -24,6 +24,7 @@ #include "USBHost.h" #include "Utils.h" #include "FATFileSystem.h" +#include "TextLCD.h" int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize); @@ -102,26 +103,42 @@ pc.putc(c); // echo return c; } - -void TestShell(); void InitUSPS(); void USPS_Refresh(); DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); Timer t; +Timer t2; +TextLCD lcd(p24, p26, p27, p28, p29, p30); +extern float WattData1[],WattData2[],WattData3[],WattData4[]; +extern int SerialNo[]; int main() { pc.baud(460800); - printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\n"); InitUSPS(); - //TestShell(); - USBInit(); - t.start(); - while(1){ - USBLoop(); - if(t>1 ){ - t.start(); - led1=!led1; -USPS_Refresh(); - } - } + USBInit(); + t.start(); + t2.start(); + lcd.cls(); + lcd.printf("Watting...."); + while(1) { + USBLoop(); + if(t>0.1 ) { + t.start(); + led4=!led4; + USPS_Refresh(); + } + if(t2>1){ + t2.start(); + if(SerialNo[0]==0)led1=0;else led1=1; + if(SerialNo[1]==0)led2=0;else led2=1; + if(SerialNo[2]==0)led3=0;else led3=1; + lcd.locate(0,0); + lcd.printf("1:%4.0fW 2:%4.0fW",WattData1[0],WattData2[0]); + lcd.locate(0,1); + lcd.printf("3:%4.0fW 4:%4.0fW",WattData3[0],WattData4[0]); + } + } }
diff -r 3f2890d103fb -r 32a2d06f4fe2 usps.cpp --- a/usps.cpp Wed Sep 26 06:29:47 2012 +0000 +++ b/usps.cpp Thu Sep 27 13:04:36 2012 +0000 @@ -6,6 +6,11 @@ int nUSPS; int dUSPS[10]; int eUSPS[10]; +int SerialNo[10]; +float TempData[10]; +float VoltData[10]; +float FreqData[10]; +float WattData1[10],WattData2[10],WattData3[10],WattData4[10]; void usps_cmd(int device,int cmd,int val,int len,int evt); void usps_measurement_mode(int device); void USPS_Refresh(); @@ -38,20 +43,22 @@ float freq; float power1,power2,power3,power4; unsigned char cmdFlag; - +bool initFlag; void InitUSPS() { + int i; nUSPS=0; + for(i=0; i<10; i++)SerialNo[i]=0; + initFlag=false; } void UspsEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) { - int i; + int i,i1,i2,sn; int Flag=0; - int ptr = 0; if(status!=0) { - int i1=0; - if(usps_debug) printf("USPS Callback Status %d \n\r",status); + i1=0; + if(usps_debug) printf("USPS Callback Status %d \n\r",status); for(i=0; i<nUSPS; i++) { if(dUSPS[i]==device) { i1=i; @@ -62,84 +69,178 @@ for(i=i1+1; i<nUSPS; i++) { dUSPS[i-1]=dUSPS[i]; eUSPS[i-1]=eUSPS[i]; + SerialNo[i-1]=SerialNo[i]; + TempData[i-1]=TempData[i]; + VoltData[i-1]=VoltData[i]; + FreqData[i-1]=FreqData[i]; + WattData1[i-1]=WattData1[i]; + WattData2[i-1]=WattData2[i]; + WattData3[i-1]=WattData3[i]; + WattData4[i-1]=WattData4[i]; } nUSPS--; } return; } - switch((int)userData) { + i2=(int)userData; + switch(i2) { case EVENT_GET_FIRMWARE: - if(usps_debug) printf("USPS Device#%X Firmware VOL%X REL%X\r\n",device,data[0],data[1]); + if(usps_debug) printf("USPS Device#%X Firmware VOL%X REL%X\r\n",device,data[0],data[1]); break; + case EVENT_GET_SERIAL: - i=data[0]<<16|data[1]<<8|data[2]; - if(usps_debug) printf("USPS Device#%x SerialNo %X \r\n",device,i); + i1=0; + sn=data[0]<<16|data[1]<<8|data[2]; + if(usps_debug) printf("USPS Device#%x SerialNo %X \r\n",device,sn); + for(i=0; i<nUSPS; i++) { + if(SerialNo[i]==sn) { + i1=i; + Flag=1; + } + } + if(Flag==1) { + for(i=i1+1; i<nUSPS; i++) { + dUSPS[i-1]=dUSPS[i]; + eUSPS[i-1]=eUSPS[i]; + SerialNo[i-1]=SerialNo[i]; + TempData[i-1]=TempData[i]; + VoltData[i-1]=VoltData[i]; + FreqData[i-1]=FreqData[i]; + WattData1[i-1]=WattData1[i]; + WattData2[i-1]=WattData2[i]; + WattData3[i-1]=WattData3[i]; + WattData4[i-1]=WattData4[i]; + } + nUSPS--; + } + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device)SerialNo[i]=sn; + } + for(i=0; i<nUSPS; i++) { + if(usps_debug) printf( "Serial(%d) : %X \n\r",i,SerialNo[i]); + } break; + case EVENT_GET_TEMP: i=data[1]<<8|data[0]; temp=i/100.0; if(usps_debug) printf("USPS Device#%x Temp %5.2f \r\n",device,temp); + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device)TempData[i]=temp; + } break; + case EVENT_GET_VOLT: volt=data[0]; - if(usps_debug) printf("USPS Device#%x Volt %6.2f \r\n",device,volt); + if(usps_debug) printf("USPS Device#%x Volt %6.2f \r\n",device,volt); + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device)VoltData[i]=volt; + } break; + case EVENT_GET_FREQ: i=data[1]<<8|data[0]; freq=2000000.0/i; if(usps_debug) printf("USPS Device#%x Freq %5.2f \r\n",device,freq); + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device)FreqData[i]=freq; + } break; + case EVENT_GET_POWER: power4=(unsigned short)(data[0xf]<<8|data[0xe]); power3=(unsigned short)(data[0xd]<<8|data[0xc]); power2=(unsigned short)(data[0xb]<<8|data[0xa]); power1=(unsigned short)(data[0x9]<<8|data[0x8]); if(usps_debug) printf("USPS Device#%x:Power1 %5.0f[W]:Power2 %5.0f[W]:Power3 %5.0f[W]:Power4 %5.0f[W]\r\n",device,power1,power2,power3,power4); + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device) { + WattData1[i]=power1; + WattData2[i]=power2; + WattData3[i]=power3; + WattData4[i]=power4; + } + } break; + default: - if(usps_debug) printf("usps response \r\n"); + if(usps_debug) printf("usps response \r\n"); if(usps_debug) printf("USPS %02X %02X %02X \r\n",device,status,len); if(usps_debug) printfBytes("",data,len); + break; } } // Add Usps Interface void USPS_Refresh() { - int i; + static int i=0; + static int k=0; u8* dst = ms_buf; - for (i=0; i<nUSPS; i++) { - usps_cmd(dUSPS[i],USPS_CMD_GET_TEMP ,0,2,EVENT_GET_TEMP); - usps_cmd(dUSPS[i],USPS_CMD_GET_VOLTAGE, 0, 1,EVENT_GET_VOLT); - usps_cmd(dUSPS[i],USPS_CMD_GET_FREQ, 0, 8,EVENT_GET_FREQ); - USBInterruptTransfer(dUSPS[i], eUSPS[i], dst,0x10, UspsEventCallback, (void*)EVENT_GET_POWER); + //for (i=0; i<nUSPS; i++) { + if(nUSPS==0)return; + if(initFlag==true) { + for(i=0; i<nUSPS; i++) { + if(SerialNo[i]==0) { + usps_cmd(dUSPS[i],USPS_CMD_GET_SERIAL ,0,3,EVENT_GET_SERIAL); + usps_measurement_mode(dUSPS[i]); + } + } + k=0; + for(i=0; i<nUSPS; i++)if(SerialNo[i]==0)k=1; + if(k==0)initFlag=false; + } else { + if(i==nUSPS)i=0; + switch(k) { + case 0: + usps_cmd(dUSPS[i],USPS_CMD_GET_TEMP ,0,2,EVENT_GET_TEMP); + k++; + break; + case 1: + usps_cmd(dUSPS[i],USPS_CMD_GET_VOLTAGE, 0, 1,EVENT_GET_VOLT); + k++; + break; + case 2: + usps_cmd(dUSPS[i],USPS_CMD_GET_FREQ, 0, 8,EVENT_GET_FREQ); + k++; + break; + case 3: + USBInterruptTransfer(dUSPS[i], eUSPS[i], dst,0x10, UspsEventCallback, (void*)EVENT_GET_POWER); + k=0; + i++; + break; + } } + //} } void AddUspsInterface(int device, InterfaceDescriptor* id, EndpointDescriptor* ed,int len) { if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) return; - usps_cmd(device,USPS_CMD_GET_FIRMWARE ,0,2,EVENT_GET_FIRMWARE); - usps_cmd(device,USPS_CMD_GET_SERIAL ,0,3,EVENT_GET_SERIAL); - usps_measurement_mode(device); dUSPS[nUSPS]=device; eUSPS[nUSPS]=ed->bEndpointAddress; + SerialNo[nUSPS]=0; nUSPS++; + if(usps_debug) printf("No of USPS :%d\n\r",nUSPS); + initFlag=true; +// usps_cmd(device,USPS_CMD_GET_FIRMWARE ,0,2,EVENT_GET_FIRMWARE); +// usps_cmd(device,USPS_CMD_GET_SERIAL ,0,3,EVENT_GET_SERIAL); +// usps_measurement_mode(device); } void usps_measurement_mode(int device) { - int i; u8* dst = ms_buf; - i= USBControlTransfer(device, 0x40, USPS_CMD_START ,0, 0, dst, 0,UspsEventCallback,(void*)EVENT_START); //UT_Vendor_Write - i= USBControlTransfer(device, 0x40, USPS_CMD_VALUE ,USPS_MODE_WATTAGE , 0, dst, 0,UspsEventCallback,(void*)EVENT_VALUE); //UT_Vendor_Write + USBControlTransfer(device, 0x40, USPS_CMD_START ,0, 0, dst, 0,UspsEventCallback,(void*)EVENT_START); //UT_Vendor_Write + USBControlTransfer(device, 0x40, USPS_CMD_VALUE ,USPS_MODE_WATTAGE , 0, dst, 0,UspsEventCallback,(void*)EVENT_VALUE); //UT_Vendor_Write } void usps_cmd(int device,int cmd,int val,int len,int evt) { int i; u8* dst = ms_buf; + // printf("req\n\r"); i= USBControlTransfer(device, 0xc0, cmd,val, 0, dst, len,UspsEventCallback,(void*)evt); if(i!=-100) printf("status : %d \r\n",i); } @@ -154,7 +255,7 @@ u8* d = buffer; u8* end = d + len; - if(usps_debug) printf("OnUspsInsert %02X %02X\r\n",device,len); // + if(usps_debug) printf("OnUspsInsert %02X %02X\r\n",device,len); // //printfBytes(" -->",d,len); while (d < end) { if (d[1] == DESCRIPTOR_TYPE_INTERFACE) {