touchmind::VISITOR_RESULT operator()(std::shared_ptr<touchmind::model::node::NodeModel> node) { if (m_nodeIdToView.count(node->GetId()) != 0) { auto &nodeView = m_nodeIdToView[node->GetId()]; if (nodeView->IsCompatible(node)) { nodeView->SetHandled(); } else { auto editControlIndex = nodeView->GetEditControlIndex(); m_nodeIdToView.erase(node->GetId()); CreateNewView(node, editControlIndex); } } else { CreateNewView(node); } return touchmind::VISITOR_RESULT_CONTINUE; }
///////////////////////////////////////////////////////////////////////////// // CHtmlWnd message handlers void CHtmlWnd::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* bCancel) { try{ CMyIEView* pv = CreateNewView(); if(pv!=NULL) *ppDisp = pv->GetApplication(); } catch(...) { } }
CVeryIEView* GetCurrentView(int nFlag, BOOL bInNewWindow) { CChildFrame* tcf = (CChildFrame*)((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive(); if(tcf!=NULL && !bInNewWindow && !PRESS_CTRL) { return (CVeryIEView*)tcf->m_pView; } else { if(nFlag == 1 || bInNewWindow || PRESS_CTRL) //create new return CreateNewView(); return NULL; } }
LRESULT CIMoteTerminal::OnReceiveSerialData(WPARAM wParam, LPARAM lParam) { //following couple of lines allow us to debug a raw datastream char *rxstring = (char *)lParam; DWORD numBytesReceived = (DWORD) wParam; BufferAppend(rxstring, numBytesReceived); delete []rxstring; return TRUE; #if 0 DWORD i,offset; //TRACE("Rx...Buffer = %#X\tNumBytesReceived = %d\n",rxstring,numBytesReceived); /***** data format for the accelerometer data looks something like: 0{2 bit addr}{5 data bits} {1}{7 data bits} ******/ for(offset=0; offset<numBytesReceived; offset++) { //find the correct first bytes if((rxstring[offset] & 0xE0) == 0) { break; } } //offset current points to the correct first element for us to look at //start reconstructing the 16 bit numbers and doing the divide for(i=offset;(i+6)<numBytesReceived; i+=6) { static bool init = false; POINT point; DWORD B,C,D,Tx, Ty,T; int Rx, Ry; B = ((rxstring[i] & 0x1F)<<7) | (rxstring[i+1] & 0x7F); C = ((rxstring[i+2] & 0x1F)<<7) | (rxstring[i+3] & 0x7F); D = ((rxstring[i+4] & 0x1F)<<7) | (rxstring[i+5] & 0x7F); Tx = B; Ty = D-C; T = C/2 + D/2 - B/2; Rx = ((Tx << 16) / T) - (65536/2); Ry = ((Ty << 16) / T) - (65536/2); //point.x =(LONG)( (rxstring[byte_index]<<8) + rxstring[byte_index+1]) -(65536/2); //point.x = (LONG)( (rxstring[byte_index]<<8) + rxstring[byte_index+1]); //TRACE("%d %d = %d\n",rxstring[i], rxstring[i+1], point.x); //TRACE("Found T, index %d \n", byte_index); //TRACE("Tx = %d, Ty = %d, T = %d, Rx = %d, Ry = %d\n",Tx, Ty, T, Rx, Ry); point.x = (LONG) Rx; point.y = (LONG) Ry; if(!init) { CIMoteCartesianPlot *pFrame=CreateNewView(0,0xDEADBEEF,0); pFrame->SetMappingFunction(-2,2); init = true; } AddPoint(point, 0); } delete rxstring; return TRUE; //#endif; POINT point; static bool bGotBeef = 0; static bool bFirstTime = true; static unsigned short NumDataBytes; static unsigned short NumBytesProcessed; //static int MoteIDs[NUMCHANNELS]; static unsigned short SensorID; static unsigned int MoteID; static unsigned int SensorType; static unsigned int ExtraInfo; static unsigned int TimeID; static unsigned int ChannelID; static unsigned char HeaderIndex; static unsigned char Header[16]; unsigned short *short_ptr; unsigned int *int_ptr; DWORD byte_index; static unsigned char LastByte = 0; // unsigned int ProblemIndex; unsigned int EmptyChannel; static unsigned int NumProblems = 0; CString logentry; static bool bPrintheader=true; CTime time; static int Tx, Ty, T, Rx, Ry, Tb, Tc, Td, b0, b1; static int CurrentCounter, CurrentByte; // Hack, for now statically allocate static unsigned char *CameraBuffer; static unsigned int CurrentCameraID; static unsigned int CameraBufferIndex; static unsigned int SegmentIndex; static bool PictureInProgress; static unsigned int LastPicID; #define MAX_PIC_SIZE 80000 #define INVALID_SENSOR 0 #define PH_SENSOR 1 #define PRESSURE_SENSOR 2 #define ACCELEROMETER_SENSOR 3 #define CAMERA_SENSOR 4 #define FIRST_SEGMENT 0x1111 #define MID_SEGMENT 0 #define END_OF_PIC 0xffff for(int channel = 0; (channel < NUMCHANNELS) && bFirstTime; channel++) { MoteIDs[channel] = 0; HeaderIndex = 0; CurrentCameraID = 0; CameraBuffer = NULL; CameraBufferIndex = 0; PictureInProgress = false; } if (bFirstTime) { // Figure out the start of the file names CFileFind finder; CString TempName; unsigned int TempID; LastPicID = 0; BOOL bResult = finder.FindFile("c:\\icam\\*.jpg"); while (bResult) { bResult = finder.FindNextFile(); TempName = finder.GetFileName(); if (sscanf((LPCSTR)TempName, "%d.jpg", &TempID) == 1) { // valid pic id if (LastPicID < TempID) { LastPicID = TempID; } } } LastPicID++; } bFirstTime = false; TRACE("Rx...Buffer = %#X\tNumBytesReceived = %d\n",rxstring,numBytesReceived); byte_index = 0; while(byte_index < numBytesReceived) { // Look for DEADBEEF, get all header info for(; (byte_index < numBytesReceived) && !bGotBeef; byte_index++) { switch (HeaderIndex) { case 0: if (rxstring[byte_index] == 0xEF) { HeaderIndex = 1; } break; case 1: if (rxstring[byte_index] == 0xBE) { HeaderIndex = 2; } else { HeaderIndex = 0; } break; case 2: if (rxstring[byte_index] == 0xAD) { HeaderIndex = 3; } else { HeaderIndex = 0; } break; case 3: if (rxstring[byte_index] == 0xDE) { HeaderIndex = 4; } else { HeaderIndex = 0; } break; case 13: // Done with header CurrentCounter = 0; CurrentByte = 0; bGotBeef = 1; Header[HeaderIndex] = rxstring[byte_index]; /* * Header : * DEADBEEF (4B) * MOTE ID (4B) * Sensor TYPE (2B) * LENGTH (2B) * Extra Info (2B) * */ int_ptr = (unsigned int *) &(Header[4]); MoteID = *int_ptr; short_ptr = (unsigned short *) &(Header[8]); SensorType = *short_ptr; short_ptr++; NumDataBytes = *short_ptr; short_ptr++; ExtraInfo = *short_ptr; NumBytesProcessed = 0; ChannelID = NUMCHANNELS; EmptyChannel = NUMCHANNELS; if (SensorType == CAMERA_SENSOR) { // check with segment TRACE("Camera seg %x, buf Index %d, NumDataBytes %d\r\n", ExtraInfo, CameraBufferIndex, NumDataBytes); if (ExtraInfo == FIRST_SEGMENT) { // first segment CurrentCameraID = MoteID; CameraBufferIndex = 0; if (!PictureInProgress) { // create buffer CameraBuffer = new unsigned char[MAX_PIC_SIZE]; PictureInProgress = true; } } SegmentIndex = 0; // Per segment index break; // don't process the channel stuff } // Find mote channel, for(int channel = 0; channel < NUMCHANNELS; channel++) { if (MoteIDs[channel] == MoteID) { ChannelID = channel; break; } else { if (MoteIDs[channel] == 0) { EmptyChannel = channel; } } } if (ChannelID == NUMCHANNELS) { // Didn't find a channel if (EmptyChannel < NUMCHANNELS) { // assign the mote id to this channel MoteIDs[EmptyChannel] = MoteID; ChannelID = EmptyChannel; CIMoteCartesianPlot *pFrame=CreateNewView(ChannelID,MoteID,SensorID); /* Note to LAMA: below is an example of how to use the setmapping function pFrame->SetMappingFunction(slope, offset, minrange, maxrange */ switch(SensorType) { case PH_SENSOR: pFrame->SetMappingFunction(0,14); rawdata = false; break; case PRESSURE_SENSOR: pFrame->SetMappingFunction(0,20.684); //pFrame->SetMappingFunction(0,300); rawdata = false; break; case ACCELEROMETER_SENSOR: pFrame->SetMappingFunction(-2,2); rawdata = false; break; default : //pFrame->SetMappingFunction(1,1,0,14); pFrame->SetMappingFunction(-32768,32768); } //UpdateAllViews(NULL); } /* * NOTE: if ChannelID is not assigned, * the processing will remain the same, but the data won't * be displayed. * TODO : handle later */ } //log transaction info to file here: if(bPrintheader) { logentry.Format("Timestamp, iMoteID, # of Bytes\r\n"); //logfile<<logentry<<endl; SaveLogEntry(&logentry); bPrintheader=false; } time=time.GetCurrentTime(); //logfile<<time.Format("%c"); SaveLogEntry(&time.Format("%c")); logentry.Format(", %#X, %d\r\n",MoteID, NumDataBytes); //logfile<<logentry<<endl; SaveLogEntry(&logentry); break; default: Header[HeaderIndex] = rxstring[byte_index]; HeaderIndex++; break; } } if (!bGotBeef) { delete []rxstring; return TRUE; } // Got DEADBEEF, process data for(; byte_index <numBytesReceived; byte_index++,NumBytesProcessed ++) { if (NumBytesProcessed >= NumDataBytes) { // go back to start, look for DEADBEEF again bGotBeef = false; HeaderIndex = 0; TRACE("Mote ID %lx, NumBytes %ld, byte index %d \n", MoteID, NumDataBytes, byte_index); //MoteID = 0; //NumDataBytes = 0; break; } if (rawdata) { //RAW_BYTES mode, no processing // Assume data is 2 bytes long, and back to back if (CurrentByte == 0) { b0 = rxstring[byte_index]; CurrentByte = 1; } else { b1 = rxstring[byte_index]; CurrentByte = 0; int sample_data; sample_data = (b1 <<8) + b0; //sample_data -= 0x2000; //sample_data = sample_data << 2; point.x = (LONG) sample_data; point.y = 0; //TRACE("sample is %d\r\n", sample_data); if (ChannelID < NUMCHANNELS) { // valid channel AddPoint(point, ChannelID); } } } else { if (CurrentByte == 0) { b0 = rxstring[byte_index]; CurrentByte = 1; if (SensorType == CAMERA_SENSOR) { // just copy data CameraBuffer[CameraBufferIndex] = b0; SegmentIndex++; CameraBufferIndex++; } } else { b1 = rxstring[byte_index]; CurrentByte = 0; switch(SensorType) { case PH_SENSOR: /* * A/D maps 0-5V range to 0-32 K * pH = -7.752 * V + 16.237 * V = raw_data * 5 / 32768 * The plot output expects the 0 - 14 range to be represented in -32 - 32 K * point.x = (-7.752 * (raw_data * 5/32768) + 16.237) * 64K / 14 - 32K */ double ph_data; ph_data = (b1 <<8) + b0; ph_data = -7.752 * (ph_data/ 32768) * 5 + 16.237; ph_data = (ph_data * 65536 / 14) - 32768; point.x = (LONG) ph_data; point.y = 0; if (ChannelID < NUMCHANNELS) { // valid channel AddPoint(point, ChannelID); } break; case PRESSURE_SENSOR: /* * A/D maps 0-5V range to 0-32 K * The plot output expects the 0 - 20.684 range to be represented in -32 - 32 K * point.x = (raw_data * 5/32768) * 64K / 20.684 - 32K */ int pressure_data; pressure_data = (b1 <<8) + b0; pressure_data = pressure_data * 2 - 32768; point.x = (LONG) pressure_data; point.y = 0; if (ChannelID < NUMCHANNELS) { // valid channel AddPoint(point, ChannelID); } break; case ACCELEROMETER_SENSOR: // TRACE("CurrentCounter %d, ByteIndex %d \n", CurrentCounter, byte_index); switch (CurrentCounter) { case 0: Tx = (b0 <<8) + b1;; CurrentCounter = 1; //TRACE("Found Tx, index %d \n", byte_index); break; case 1: Ty = (b0 <<8) + b1; CurrentCounter = 2; //TRACE("Found Ty, index %d \n", byte_index); break; case 2: T = (b0 <<8) + b1; Rx = ((Tx << 16) / T) - (65536/2); Ry = ((Ty << 16) / T) - (65536/2); //point.x =(LONG)( (rxstring[byte_index]<<8) + rxstring[byte_index+1]) -(65536/2); //point.x = (LONG)( (rxstring[byte_index]<<8) + rxstring[byte_index+1]); //TRACE("%d %d = %d\n",rxstring[i], rxstring[i+1], point.x); //TRACE("Found T, index %d \n", byte_index); //TRACE("Tx = %d, Ty = %d, T = %d, Rx = %d, Ry = %d\n",Tx, Ty, T, Rx, Ry); point.x = (LONG) Rx; point.y = (LONG) Ry; if (ChannelID < NUMCHANNELS) { // valid channel AddPoint(point, ChannelID); } CurrentCounter = 0; break; default: break; } break; case CAMERA_SENSOR: // just copy data CameraBuffer[CameraBufferIndex] = b1; SegmentIndex++; CameraBufferIndex++; break; } } //for now, just save the point in the x field of the structure } //NumBytesProcessed += 2; } TRACE("NumBytesProcessed %d, NumDataBytes %d\r\n", NumBytesProcessed, NumDataBytes); // Check if we reached the end of a picture, write it to file if ((SensorType == CAMERA_SENSOR) && (NumBytesProcessed == NumDataBytes) && (ExtraInfo == END_OF_PIC)) { // Create output buffer , assume header < 1000 unsigned char *JpgImage; int JpgImageLen; JpgImage = new unsigned char[CameraBufferIndex+1000]; // build jpeg image BuildJPG(CameraBuffer, CameraBufferIndex, JpgImage, &JpgImageLen); // write to file char pszFileName[200]; CFile PictureFile; CFileException fileException; sprintf(pszFileName, "c:\\icam\\%d.jpg", LastPicID); LastPicID++; if ( !PictureFile.Open( pszFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &fileException ) ) { TRACE( "Can't open file %s, error = %u\n", pszFileName, fileException.m_cause ); } //PictureFile.Write(CameraBuffer, CameraBufferIndex); PictureFile.Write(JpgImage, JpgImageLen); PictureFile.Close(); TRACE("Wrote Jpeg image raw %d\r\n", CameraBufferIndex); delete []CameraBuffer; delete []JpgImage; PictureInProgress = false; } } delete []rxstring; return TRUE; #endif; }