Esempio n. 1
0
 private void _sdDetectPort_OnInterrupt(uint data1, uint data2, DateTime time)
 {
     if (_sdDetectPort.Read())
     {
         ReleaseCardStorage();
     }
     else
     {
         if (_cardMounter == null)
         {
             _cardMounter = new Thread(new ThreadStart(() =>
             {
                 int tries = 5;
                 while (tries-- != 0)
                 {
                     if (MountCardStorage() || !_sdDetectPort.Read())
                     {
                         break;
                     }
                     Thread.Sleep(100);
                 }
                 _cardMounter = null;
             }));
             _cardMounter.Start();
         }
     }
 }
Esempio n. 2
0
        private void PrepareRound()
        {
            BrainPad.TrafficLight.TurnOffAllLights();
            BrainPad.TrafficLight.TurnYellowLightOn();
            DrawPlayer();
            ClearMiddle();
            BrainPad.Display.DrawLargeText(30, 50, "To Start!", BrainPad.Color.White);

            while (!inputStart.Read())
            {
                BrainPad.Wait.Milliseconds(5);
            }

            ClearMiddle();
            BrainPad.Display.DrawLargeText(40, 50, "Ready?", BrainPad.Color.White);


            while (inputStart.Read())
            {
                BrainPad.Wait.Milliseconds(5);
            }

            melodyStart.Play(1000);
            ClearMiddle();

            startTime   = DateTime.Now;
            lastContact = Contact.None;

            BrainPad.LightBulb.TurnOn();
            BrainPad.TrafficLight.TurnGreenLightOn();
        }
Esempio n. 3
0
        protected void AciSend(AciOpCode opCode, params byte[] data)
        {
            if (data.Length > 30)
            {
                throw new ArgumentOutOfRangeException("data", "The maximum amount of data bytes is 30.");
            }

            // Create ACI packet
            var packet = new byte[data.Length + 2];

            packet[0] = (byte)(data.Length + 1);
            packet[1] = (byte)opCode;
            Array.Copy(data, 0, packet, 2, data.Length);

            // Request transfer
            _rdy.DisableInterrupt();
            _req.Write(false);

            // Wait for RDY to go low
            while (_rdy.Read())
            {
                ;
            }

            _spi.WriteLsb(packet);

            _req.Write(true);

            // Wait for RDY to go high
            while (!_rdy.Read())
            {
                ;
            }
            _rdy.EnableInterrupt();
        }
Esempio n. 4
0
            public static void Toggle(Cpu.Pin pin, ToggleHandler callback, bool fire = true)
            {
                var btn = new InterruptPort(pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);

                btn.OnInterrupt += (s, e, t) => callback(!btn.Read());
                buttons.Add(btn);
                if (fire)
                {
                    callback(!btn.Read());
                }
            }
Esempio n. 5
0
        // Returns true if the ports are wired together, otherwise false.
        private bool CheckPins()
        {
            Debug.Assert(portIn != null, "Input port should not be null.");
            Debug.Assert(portOut != null, "Output port should not be null.");
            Debug.Assert(!portOut.Active, "Output port should not be active.");

            portOut.Active = true;  // Switch to output
            portOut.Write(false);
            var expectedFalse = portIn.Read();

            portOut.Active = false; // Switch to input
            var expectedTrue = portIn.Read();

            return(expectedTrue && !expectedFalse);
        }
Esempio n. 6
0
        public static void Main()
        {
            InterruptPort button =
                new InterruptPort((Cpu.Pin) 0, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLevelHigh); //Button Declaration.
            OutputPort led  = new OutputPort((Cpu.Pin) 63, false);                                                            //Blue led
            OutputPort led0 = new OutputPort((Cpu.Pin) 62, false);                                                            //Red led
            OutputPort led1 = new OutputPort((Cpu.Pin) 61, false);                                                            //Orange led
            OutputPort led2 = new OutputPort((Cpu.Pin) 60, false);                                                            //Green led

            while (true)                                                                                                      //control loop
            {
                if (button.Read() == true)                                                                                    //if button click
                {
                    //Turn on red and blue led, turn off orange and green led
                    led.Write(true);
                    led0.Write(true);
                    led1.Write(false);
                    led2.Write(false);
                }
                else
                {
                    //Turn on orange and green led, turn off red and blue led
                    led.Write(false);
                    led0.Write(false);
                    led1.Write(true);
                    led2.Write(true);
                }
            }
        }
