public static void Setup(
            GameControlsState controlsState,
            eCellType[] fieldMatrix,
            Position head,
            Position tail,
            ref eDirectionType currentDirection,
            int randomValue,
            ref eGameMode gameMode,
            ref byte baseColor)
        {
            // seed initial state
            FieldMatrix.Seed(fieldMatrix, head, tail);
            currentDirection = eDirectionType.None;

            switch (controlsState.keyCode)
            {
            case KeypadKeyCode.GO:
                FieldMatrix.PlaceNextPiece(fieldMatrix, randomValue);
                gameMode = eGameMode.Play;
                break;

            case KeypadKeyCode.D2:
                baseColor = (byte)(baseColor < 10 ? baseColor + 1 : baseColor);
                break;

            case KeypadKeyCode.D8:
                baseColor = (byte)(baseColor > 1 ? baseColor - 1 : baseColor);
                break;
            }
        }
        public static void GameIteration(
            GameControlsState controlsState,
            eCellType[] fieldMatrix,
            Position head,
            Position tail,
            ref eDirectionType currentDirection,
            int randomValue,
            FPGA.Signal <bool> TXD)
        {
            eDirectionType nextDirectionFromKeypad   = eDirectionType.None;
            eDirectionType nextDirectionFromJoystick = eDirectionType.None;
            eDirectionType nextDirection             = eDirectionType.None;

            Lookups.KeyCodeToDirection(
                controlsState.keyCode,
                ref nextDirectionFromKeypad);

            Lookups.JoystickPositionToDirection(
                controlsState.adcChannel1,
                controlsState.adcChannel2,
                ref nextDirectionFromJoystick);

            bool isReverse = false;

            Lookups.IsReverse(currentDirection, nextDirection, ref isReverse);

            // TODO: variable declaration from conditional expression;
            nextDirection = isReverse
                            ? currentDirection
                            : nextDirectionFromKeypad != eDirectionType.None
                                    ? nextDirectionFromKeypad
                                        : nextDirectionFromJoystick != eDirectionType.None
                                            ? nextDirectionFromJoystick
                                            : currentDirection;

            Diagnostics.ReportState(
                controlsState,
                nextDirectionFromKeypad,
                nextDirectionFromJoystick,
                nextDirection,
                TXD);

            bool expanded = false;

            ApplyChange(
                fieldMatrix,
                head,
                tail,
                currentDirection, nextDirection,
                out expanded);

            if (expanded)
            {
                FieldMatrix.PlaceNextPiece(fieldMatrix, randomValue);
            }

            currentDirection = nextDirection;
        }
        public static async Task Aggregator(
            // blinker
            FPGA.OutputSignal <bool> LED1,

            // keypad
            FPGA.OutputSignal <bool> K7,
            FPGA.OutputSignal <bool> K6,
            FPGA.OutputSignal <bool> K5,
            FPGA.OutputSignal <bool> K4,
            FPGA.InputSignal <bool> K3,
            FPGA.InputSignal <bool> K2,
            FPGA.InputSignal <bool> K1,
            FPGA.InputSignal <bool> K0,

            // banks
            FPGA.OutputSignal <bool> Bank1,
            FPGA.OutputSignal <bool> Bank2,

            // WS2812
            FPGA.OutputSignal <bool> DOUT,

            // ADC
            FPGA.OutputSignal <bool> ADC1NCS,
            FPGA.OutputSignal <bool> ADC1SLCK,
            FPGA.OutputSignal <bool> ADC1DIN,
            FPGA.InputSignal <bool> ADC1DOUT,

            // UART
            FPGA.InputSignal <bool> RXD,
            FPGA.OutputSignal <bool> TXD
            )
        {
            QuokkaBoard.OutputBank(Bank1);
            QuokkaBoard.InputBank(Bank2);

            GameControlsState controlsState = new GameControlsState();

            IsAlive.Blink(LED1);

            Peripherals.GameControls(
                ADC1NCS, ADC1SLCK, ADC1DIN, ADC1DOUT,
                K7, K6, K5, K4, K3, K2, K1, K0,
                controlsState);

            SnakeControl(
                controlsState,
                DOUT, TXD);
        }
        public static void MakeRandomValue(
            GameControlsState controlsState,
            FPGA.Register <int> randomValue)
        {
            FPGA.Register <byte> tickCounter     = 0;
            Func <byte>          nextTickCounter = () => (byte)(tickCounter + 1);
            Func <bool>          tickCounterWE   = () => controlsState.keyCode != Drivers.KeypadKeyCode.None;

            FPGA.Config.RegisterOverride(tickCounter, nextTickCounter, tickCounterWE);

            FPGA.Register <bool> randomCounterWE = true;

            Func <int> nextRandomCounter = () => tickCounter + randomValue + controlsState.adcChannel1 + controlsState.adcChannel2;

            FPGA.Config.RegisterOverride(randomValue, nextRandomCounter, randomCounterWE);
        }
