示例#1
0
文件: Program.cs 项目: ktos/HD44780
        static void Main(string[] args)
        {
            ArgumentParser pp = new ArgumentParser(args);
            pp.AddExpanded("-l", "--light");
            pp.AddExpanded("-nl", "--no-light");
            pp.Parse();

            var lcdProvider = new RaspPiGPIOMemLcdTransferProvider(GPIOPins.GPIO_07, GPIOPins.GPIO_08, GPIOPins.GPIO_25, GPIOPins.GPIO_24, GPIOPins.GPIO_23, GPIOPins.GPIO_18);
            var lcd = new Lcd(lcdProvider);

            GPIOMem backlit = new GPIOMem(GPIOPins.GPIO_15, GPIODirection.Out);
            //backlit.Write(false);

            lcd.Begin(16, 2);
            lcd.Clear();
            lcd.SetCursorPosition(0, 0);

            if (pp.SwitchExists("--light"))
            {
                backlit.Write(true);
            }

            if (pp.SwitchExists("--no-light"))
            {
                backlit.Write(false);
            }

            string text = String.Empty;
            try
            {
                var isKey = System.Console.KeyAvailable;
                text = Console.ReadLine();
            }
            catch (InvalidOperationException)
            {
                // when we're in piped output, InvalidOperationException is thrown for KeyAvaliable
                // and we're using it here to check... dirty, dirty hack!
                text = Console.In.ReadToEnd();
            }

            //Console.WriteLine(text);

            text = text.Replace("\r\n", "\n");
            if (text.Length > 32) text = text.Substring(0, 32);
            if (text.Length > 16) text = text.Insert(16, "\n");

            if (text.IndexOf('\n') > -1)
            {
                lcd.Write(text.Substring(0, text.IndexOf('\n')));
                lcd.SetCursorPosition(0, 1);
                lcd.Write(text.Substring(text.IndexOf('\n') +1));
            }
            else
                lcd.Write(text);
        }
        private static void ThreadSmartCard()
        {
            RaspPiGPIOMemLcdTransferProvider lcdProvider = new RaspPiGPIOMemLcdTransferProvider(
            GPIOPins.V2_GPIO_07,
            GPIOPins.V2_GPIO_08,
            GPIOPins.V2_GPIO_25,
            GPIOPins.V2_GPIO_24,
            GPIOPins.V2_GPIO_23,
            GPIOPins.V2_GPIO_18);
            Lcd lcd = new Lcd(lcdProvider);
            lcd.Begin(16, 2);
            lcd.Clear();
            lcd.SetCursorPosition(0, 0);
            lcd.Write("No NFC Card!");

            var gpio22 = TinyGPIO.Export(22);
            gpio22.Direction = (GPIODirection)GPIODirection.Out;

            List<string> deviceNameList = new List<string>();
            NFCContext nfcContext = new NFCContext();
            NFCDevice nfcDevice = nfcContext.OpenDevice(null);
            deviceNameList = nfcContext.ListDeviceNames();
            Console.WriteLine("Device Count: " + deviceNameList.Count());
            foreach (string deviceName in deviceNameList)
            {
                Console.WriteLine("Device Name: " + deviceName);
            }
            int rtn = nfcDevice.initDevice();
            if (rtn < 0)
            {
                Console.WriteLine("Context init failed");
            }

            nfc_target nfcTarget = new nfc_target();
            List<nfc_modulation> nfc_modulationList = new List<nfc_modulation>();
            nfc_modulation nfcModulation = new nfc_modulation();
            nfcModulation.nbr = nfc_baud_rate.NBR_106;
            nfcModulation.nmt = nfc_modulation_type.NMT_ISO14443A;
            nfc_modulationList.Add(nfcModulation);

            string currentSignalRStr = null;
            string currentConsoleStr = null;
            string currentlcdStr = null;
            string signalRStr;
            string consoleStr;
            string lcdStr;
            string state = "---";

            for (; ; )
            {
                gpio22.Value = 0;
                Thread.Sleep(100);
                rtn = nfcDevice.Pool(nfc_modulationList, 1, 2, out nfcTarget);

                if (rtn < 0)
                {
                    consoleStr = "NFC-Poll Targert Not Found!";
                    signalRStr = "---";
                    lcdStr = "No NFC Card!";
                    gpio22.Value = 0;
                }
                else
                {
                    signalRStr = string.Join(
                        separator: "",
                        values: nfcTarget.nti.abtUid.Take((int)nfcTarget.nti.szUidLen).Select(b => b.ToString("X2").ToLower())
                     );
                    signalRStr = "0x" + signalRStr;
                    consoleStr = string.Format("NFC-Poll Target Found: uid is [{0}]", signalRStr);
                    lcdStr = "NFC Detected!";
                }
                if (signalRStr != state)
                {
                    if (signalRStr != currentSignalRStr)
                    {
                        NFC.Instance.UpdateNFCStatus(signalRStr);
                        currentSignalRStr = signalRStr;
                        gpio22.Value = 1;
                        lcd.Begin(16, 2);
                        lcd.Clear();
                        lcd.SetCursorPosition(0, 0);
                        lcd.Write("NFC Detected!");
                        lcd.SetCursorPosition(0, 1);
                        lcd.Write(signalRStr);
                        currentlcdStr = lcdStr;
                        Thread.Sleep(100);
                    }
                    else
                    {
                        gpio22.Value = 0;
                    }
                }
                else
                {
                    if (lcdStr != currentlcdStr)
                    {
                        gpio22.Value = 0;
                        NFC.Instance.UpdateNFCStatus(signalRStr);
                        currentSignalRStr = signalRStr;
                        lcd.Begin(16, 2);
                        lcd.Clear();
                        lcd.SetCursorPosition(0, 0);
                        lcd.Write("No NFC Card!");
                        currentlcdStr = lcdStr;
                    }
                }
                if (consoleStr != currentConsoleStr)
                {
                    Console.WriteLine(consoleStr);
                    currentConsoleStr = consoleStr;
                }
            }
        }