Esempio n. 7
0
        void Initialize(GoSocket socket)
        {
            // now try to bind to the socket (and verify our module's uniqueId)
            if (!base.BindSocket(socket, _moduleGuid))
            {
                throw new ArgumentException();
            }

            // get socket's physical pins and SPI bus
            Cpu.Pin        socketGpioPin;
            SPI.SPI_module socketSpiModule;
            Cpu.Pin        socketSpiSlaveSelectPin;
            //
            socket.GetPhysicalResources(out socketGpioPin, out socketSpiModule, out socketSpiSlaveSelectPin);

            _spiConfig = new SPI.Configuration(socketSpiSlaveSelectPin, false, 0, 0, false, false, 500, socketSpiModule);
            _spi       = new SPI(_spiConfig);

            // wire up event handlers
            _interruptPort              = new InterruptPort((Cpu.Pin)socketGpioPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            _interruptPort.OnInterrupt += _interruptPort_OnInterrupt;

            // read initial button state
            _isPressed = !_interruptPort.Read();
        }
Esempio n. 8
0
        public bool Read()
        {
            ThrowIfDisposed();
            bool value = _interrupt.Read();

            return(InvertReading ? !value : value);
        }
Esempio n. 9
0
        }                                                   // Keeps track of the axis location in multiples of 8. EG: Half step would move in increments of 4.

        #endregion

        #region Constructors

        public Axis(string axisID, Cpu.Pin stepPin, Cpu.Pin directionPin, Cpu.Pin limitSwitchPin, bool invertDirectionPinOutput = false)
        {
            this.ID       = axisID;
            StepPort      = new OutputPort(stepPin, false);
            DirectionPort = new OutputPort(directionPin, false);
            LimitReached  = LimitSwitch.None;

            if (limitSwitchPin != Cpu.Pin.GPIO_NONE)
            {
                LimitSwitchPort              = new InterruptPort(limitSwitchPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLevelLow);
                LimitSwitchPort.OnInterrupt += new NativeEventHandler(LimitSwitchPin_OnInterrupt);

                // Since we're sharing a port for the Min/Max limit switches on each axis, we can only determine which switch is tripped
                // while there is motion (based on direction). If we initialize the axis and find that the switch is tripped, then we don't
                // know which end it is at and the user will need to move the axis off of the switch before the machine can be homed.
                if (LimitSwitchPort.Read() == false)
                {
                    LimitReached = LimitSwitch.Unknown;
                }
            }

            InvertDirectionPinOutput = invertDirectionPinOutput;

            Location             = 0;
            LocationUnitsPerStep = 4;       //Should be overwritten when machine sets the resolution
            StepDirection        = true;
        }
Esempio n. 10
0
        private void ShowActivity()
        {
            bool wasPressed = false;

            BrainPad.Display.DrawText(10, 50, "Started", BrainPad.Color.Blue);
            while (BrainPad.Looping)
            {
                if (inputStart.Read())
                {
                    //BrainPad.Display.DrawFilledRectangle(0, 25, BrainPad.Display.Width, 70, BrainPad.Color.Black);
                    BrainPad.Display.Clear();
                    BrainPad.TrafficLight.TurnYellowLightOn();
                    BrainPad.Display.DrawText(10, 50, "Yellow pressed    ", BrainPad.Color.Yellow);
                    wasPressed = true;
                }
                else
                {
                    BrainPad.TrafficLight.TurnYellowLightOff();
                    if (wasPressed)
                    {
                        BrainPad.Display.DrawText(10, 50, "Yellow released", BrainPad.Color.Red);
                    }
                }

                BrainPad.Wait.Milliseconds(50);
            }
        }
        // Returns true if the ports are wired together, otherwise false.
        private bool CheckPins()
        {
            return(true);

            //bug, apparently with Netduino Firmware 4.3. check is now disabled and initial reads will also fail
            Debug.Assert(portIn != null, "Input port should not be null.");
            Debug.Assert(portOut != null, "Output port should not be null.");
            Debug.Assert(!portOut.Active, "Output port should not be active.");
            portOut.Active = true;  // Switch to output
            portOut.Write(false);
            Util.Delay(50);
            var expectedFalse = portIn.Read();

            //portOut.Active = false; // Switch to input
            Util.Delay(50);
            var expectedTrue = portIn.Read();

            return(expectedTrue && !expectedFalse);
        }
        public DigitalInputPin(Cpu.Pin pin, bool glitchFilter = false)
        {
            port = new InterruptPort(pin, glitchFilter, HWPort.ResistorMode.Disabled, HWPort.InterruptMode.InterruptEdgeBoth);

            var initialValue = port.Read() ? 1 : 0;

            Output = AddPort("Output", Units.Digital, initialValue);

            port.OnInterrupt += port_OnInterrupt;
        }