Exemple #5
0
        public static void GameControls(
            // ADC
            FPGA.OutputSignal <bool> ADC1NCS,
            FPGA.OutputSignal <bool> ADC1SLCK,
            FPGA.OutputSignal <bool> ADC1DIN,
            FPGA.InputSignal <bool> ADC1DOUT,

            // keypad
            FPGA.OutputSignal <bool> K7,
            FPGA.OutputSignal <bool> K6,
            FPGA.OutputSignal <bool> K5,
            FPGA.OutputSignal <bool> K4,
            FPGA.InputSignal <bool> K3,
            FPGA.InputSignal <bool> K2,
            FPGA.InputSignal <bool> K1,
            FPGA.InputSignal <bool> K0,
            GameControlsState controlsState)
        {
            KeypadKeyCode internalCode = 0;

            FPGA.Config.Link(internalCode, out controlsState.keyCode);

            ushort adcChannel1Value = 32767, adcChannel2Value = 32767;

            FPGA.Config.Link(adcChannel1Value, controlsState.adcChannel1);
            FPGA.Config.Link(adcChannel2Value, controlsState.adcChannel2);

            Sequential keypadHandler = () =>
            {
                Keypad4x4.ReadASCIICode(K7, K6, K5, K4, K3, K2, K1, K0, out internalCode);
            };

            FPGA.Config.OnTimer(TimeSpan.FromMilliseconds(20), keypadHandler);

            Sequential joystickHandler = () =>
            {
                ADC102S021.Read(out adcChannel1Value, out adcChannel2Value, ADC1NCS, ADC1SLCK, ADC1DIN, ADC1DOUT);
            };

            const bool trigger = true;

            FPGA.Config.OnSignal(trigger, joystickHandler);
        }
        public static void ReportState(
            GameControlsState controlsState,
            eDirectionType nextDirectionFromKeypad,
            eDirectionType nextDirectionFromJoystick,
            eDirectionType nextDirection,
            FPGA.Signal <bool> TXD
            )
        {
            SnakeDBG dbg = new SnakeDBG();

            dbg.C1 = controlsState.adcChannel1;
            dbg.C2 = controlsState.adcChannel2;

            dbg.KD = nextDirectionFromKeypad;
            dbg.JD = nextDirectionFromJoystick;
            dbg.ND = nextDirection;

            JSON.SerializeToUART(ref dbg, TXD);
        }
        public static void SnakeControl(
            GameControlsState controlsState,
            FPGA.OutputSignal <bool> DOUT,
            FPGA.OutputSignal <bool> TXD)
        {
            bool internalDOUT = false;

            FPGA.Config.Link(internalDOUT, DOUT);

            FPGA.Register <int> randomValue = 0;
            RandomValueGenerator.MakeRandomValue(controlsState, randomValue);

            eCellType[]    fieldMatrix      = new eCellType[64];
            var            head             = new Position();
            var            tail             = new Position();
            eDirectionType currentDirection = eDirectionType.None;
            byte           baseColor        = 0x1;
            eGameMode      gameMode         = eGameMode.Setup;

            // drawing
            Sequential drawHandler = () =>
            {
                try
                {
                    switch (gameMode)
                    {
                    case eGameMode.Setup:
                    {
                        GameEngine.Setup(
                            controlsState,
                            fieldMatrix,
                            head,
                            tail,
                            ref currentDirection,
                            randomValue,
                            ref gameMode,
                            ref baseColor);
                    }
                    break;

                    case eGameMode.Play:
                    {
                        GameEngine.GameIteration(
                            controlsState,
                            fieldMatrix,
                            head,
                            tail,
                            ref currentDirection,
                            randomValue,
                            TXD);
                    }
                    break;

                    default:
                        if (controlsState.keyCode == Drivers.KeypadKeyCode.PWR)
                        {
                            gameMode = eGameMode.Setup;
                        }

                        break;
                    }
                }
                catch (GameCompletedException)
                {
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.GreenCross);
                    gameMode = eGameMode.Completed;
                }
                catch (Exception)
                {
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.RedCross);
                    gameMode = eGameMode.Failed;
                }

                Graphics.DrawFieldMatrix(baseColor, fieldMatrix, out internalDOUT);
            };

            FPGA.Config.OnTimer(TimeSpan.FromMilliseconds(400), drawHandler);
        }
Exemple #8
0
        public static async Task Aggregator(
            // banks
            FPGA.OutputSignal <bool> Bank1,
            FPGA.OutputSignal <bool> Bank2,

            // blinker
            FPGA.OutputSignal <bool> LED1,
            FPGA.OutputSignal <bool> LED2,
            FPGA.OutputSignal <bool> LED3,
            FPGA.OutputSignal <bool> LED4,

            // WS2812
            FPGA.OutputSignal <bool> DOUT,

            // keypad
            FPGA.OutputSignal <bool> K7,
            FPGA.OutputSignal <bool> K6,
            FPGA.OutputSignal <bool> K5,
            FPGA.OutputSignal <bool> K4,
            FPGA.InputSignal <bool> K3,
            FPGA.InputSignal <bool> K2,
            FPGA.InputSignal <bool> K1,
            FPGA.InputSignal <bool> K0,

            // ADC
            FPGA.OutputSignal <bool> ADC1NCS,
            FPGA.OutputSignal <bool> ADC1SLCK,
            FPGA.OutputSignal <bool> ADC1DIN,
            FPGA.InputSignal <bool> ADC1DOUT,

            // UART
            FPGA.InputSignal <bool> RXD,
            FPGA.OutputSignal <bool> TXD

            )
        {
            QuokkaBoard.OutputBank(Bank1);
            QuokkaBoard.InputBank(Bank2);

            GameControlsState controlsState = new GameControlsState();

            IsAlive.Blink(LED1);

            IsAlive.Blink(LED2);
            IsAlive.Blink(LED3);
            IsAlive.Blink(LED4);

            Peripherals.GameControls(
                ADC1NCS, ADC1SLCK, ADC1DIN, ADC1DOUT,
                K7, K6, K5, K4, K3, K2, K1, K0,
                controlsState);

            LEDControl(controlsState.keyCode, DOUT);

            byte       data    = 0;
            Sequential handler = () =>
            {
                UART.Write(115200, data, TXD);
                data++;
            };

            FPGA.Config.OnTimer(TimeSpan.FromSeconds(1), handler);
        }