Exemplo n.º 1
0
        private string EventKindToStringValue(PinEvent kind)
        {
            string result;

            if (kind == PinEvent.None)
            {
                result = "none";
            }
            else if (kind.HasFlag(PinEvent.SyncBoth) ||
                     kind.HasFlag(PinEvent.AsyncBoth))
            {
                result = "both";
            }
            else if (kind.HasFlag(PinEvent.SyncRisingEdge) ||
                     kind.HasFlag(PinEvent.AsyncRisingEdge))
            {
                result = "rising";
            }
            else if (kind.HasFlag(PinEvent.SyncFallingEdge) ||
                     kind.HasFlag(PinEvent.AsyncFallingEdge))
            {
                result = "falling";
            }
            else
            {
                throw new NotSupportedException($"Not supported GPIO event kind '{kind}'");
            }

            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// schedule this event
 /// </summary>
 /// <param name="p"></param>
 public void Schedule(PinEvent p)
 {
     lock (locker)
     {
         events.Add(p);
     }
 }
        private void PinAndHit()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            pinEvent.Hit();
            pinEvent.Done();
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void countFlushesAndBytesWritten()
        internal virtual void CountFlushesAndBytesWritten()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            {
                PageFaultEvent faultEvent = pinEvent.BeginPageFault();
                {
                    EvictionEvent evictionEvent = faultEvent.BeginEviction();
                    {
                        FlushEventOpportunity flushEventOpportunity = evictionEvent.FlushEventOpportunity();
                        {
                            FlushEvent flushEvent = flushEventOpportunity.BeginFlush(0, 0, _swapper);
                            flushEvent.AddBytesWritten(27);
                            flushEvent.Done();
                            FlushEvent flushEvent1 = flushEventOpportunity.BeginFlush(0, 1, _swapper);
                            flushEvent1.AddBytesWritten(13);
                            flushEvent1.Done();
                        }
                    }
                    evictionEvent.Close();
                }
                faultEvent.Done();
            }
            pinEvent.Done();

            assertEquals(1, _pageCursorTracer.pins());
            assertEquals(1, _pageCursorTracer.unpins());
            assertEquals(1, _pageCursorTracer.faults());
            assertEquals(1, _pageCursorTracer.evictions());
            assertEquals(2, _pageCursorTracer.flushes());
            assertEquals(40, _pageCursorTracer.bytesWritten());
        }