Esempio n. 13
0
 /// <summary>
 /// This is a timer responder. Each time a bit is received, the timer is reset. If no bits are received after a
 /// period defined by the rc_timeout constant in this class then the quiet time is deemed to have elapsed
 /// and the received pulse train will be decoded.
 /// </summary>
 /// <param name="o"></param>
 static void RCtimeout(object o)
 {
     // Disable the RC interrupt, process the command, clear the buffers then re-enable the interrupt
     intervals[pos]    = DateTime.Now.Ticks / 10;
     signalStates[pos] = RemoteInputPin.Read();
     RCtimeoutTimer.Change(Timeout.Infinite, Timeout.Infinite);  // Stop the timer
     RC6_Decoder.Decode(intervals, signalStates);
     pos          = 0;
     intervals    = new long[256];           // The garbage collector is going to need to run for this at some point!
     signalStates = new Boolean[256];        // But when? Probably in the middle of an interrupt. This bad code! :^)
 }
Esempio n. 14
0
        public static void Main()
        {
            // Threading system timers
            Timer blueSequence   = new Timer(new TimerCallback(blueTask), false, 10000, 250);
            Timer redSequence    = new Timer(new TimerCallback(redTask), false, 10000, 500);
            Timer orangeSequence = new Timer(new TimerCallback(orangeTask), false, 10000, 1000);

            // input interrupt
            pushBtn.OnInterrupt += pushBtn_OnInterrupt;
            //
            openPort();
            // Main Loop
            while (true)
            {
                if (serialPortFailure)
                {
                    // Flag serial port failure blinking green leed
                    System.Threading.Thread.Sleep(50);
                    Green.Write(true);
                    System.Threading.Thread.Sleep(50);
                    Green.Write(false);
                }
                else
                {
                    reset = pushBtn.Read();
                    System.Threading.Thread.Sleep(2000);
                    if (pushBtn.Read() && reset)
                    {
                        hold = true;
                        pushBtn.OnInterrupt -= pushBtn_OnInterrupt;
                        Red.Write(true);
                        Orange.Write(true);
                        Blue.Write(true);
                        Thread.Sleep(10000);
                        pushBtn.OnInterrupt += pushBtn_OnInterrupt;
                        hold = false;
                    }
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Used to trigger alarm high event
        /// </summary>
        /// <param name="pin">Not used</param>
        /// <param name="state">Not used</param>
        /// <param name="time">Not used</param>
        private void _AL_H_OnInterrupt(uint pin, uint state, DateTime time)
        {
            //This is for debouncing
            long differnce = (time - lastEvent).Ticks / TimeSpan.TicksPerMillisecond;

            if (differnce > 500)
            {
                lastEvent = time.AddMilliseconds(20);
                Thread.Sleep(20);
                AlarmArgs args = new AlarmArgs();
                args.TempC = _temp;
                args.hum   = _hum;
                args.state = _AL_H.Read();
                highAlarmEvent(this, args);
            }
        }
Esempio n. 16
0
 /// <summary>
 ///     Catch the PIR motion change interrupts and work out which interrupt should be raised.
 /// </summary>
 private void _interruptPort_OnInterrupt(uint data1, uint data2, DateTime time)
 {
     if (_interruptPort.Read())
     {
         if (OnMotionStart != null)
         {
             OnMotionStart(this);
         }
     }
     else
     {
         if (OnMotionEnd != null)
         {
             OnMotionEnd(this);
         }
     }
 }
Esempio n. 17
0
            /// <summary>
            /// Debounce timer
            /// </summary>
            /// <param name="obj"></param>
            private void DebounceTimer_Tick(object obj)
            {
#if DebugViaPrint
                //Debug.Print("Timer. _debounceState: " + DebounceStateDef);
#endif
                switch (_debounceState)
                {
                case DebounceStates.WaitPress:
                    // Ignore
                    break;

                case DebounceStates.DebouncePress:
                    var portValue = _port.Read() ? 1 : 0;
                    if (portValue != _onVal)
                    {
                        _debounceState = DebounceStates.WaitPress;
#if DebugViaPrint
                        Debug.Print("Timer. Brief click. Switch to " + DebounceStateDef);
#endif
                        break;
                    }
                    _debounceState = DebounceStates.WaitRelease;
                    _pressedTime   = DateTime.Now;
#if DebugViaPrint
                    Debug.Print("Timer. Switch to " + DebounceStateDef);
#endif
                    break;

                case DebounceStates.WaitRelease:
                    // Ignore
                    break;

                case DebounceStates.DebounceRelease:
                    _debounceState = DebounceStates.WaitPress;
#if DebugViaPrint
                    Debug.Print("Timer. Switch to " + DebounceStateDef);
#endif
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
Esempio n. 18
0
        public static void Btn_OnInterrupt(uint port, uint state, DateTime time)
        {
            Thread.Sleep(1);

            if (port == 45)
            {
                if (btn1.Read() && state == 0)
                {
                    AButtonIsPressed();
                    ledBtn1.Write(false);
                    ledBtn2.Write(true);
                }
            }
            else if (port == 44)
            {
                if (btn2.Read() && state == 1)
                {
                    AButtonIsPressed();
                    ledBtn1.Write(true);
                    ledBtn2.Write(false);
                }
            }
        }
Esempio n. 19
0
 bool IContactSensor.GetState()
 {
     return(sensorPort.Read());
 }
Esempio n. 20
0
 public override bool Read()
 {
     return(_port.Read());
 }
Esempio n. 21
0
 public override bool IsThereAnObstacle()
 {
     return(!capteurIR.Read());
 }
Esempio n. 22
0
        /// <summary>
        /// When the menu to add a card is selected
        /// </summary>
        private void AddCard()
        {
            switch (_addCardState)
            {
            case ADD_CARD_STATE.waitRFID:
                LCD.GetInstance().Clear();
                LCD.GetInstance().DisplayText(GT.Color.Gray, "Veuillez approcher un badge du lecteur");
                if (RFIDReader.GetInstance().IsBadgeScan)
                {
                    _addCardState = ADD_CARD_STATE.RFIDDetected;
                    LCD.GetInstance().DisplayText(GT.Color.Green, "Votre badge a ete correctement scanne", 10, LCD.GetInstance().LcdHeight / 2);
                    Thread.Sleep(2000);     // Wait 2 seconds to see the message
                }
                break;

            case ADD_CARD_STATE.RFIDDetected:
                LCD.GetInstance().Clear();
                string uid = RFIDReader.GetInstance().CurrentUid;

                if (ListOfCards.GetInstance().FindCardInlist(uid))     // If the badge scanned already exist
                {
                    _addCardState = ADD_CARD_STATE.badgeExist;
                }
                else
                {
                    _addCardState = ADD_CARD_STATE.bageDontExist;
                }
                break;

            case ADD_CARD_STATE.badgeExist:
                LCD.GetInstance().DisplayText(GT.Color.Red, "/!\\ Erreur : Ce badge est deja sauvegarde /!\\", 10, LCD.GetInstance().LcdHeight / 2);
                Thread.Sleep(2000);     // Wait 2 seconds to see the message
                RestoreInitialState();
                break;

            case ADD_CARD_STATE.bageDontExist:
                string name      = LCDTextFields.Content; // The content value of the LCD field, it's the name of the badge
                char[] charArray = name.ToCharArray();    // We split the name in a char array to make it easier to modify char by char
                int    x         = 110;                   // The position index where we're gonna write the first char

                if (LCDTextFields.ShouldBeRefresh)        // If we need to refresh because we have modify a char or the position of the cursor
                {
                    LCD.GetInstance().Clear();
                    LCD.GetInstance().DisplayText(GT.Color.Gray, "Votre badge :", 10, LCD.GetInstance().LcdHeight / 2);
                    LCD.GetInstance().DisplayText(GT.Color.Gray, "Pour valider le nom, appuyer sur le joystick", 10, LCD.GetInstance().LcdHeight - 20);
                    for (int i = 0; i < charArray.Length; i++)
                    {
                        if (i == LCDTextFields.CursorPosition)     // If the cursorposition is at this char
                        {
                            LCD.GetInstance().DisplayText(GT.Color.Blue, charArray[i].ToString(), x, LCD.GetInstance().LcdHeight / 2);
                        }
                        else
                        {
                            LCD.GetInstance().DisplayText(GT.Color.Black, charArray[i].ToString(), x, LCD.GetInstance().LcdHeight / 2);
                        }
                        x += 10;     // Increment the X position on the LCD
                    }
                    LCDTextFields.ShouldBeRefresh = false;
                }

                if (_joystickX.Read() < JOYSTICK_UP_RIGHT)       // If the joystick is up
                {
                    charArray[LCDTextFields.CursorPosition]++;   // Increment the char ex : A -> B
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(100);                           // Wait 0.1 second to prevent the letter from scrolling too fast
                }
                else if (_joystickX.Read() > JOYSTICK_DOWN_LEFT) // If the joystick is down
                {
                    charArray[LCDTextFields.CursorPosition]--;   // ecrement the char ex : B -> A
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(100);                           // Wait 0.1 second to prevent the letter from scrolling too fast
                }


                if (_joystickY.Read() < JOYSTICK_UP_RIGHT)                   // If the joystick is right
                {
                    LCDTextFields.CursorPosition++;                          // Move the cursor to the next char
                    if (LCDTextFields.CursorPosition > charArray.Length - 1) // If the cursor get out of the range of the char array
                    {
                        LCDTextFields.CursorPosition = 0;                    // Move to the first position of the char array
                    }
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(200);                                       // Wait 0.2 seconds to prevent the cursor from moving too fast
                }
                else if (_joystickY.Read() > JOYSTICK_DOWN_LEFT)             // If the joystick is left
                {
                    LCDTextFields.CursorPosition--;                          // Move the cursor to the previous char
                    if (LCDTextFields.CursorPosition < 0)                    // If the cursor get out of the range of the char array
                    {
                        LCDTextFields.CursorPosition = charArray.Length - 1; // Move to the last position of the char array
                    }
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(200);     // Wait 0.2 seconds to prevent the cursor from moving too fast
                }

                LCDTextFields.Content = new string(charArray); // Set the LCD text field with the value of the modify char array

                if (!_joystickButton.Read())                   // If joystick button is press
                {
                    _addCardState = ADD_CARD_STATE.save;
                }
                break;

            case ADD_CARD_STATE.save:
                try
                {
                    uid  = RFIDReader.GetInstance().CurrentUid;    // Get the uid of the badge that was scanned
                    name = LCDTextFields.Content;
                    ListOfCards.GetInstance().AddCardToList(name, uid);
                    SDCard.GetInstance().SaveCards(ListOfCards.GetInstance().CardsList);
                    _addCardState = ADD_CARD_STATE.successMSG;
                }
                catch (Exception e)
                {
                    _addCardState = ADD_CARD_STATE.errorMSG;
                }
                break;

            case ADD_CARD_STATE.errorMSG:
                DisplayError();
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            case ADD_CARD_STATE.successMSG:
                DisplaySave();
                LCD.GetInstance().DisplayText(GT.Color.Green, "Le badge a bien ete ajoute", 10, LCD.GetInstance().LcdHeight / 2);
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            default:
                _addCardState = ADD_CARD_STATE.waitRFID;
                break;
            }
        }
 public void Start()
 {
     oldState = input.Read() ? InputSensorState.Low : InputSensorState.High;
     _stopped = false;
 }