Esempio n. 1
0
        public async void OnPenConditionHandlerAsync(object sender, PenConditionEvent e)
        {
            var newValue = string.Format("Sensor Position={0} \r\n", e.conditionData.StationPosition);

            newValue += string.Format("Battery Sensor={0}(%) Pen={1} (100:High Others:Low) \r\n", e.conditionData.battery_station, e.conditionData.battery_pen);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                      () =>
            {
            });
        }
Esempio n. 2
0
        private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
        {
            // BT_Code: An Indicate or Notify reported that the value has changed.
            // Display the new value with a timestamp.
            // var newValue = FormatValueByPresentation(args.CharacteristicValue);
            // var message = $"Read Data {DateTime.Now:hh:mm:ss}: {newValue}";

            //System.Diagnostics.Debug.WriteLine(message);

            CryptographicBuffer.CopyToByteArray(args.CharacteristicValue, out rxdata);
            var newValue = ByteArrayToString(rxdata);

//            var message = $"Read Data {DateTime.Now:hh:mm:ss}: {newValue}";
//            ConsoleWriteLine(message);
            if (rxdata[2] == 0xC0)       // env
            {
                if (m_bConnect == false) //
                {
                    await cmdAppClose();

                    return;
                }
                penConditionData.StationPosition = StationPosition;
                float batS  = (float)(MAKEWORD(rxdata[7], rxdata[8]) - 0x4A8);
                int   batSt = (int)((batS / (float)(0x5A6 - 0x4A8)) * 100);
                if (batSt > 0)
                {
                    penConditionData.battery_station = batSt > 100 ? 100 : batSt;
                }

                batS  = (float)(MAKEWORD(rxdata[9], rxdata[10]) - 0x408);
                batSt = (int)((batS / (float)(0x4EB - 0x408)) * 100);
                if (batSt > 0)
                {
                    penConditionData.battery_pen = (batSt > 100) ? 100 : batSt;
                }
                penConditionData.pen_alive = (int)MAKEWORD(rxdata[5], (byte)(rxdata[6] & 0x07));

                penConditionData.usbConnect = 0;

                if (firePenConditionDataEvent != null)
                {
                    PenConditionEvent ev = new PenConditionEvent();
                    ev.conditionData = penConditionData;
                    firePenConditionDataEvent(this, ev);
                    ev = null;
                }
            }
            else if (rxdata[2] == 0x7F)
            {
                if (rxdata[3] == 0xCF) // new page
                {
                    ConsoleWriteLine("new page!!!!");
                    SendButtonEvent(NEWPAGE_BUTTON_PRESSED);
                }
                else if (rxdata[3] == 0xDF)
                {
                    ConsoleWriteLine("duplicate page!!!!");
                    SendButtonEvent(NEWPAGE_BUTTON_LONG_PRESSED);
                }
                else if (rxdata[3] == 0xFF)
                {
                    m_nConnectStatus = PEN_DISCONNECT;
                    SendConnectionStatus();

                    ConsoleWriteLine("Dis connected!!!!");
                }
                else
                {
                    m_nModelCode = rxdata[0];
                    if (rxdata[0] == 1)
                    {
                        if (m_bConnect == false)
                        {
                            switch (rxdata[1])
                            {
                            case 0:
                                m_neBeamSMFirstData |= 1;
                                m_nSD      = (int)MAKEWORD(rxdata[5], rxdata[6]);
                                m_nSD2     = m_nSD * m_nSD;
                                m_nIRGap   = (int)MAKEWORD(rxdata[7], rxdata[8]);
                                m_nMCU1Ver = (int)rxdata[10];
                                m_nMCU2Ver = (int)rxdata[11];
                                m_nHWVer   = (int)rxdata[12];
                                ConsoleWriteLine("1 First Data ------------ MCU1: {0} MCU2:{1} HW={2} SD={3} IRG={4}",
                                                 m_nMCU1Ver, m_nMCU2Ver, m_nHWVer, m_nSD, m_nIRGap);
                                break;

                            case 1:
                                m_neBeamSMFirstData |= 2;
                                penCalData.position  = rxdata[5];
                                StationPosition      = rxdata[5];
                                ConsoleWriteLine("2 First Data ------------ position={0} ", penCalData.position);
                                break;

                            case 2:
                                m_neBeamSMFirstData |= 4;
                                penCalData.x1        = (int)MAKEWORD(rxdata[5], rxdata[6]);
                                penCalData.y1        = (int)MAKEWORD(rxdata[7], rxdata[8]);

                                penCalData.x2 = (int)MAKEWORD(rxdata[9], rxdata[10]);
                                penCalData.y2 = (int)MAKEWORD(rxdata[11], rxdata[12]);
                                ConsoleWriteLine("3 First Data------------ x1={0} y1={1} x2={2} y2={3} ", penCalData.x1, penCalData.y1, penCalData.x2, penCalData.y2);
                                break;

                            case 3:
                                m_neBeamSMFirstData |= 8;
                                penCalData.x3        = (int)MAKEWORD(rxdata[5], rxdata[6]);
                                penCalData.y3        = (int)MAKEWORD(rxdata[7], rxdata[8]);

                                penCalData.x4 = (int)MAKEWORD(rxdata[9], rxdata[10]);
                                penCalData.y4 = (int)MAKEWORD(rxdata[11], rxdata[12]);
                                ConsoleWriteLine("4 First Data------------ x3={0} y3={1} x4={2} y4={3} ", penCalData.x3, penCalData.y3, penCalData.x4, penCalData.y4);
                                break;

                            default:
                                break;
                            }
                            if (m_bConnect == false && m_neBeamSMFirstData == 15)
                            {
                                m_bConnect = true;

                                bool bCalError = false;
                                if (penCalData.x1 >= penCalData.x3 || penCalData.x1 >= penCalData.x4)
                                {
                                    bCalError = true;
                                }
                                else if (penCalData.x2 >= penCalData.x3 || penCalData.x2 >= penCalData.x4)
                                {
                                    bCalError = true;
                                }
                                else if (penCalData.y1 >= penCalData.y2 || penCalData.y1 >= penCalData.y3)
                                {
                                    bCalError = true;
                                }
                                else if (penCalData.y4 >= penCalData.y2 || penCalData.y4 >= penCalData.y3)
                                {
                                    bCalError = true;
                                }
                                if (bCalError)
                                {
                                    ConsoleWriteLine("Calibration Data Error!!!!Set Default Data");
                                    SaveCalibrationToDevice(1768, 563, 1768, 5160, 5392, 5160, 5392, 563);
                                }
                                else
                                {
                                    SetCalibration_AllPoints(penCalData.x1, penCalData.y1, penCalData.x2, penCalData.y2,
                                                             penCalData.x3, penCalData.y3, penCalData.x4, penCalData.y4);
                                    SetCalibration_End();
                                }



                                bool ret = await cmdAppStart();

                                m_nConnectStatus = PEN_DATA_READY;
                                SendConnectionStatus();
                            }
                        }
                    }
                }
            }
            else
            {
                MakeVal();
            }


            //await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            ///() => CharacteristicLatestValue.Text = message);
        }