Exemplo n.º 5
0
        private static void DetectButtonLed(GpioDriver driver)
        {
            using (var controller = new GpioController(driver, PinNumberingScheme.Bcm))
            {
                Pin button = controller.OpenPin(18, PinMode.Input);

                if (button.IsModeSupported(PinMode.InputPullDown))
                {
                    button.Mode = PinMode.InputPullDown;
                }

                Pin led = controller.OpenPin(26, PinMode.Output);

                button.DebounceTimeout     = TimeSpan.FromSeconds(1);
                button.NotifyEvents        = PinEvent.SyncFallingEdge;
                button.ValueChanged       += OnPinValueChanged2;
                button.EnableRaisingEvents = true;

                PinEvent events = button.NotifyEvents;
                Console.WriteLine($"Events to detect: {events}");

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
        protected internal override void SetPinEventsToDetect(int bcmPinNumber, PinEvent events)
        {
            ValidatePinNumber(bcmPinNumber);

            PinEvent kind    = PinEvent.Low;
            bool     enabled = events.HasFlag(kind);

            SetEventDetection(bcmPinNumber, kind, enabled);

            kind    = PinEvent.High;
            enabled = events.HasFlag(kind);
            SetEventDetection(bcmPinNumber, kind, enabled);

            kind    = PinEvent.SyncRisingEdge;
            enabled = events.HasFlag(kind);
            SetEventDetection(bcmPinNumber, kind, enabled);

            kind    = PinEvent.SyncFallingEdge;
            enabled = events.HasFlag(kind);
            SetEventDetection(bcmPinNumber, kind, enabled);

            kind    = PinEvent.AsyncRisingEdge;
            enabled = events.HasFlag(kind);
            SetEventDetection(bcmPinNumber, kind, enabled);

            kind    = PinEvent.AsyncFallingEdge;
            enabled = events.HasFlag(kind);
            SetEventDetection(bcmPinNumber, kind, enabled);

            ClearDetectedEvent(bcmPinNumber);
        }
Exemplo n.º 7
0
        protected internal override void SetPinEventsToDetect(int gpioPinNumber, PinEvent events)
        {
            VerifyPinIsInValidRange(gpioPinNumber, nameof(gpioPinNumber));

            lock (_openPinsLock)
                VerifyPinIsOpen(gpioPinNumber, nameof(gpioPinNumber)).PinEventsToDetect = events;
        }
Exemplo n.º 8
0
 /// <summary>
 /// schedule this event
 /// </summary>
 /// <param name="p"></param>
 public void Schedule(PinEvent p)
 {
     lock (locker)
     {
         events.Add(p);
     }
 }
        public PinEvent[] GetEvents()
        {
            var c = new PinEvent[events.Count];

            events.CopyTo(c, 0);
            return(c);
        }
Exemplo n.º 10
0
        private void PinFaultAndHit()
        {
            PinEvent       pinEvent       = _pageCursorTracer.beginPin(true, 0, _swapper);
            PageFaultEvent pageFaultEvent = pinEvent.BeginPageFault();

            pinEvent.Hit();
            pageFaultEvent.Done();
            pinEvent.Done();
        }
Exemplo n.º 11
0
        protected internal override void SetPinEventsToDetect(int bcmPinNumber, PinEvent kind)
        {
            ValidatePinNumber(bcmPinNumber);

            string edgePath    = $"{GpioPath}/gpio{bcmPinNumber}/edge";
            string stringValue = EventKindToStringValue(kind);

            File.WriteAllText(edgePath, stringValue);
        }
Exemplo n.º 12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void countPinsAndUnpins()
        internal virtual void CountPinsAndUnpins()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            pinEvent.Done();
            pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            assertEquals(2, _pageCursorTracer.pins());
            assertEquals(1, _pageCursorTracer.unpins());
        }
Exemplo n.º 13
0
        protected internal override PinEvent GetPinEventsToDetect(int bcmPinNumber)
        {
            ValidatePinNumber(bcmPinNumber);

            string   edgePath    = $"{GpioPath}/gpio{bcmPinNumber}/edge";
            string   stringValue = File.ReadAllText(edgePath);
            PinEvent value       = StringValueToEventKind(stringValue);

            return(value);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Pin the desired file page to this cursor, page faulting it into memory if it isn't there already. </summary>
        /// <param name="filePageId"> The file page id we want to pin this cursor to. </param>
        /// <param name="writeLock"> 'true' if we will be taking a write lock on the page as part of the pin. </param>
        /// <exception cref="IOException"> if anything goes wrong with the pin, most likely during a page fault. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected void pin(long filePageId, boolean writeLock) throws java.io.IOException
        protected internal virtual void Pin(long filePageId, bool writeLock)
        {
            PinEvent = _tracer.beginPin(writeLock, filePageId, Swapper);
            int chunkId = MuninnPagedFile.ComputeChunkId(filePageId);
            // The chunkOffset is the addressing offset into the chunk array object for the relevant array slot. Using
            // this, we can access the array slot with Unsafe.
            long chunkOffset = MuninnPagedFile.ComputeChunkOffset(filePageId);

            int[][] tt = PagedFile.translationTable;
            if (tt.Length <= chunkId)
            {
                tt = ExpandTranslationTableCapacity(chunkId);
            }
            int[] chunk = tt[chunkId];

            // Now, if the reference in the chunk slot is a latch, we wait on it and look up again (in a loop, since the
            // page might get evicted right after the page fault completes). If we find a page, we lock it and check its
            // binding (since it might get evicted and faulted into something else in the time between our look up and
            // our locking of the page). If the reference is null or it referred to a page that had wrong bindings, we CAS
            // in a latch. If that CAS succeeds, we page fault, set the slot to the faulted in page and open the latch.
            // If the CAS failed, we retry the look up and start over from the top.
            for ( ;;)
            {
                int mappedPageId = UnsafeUtil.getIntVolatile(chunk, chunkOffset);
                if (mappedPageId != UNMAPPED_TTE)
                {
                    // We got *a* page, but we might be racing with eviction. To cope with that, we have to take some
                    // kind of lock on the page, and check that it is indeed bound to what we expect. If not, then it has
                    // been evicted, and possibly even page faulted into something else. In this case, we discard the
                    // item and try again, as the eviction thread would have set the chunk array slot to null.
                    long pageRef = PagedFile.deref(mappedPageId);
                    bool locked  = TryLockPage(pageRef);
                    if (locked & PagedFile.isBoundTo(pageRef, SwapperId, filePageId))
                    {
                        PinCursorToPage(pageRef, filePageId, Swapper);
                        PinEvent.hit();
                        return;
                    }
                    if (locked)
                    {
                        UnlockPage(pageRef);
                    }
                }
                else
                {
                    if (UncommonPin(filePageId, chunkOffset, chunk))
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 15
0
        protected internal override PinEvent GetPinEventsToDetect(int bcmPinNumber)
        {
            ValidatePinNumber(bcmPinNumber);

            PinEvent result  = PinEvent.None;
            PinEvent kind    = PinEvent.Low;
            bool     enabled = GetEventDetection(bcmPinNumber, kind);

            if (enabled)
            {
                result |= kind;
            }

            kind    = PinEvent.High;
            enabled = GetEventDetection(bcmPinNumber, kind);
            if (enabled)
            {
                result |= kind;
            }

            kind    = PinEvent.SyncRisingEdge;
            enabled = GetEventDetection(bcmPinNumber, kind);
            if (enabled)
            {
                result |= kind;
            }

            kind    = PinEvent.SyncFallingEdge;
            enabled = GetEventDetection(bcmPinNumber, kind);
            if (enabled)
            {
                result |= kind;
            }

            kind    = PinEvent.AsyncRisingEdge;
            enabled = GetEventDetection(bcmPinNumber, kind);
            if (enabled)
            {
                result |= kind;
            }

            kind    = PinEvent.AsyncFallingEdge;
            enabled = GetEventDetection(bcmPinNumber, kind);
            if (enabled)
            {
                result |= kind;
            }

            return(result);
        }
Exemplo n.º 16
0
 protected internal override void UnpinCurrentPage()
 {
     if (PinnedPageRef != 0)
     {
         PinEvent.done();
         // Mark the page as dirty *after* our write access, to make sure it's dirty even if it was concurrently
         // flushed. Unlocking the write-locked page will mark it as dirty for us.
         if (EagerFlush)
         {
             EagerlyFlushAndUnlockPage();
         }
         else
         {
             PagedFile.unlockWrite(PinnedPageRef);
         }
     }
     ClearPageCursorState();
 }
Exemplo n.º 17
0
        void queueEvent(PinEvent e)
        {
            // schedule the off note
            var dueOffAt = DateTime.Now.AddMilliseconds(e.TotalMsDelta);

            lock (noteLock)
            {
                noteons[e.Message.PinNumber] = new NoteInfo()
                {
                    Message = e.Message,
                    Off     = dueOffAt,
                };
            }

            // start thread if needed
            if (playerThread == null)
            {
                playerThread = new Thread(new ThreadStart(playThreadLoop));
                playerThread.Start();
            }
        }
        void queueEvent(PinEvent e)
        {
            // schedule the off note
            var dueOffAt = DateTime.Now.AddMilliseconds(e.TotalMsDelta);

            lock (noteLock)
            {
                noteons[e.Message.PinNumber] = new NoteInfo()
                {
                    Message = e.Message,
                    Off = dueOffAt,
                };
            }

            // start thread if needed
            if (playerThread == null)
            {
                playerThread = new Thread(new ThreadStart(playThreadLoop));
                playerThread.Start();
            }

        }
Exemplo n.º 19
0
        private void GenerateEventSet()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(false, 0, _swapper);

            {
                PageFaultEvent pageFaultEvent = pinEvent.BeginPageFault();
                pageFaultEvent.AddBytesRead(150);
                {
                    EvictionEvent evictionEvent = pageFaultEvent.BeginEviction();
                    {
                        FlushEventOpportunity flushEventOpportunity = evictionEvent.FlushEventOpportunity();
                        FlushEvent            flushEvent            = flushEventOpportunity.BeginFlush(0, 0, _swapper);
                        flushEvent.AddBytesWritten(10);
                        flushEvent.Done();
                    }
                    evictionEvent.ThrewException(new IOException("eviction exception"));
                    evictionEvent.Close();
                }
                pageFaultEvent.Done();
            }
            pinEvent.Done();
        }
Exemplo n.º 20
0
 void send(PinEvent e)
 {
     // send it right away if needed (optimisation)
     if (e.TotalMsDelta == 0)
     {
         // yep... go send
         //                Debug.WriteLine("> " + e.Message.PinNumber + " --- " + e.Message.Value);
         pinSender.Send(e.Message);
         if (e.Message.Value == 0)
         {
             // this is off.. take it from the queue
             lock (noteLock)
             {
                 noteons.Remove(e.Message.PinNumber);
             }
         }
     }
     else
     {
         // no.. needs to be queued
         queueEvent(e);
     }
 }
Exemplo n.º 21
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void countPageFaultsAndBytesRead()
        internal virtual void CountPageFaultsAndBytesRead()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            {
                PageFaultEvent pageFaultEvent = pinEvent.BeginPageFault();
                {
                    pageFaultEvent.AddBytesRead(42);
                }
                pageFaultEvent.Done();
                pageFaultEvent = pinEvent.BeginPageFault();
                {
                    pageFaultEvent.AddBytesRead(42);
                }
                pageFaultEvent.Done();
            }
            pinEvent.Done();

            assertEquals(1, _pageCursorTracer.pins());
            assertEquals(1, _pageCursorTracer.unpins());
            assertEquals(2, _pageCursorTracer.faults());
            assertEquals(84, _pageCursorTracer.bytesRead());
        }
Exemplo n.º 22
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void countPageEvictions()
        internal virtual void CountPageEvictions()
        {
            PinEvent pinEvent = _pageCursorTracer.beginPin(true, 0, _swapper);

            {
                PageFaultEvent faultEvent = pinEvent.BeginPageFault();
                {
                    EvictionEvent evictionEvent = faultEvent.BeginEviction();
                    evictionEvent.FilePageId  = 0;
                    evictionEvent.CachePageId = 0;
                    evictionEvent.ThrewException(new IOException("exception"));
                    evictionEvent.Close();
                }
                faultEvent.Done();
            }
            pinEvent.Done();

            assertEquals(1, _pageCursorTracer.pins());
            assertEquals(1, _pageCursorTracer.unpins());
            assertEquals(1, _pageCursorTracer.faults());
            assertEquals(1, _pageCursorTracer.evictions());
            assertEquals(1, _pageCursorTracer.evictionExceptions());
        }
Exemplo n.º 23
0
        private static void Driver_DetectButtonLed(GpioDriver driver)
        {
            const int button = 18;
            const int led    = 26;

            using (driver)
            {
                PinMode buttonMode = PinMode.Input;

                if (driver.IsPinModeSupported(PinMode.InputPullDown))
                {
                    buttonMode = PinMode.InputPullDown;
                }

                driver.OpenPin(button);
                driver.SetPinMode(button, buttonMode);

                driver.OpenPin(led);
                driver.SetPinMode(led, PinMode.Output);

                driver.SetDebounce(button, TimeSpan.FromSeconds(1));
                driver.SetPinEventsToDetect(button, PinEvent.SyncFallingEdge);
                driver.ValueChanged += OnPinValueChanged2;
                driver.SetEnableRaisingPinEvents(button, true);

                PinEvent events = driver.GetPinEventsToDetect(button);
                Console.WriteLine($"Events to detect: {events}");

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Exemplo n.º 24
0
 protected internal abstract void SetPinEventsToDetect(int gpioPinNumber, PinEvent events);
Exemplo n.º 25
0
 public PinEvent[] GetEvents()
 {
     var c = new PinEvent[events.Count];
     events.CopyTo(c, 0);
     return c;
 }
        void send(PinEvent e)
        {
            // send it right away if needed (optimisation)
            if (e.TotalMsDelta == 0)
            {
                // yep... go send
                //                Debug.WriteLine("> " + e.Message.PinNumber + " --- " + e.Message.Value);
                pinSender.Send(e.Message);
                if (e.Message.Value == 0)
                {
                    // this is off.. take it from the queue
                    lock (noteLock)
                    {
                        noteons.Remove(e.Message.PinNumber);
                    }
                }
            }
            else
            {
                // no.. needs to be queued
                queueEvent(e);
            }

        }
Exemplo n.º 27
0
 public void Dispose()
 {
     Pin?.Dispose();
     Pin = null;
     PinEventsToDetect = PinEvent.Any;
 }
Exemplo n.º 28
0
        private bool GetEventDetection(int bcmPinNumber, PinEvent kind)
        {
            //switch (kind)
            //{
            //    case GpioEventKind.High:
            //        value = GetBit(RegisterViewPointer->GPHEN, pin);
            //        break;

            //    case GpioEventKind.Low:
            //        value = GetBit(RegisterViewPointer->GPLEN, pin);
            //        break;

            //    case GpioEventKind.SyncRisingEdge:
            //        value = GetBit(RegisterViewPointer->GPREN, pin);
            //        break;

            //    case GpioEventKind.SyncFallingEdge:
            //        value = GetBit(RegisterViewPointer->GPFEN, pin);
            //        break;

            //    case GpioEventKind.AsyncRisingEdge:
            //        value = GetBit(RegisterViewPointer->GPAREN, pin);
            //        break;

            //    case GpioEventKind.AsyncFallingEdge:
            //        value = GetBit(RegisterViewPointer->GPAFEN, pin);
            //        break;

            //    default: throw new InvalidGpioEventKindException(kind);
            //}

            int    index        = bcmPinNumber / 32;
            int    shift        = bcmPinNumber % 32;
            string registerName = string.Empty;
            uint   register     = 0U;

            switch (kind)
            {
            case PinEvent.High:
                register = _registerViewPointer->GPHEN[index];
                break;

            case PinEvent.Low:
                register = _registerViewPointer->GPLEN[index];
                break;

            case PinEvent.SyncRisingEdge:
                register = _registerViewPointer->GPREN[index];
                break;

            case PinEvent.SyncFallingEdge:
                register = _registerViewPointer->GPFEN[index];
                break;

            case PinEvent.AsyncRisingEdge:
                register = _registerViewPointer->GPAREN[index];
                break;

            case PinEvent.AsyncFallingEdge:
                register = _registerViewPointer->GPAFEN[index];
                break;

            default:
                throw new ArgumentException($"Invalid GPIO event kind '{kind}'");
            }

            uint value = (register >> shift) & 1;

            bool result = Convert.ToBoolean(value);

            return(result);
        }
Exemplo n.º 29
0
        private void SetEventDetection(int bcmPinNumber, PinEvent kind, bool enabled)
        {
            //switch (kind)
            //{
            //    case GpioEventKind.High:
            //        SetBit(RegisterViewPointer->GPHEN, pin);
            //        break;

            //    case GpioEventKind.Low:
            //        SetBit(RegisterViewPointer->GPLEN, pin);
            //        break;

            //    case GpioEventKind.SyncRisingEdge:
            //        SetBit(RegisterViewPointer->GPREN, pin);
            //        break;

            //    case GpioEventKind.SyncFallingEdge:
            //        SetBit(RegisterViewPointer->GPFEN, pin);
            //        break;

            //    case GpioEventKind.AsyncRisingEdge:
            //        SetBit(RegisterViewPointer->GPAREN, pin);
            //        break;

            //    case GpioEventKind.AsyncFallingEdge:
            //        SetBit(RegisterViewPointer->GPAFEN, pin);
            //        break;

            //    default: throw new InvalidGpioEventKindException(kind);
            //}

            int    index           = bcmPinNumber / 32;
            int    shift           = bcmPinNumber % 32;
            uint * registerPointer = null;
            string registerName    = string.Empty;

            switch (kind)
            {
            case PinEvent.High:
                registerPointer = &_registerViewPointer->GPHEN[index];
                registerName    = nameof(RegisterView.GPHEN);
                break;

            case PinEvent.Low:
                registerPointer = &_registerViewPointer->GPLEN[index];
                registerName    = nameof(RegisterView.GPLEN);
                break;

            case PinEvent.SyncRisingEdge:
                registerPointer = &_registerViewPointer->GPREN[index];
                registerName    = nameof(RegisterView.GPREN);
                break;

            case PinEvent.SyncFallingEdge:
                registerPointer = &_registerViewPointer->GPFEN[index];
                registerName    = nameof(RegisterView.GPFEN);
                break;

            case PinEvent.AsyncRisingEdge:
                registerPointer = &_registerViewPointer->GPAREN[index];
                registerName    = nameof(RegisterView.GPAREN);
                break;

            case PinEvent.AsyncFallingEdge:
                registerPointer = &_registerViewPointer->GPAFEN[index];
                registerName    = nameof(RegisterView.GPAFEN);
                break;

            default:
                throw new ArgumentException($"Invalid GPIO event kind '{kind}'");
            }

            //Console.WriteLine($"{registerName} register address = {(long)registerPointer:X16}");

            uint register = *registerPointer;

            //Console.WriteLine($"{registerName} original register value = {register:X8}");

            if (enabled)
            {
                register |= 1U << shift;
            }
            else
            {
                register &= ~(1U << shift);
            }

            //Console.WriteLine($"{registerName} new register value = {register:X8}");

            *registerPointer = register;
        }