Difference between revisions of "F18: baem geim"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Abstract)
(Hardware Design)
 
(289 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Grading Criteria ===
+
[[File:snakepixel.jpg|thumb|900x600px|right|Snake]]
<font color="green">
+
 
*  How well is Software & Hardware Design described?
 
*  How well can this report be used to reproduce this project?
 
*  Code Quality
 
*  Overall Report Quality:
 
**  Software Block Diagrams
 
**  Hardware Block Diagrams
 
**:  Schematic Quality
 
**  Quality of technical challenges and solutions adopted.
 
</font>
 
 
== Project Title ==
 
== Project Title ==
 
'''baem geim - Snake Game Reborn'''
 
'''baem geim - Snake Game Reborn'''
  
[[File:Snake.jpg]]
+
[[File:Snake.jpg|thumb|900x300px|center]]
  
 
== Abstract ==
 
== Abstract ==
beim geim is revived, most amusing snake game where player maneuvers a line which grows in length by consuming dots as food, with the line itself and wall as obstacle. The player's points augment when snake consumes food. The game is over if snake comes in contact with itself or against the wall.
+
baem geim is a revived, amusing Snake Game where a player maneuvers a line which grows in length, with the line itself and wall as obstacle. The concept originated in the 1976 arcade game Blockade, and the ease of implementing snake has led to hundreds of versions for many platforms. This is a new version of snake game which we have developed. The player controls the dot using handheld joystick. As it moves forward, it leaves a trail behind, resembling a moving snake. The snake movement is visually displayed in real time on a 32*32 LED Matrix. The player attempts to eat randomly generated dots as fruits by running into it with the head of the snake. Each fruit eaten increases the length of the snake and controlling it becomes progressively difficult. The player loses the game if the snake runs into itself or hits the screen border.
The game is designed using two SJone LPC1758 microcontroller boards one each to drive 32x32 LED Matrix and Joystick module respectively. They communicate wirelessly through RF Nordic transceiver.
+
The game is designed using two SJone LPC1758 microcontroller boards one each to drive LED Matrix and Joystick module respectively. They communicate wirelessly through RF Nordic transceiver. An additional feature is the use of active buzzer for sound effects.
  
 
== Objectives & Introduction ==
 
== Objectives & Introduction ==
Show list of your objectives. This section includes the high level details of your project. You can write about the various sensors or peripherals you used to get your project completed.
+
The main objective of the project is to develop a 2D single player snake game. Other milestones to achieve the objective are as below.
 +
*Visually display the movement of the snake on LED Matrix in real time.
 +
*Control the movement of the Snake through Joystick.
 +
*Transmit the control data wirelessly from input to output console through Nordic transceiver.
 +
*Design PCB for distribution of input power appropriately between two SJone boards.
  
=== Team Members & Responsibilities ===
+
The project is broadly divided into two modules.
* Bharath Vyas Balasubramanyam
+
 
** Driver for Nordic Wireless and Joystick
+
1. '''Input Module''': The input module connects SJone board to two-axis joystick which is used to control the snake advancement. Nordic transmitter continuously transmits control packets to the output module.
* Neeraj Dhavale
+
 
** Driver for LED Matrix
+
2. '''Output Module''': The output module connects another SJone board to LED Matrix for visual display. It recursively polls to receive control packets from the input module and updates the snake movement as per user's input.
* Sudharshan Aithal
+
 
** Design of Game Algorithm
+
== Team Members & Responsibilities ==  
* Vignesh Kumar Venkateshwar
+
 
** Managing Wiki and PCB Design and Driver for Joystick
+
* '''[https://www.linkedin.com/in/bharath-vyas/ Bharath "Boa" Vyas Balasubramanyam]'''
 +
** Nordic Wireless Transmitter driver implementation and Driver for Joystick Module
 +
 
 +
* '''[https://www.linkedin.com/in/neeraj-dhavale-4ba651164/ Neeraj "Naag" Dhavale]'''
 +
** Driver for LED Matrix and  Nordic Wireless Receiver Driver Implementation
 +
 
 +
* '''[https://www.linkedin.com/in/sudarshan-aithal/ Sudarshan "Python" Aithal]'''
 +
** Design, Development and Implementation of Game Algorithm
 +
 
 +
* '''[https://www.linkedin.com/in/vignesh-kumar-v/ Vignesh "Viper" Kumar Venkateshwar]'''
 +
** PCB Design and Driver Implementation for Joystick Module
  
 
== Schedule ==
 
== Schedule ==
Line 58: Line 63:
  
 
! scope="row"| 2
 
! scope="row"| 2
| 09/30/2018
+
| 09/29/2018
 
|  
 
|  
 
* Had detailed discussion on second set of project ideas involving LED Matrix.
 
* Had detailed discussion on second set of project ideas involving LED Matrix.
Line 89: Line 94:
 
|
 
|
 
* 10/14/2018
 
* 10/14/2018
|-  
+
|
 +
|-
 +
 
 
! scope="row"| 5
 
! scope="row"| 5
 
| 10/20/2018
 
| 10/20/2018
Line 101: Line 108:
 
* 10/21/2018
 
* 10/21/2018
 
|
 
|
|-  
+
|-
 +
 
 
! scope="row"| 6
 
! scope="row"| 6
 
| 10/27/2018
 
| 10/27/2018
Line 119: Line 127:
 
|
 
|
 
* Discussed challenges in writing the complete driver for LED Matrix, switched to the approach of modifying pre-existing library as per requirements.
 
* Discussed challenges in writing the complete driver for LED Matrix, switched to the approach of modifying pre-existing library as per requirements.
* Finalized Joystick module for input,snake control and placed order.
+
* Finalized Joystick module as input for snake control and placed order.
 
|
 
|
 
*Completed
 
*Completed
Line 142: Line 150:
 
|-  
 
|-  
 
   
 
   
! scope="row"| 9
+
! scope="row"| 8
 
| 11/13/2018
 
| 11/13/2018
 
|  
 
|  
* To read the datasheet of Nordic wireless and understand high level API's used.  
+
* To read the datasheet of Nordic wireless and understand high level API's used. Realized need for Quad band Antennas and placed order.  
 
* Finalize components to be on PCB and kickstart with schematic.
 
* Finalize components to be on PCB and kickstart with schematic.
 
|
 
|
*In Progress
+
*Completed
*In Progress
+
*Completed
 +
|
 +
* 11/16/2018
 
|
 
|
 
|-  
 
|-  
 
   
 
   
! scope="row"| 10
+
! scope="row"| 9
 
| 11/17/18
 
| 11/17/18
 
|  
 
|  
*  
+
* Develop basic driver to transfer data wirelessly between two SJone boards.
*  
+
* Test working of Snake game by utilizing pushbutton switches on SJONE board.
 +
* Review schematic and PCB layout designs of previous years projects.
 
|
 
|
*  
+
* Completed
*  
+
* Completed
 +
* Completed
 +
|
 +
* 11/19/2018
 +
* 11/19/2018
 +
* 11/19/2018
 
|
 
|
 
|-
 
|-
  
! scope="row"| 11
+
! scope="row"| 9
 +
| 11/20/18
 +
|
 +
* Review and test Nordic wireless driver for successful data transfer between two boards.
 +
* Schematic and Layout design on Eagle.
 +
|
 +
* Completed
 +
* Completed
 +
|
 +
* 11/21/2018
 +
|
 +
|-
 +
 
 +
! scope="row"| 9
 +
| 11/22/18
 +
|
 +
* Integrate driver of Joystick with Nordic to wirelessly transmit control movements to recipient board.
 +
* Schematic and Layout design on Eagle.
 +
|
 +
* Completed
 +
* Completed
 +
|
 +
* 11/23/2018
 +
* 11/23/2018
 +
|
 +
|-
 +
 
 +
! scope="row"| 9
 +
| 11/22/18
 +
|
 +
* Implement Start Screen and logic for gameover when Snake head comes in contact with its body/tail.
 +
* Review Schematic and Layout design.
 +
|
 +
* Completed
 +
* Completed
 +
|
 +
* 11/26/2018
 +
* 11/24/2018
 +
|
 +
|-
 +
 
 +
! scope="row"| 10
 
| 11/24/18
 
| 11/24/18
 
|  
 
|  
*  
+
* Design algorithm to display score and implement two modes for playing game with/without screen border.
*  
+
* Generate PCB gerber files and provide it to manufacturer for fabrication.
 +
|
 +
* Completed
 +
* Completed
 
|
 
|
*  
+
* 12/01/2018
*  
+
* 11/30/2018
 
|
 
|
 +
|-
  
 +
! scope="row"| 10
 +
| 11/27/18
 +
|
 +
* Pre-test the complete game diligently and examine errors if any.
 +
* Test the fabricated PCB.
 +
|
 +
* Completed
 +
* Completed
 +
|
 +
* 12/05/18
 +
* 12/07/18
 +
|
 +
|-
 +
 +
! scope="row"| 11
 +
| 12/01/18
 +
|
 +
* Final testing of the game and resolve flaws if any
 +
|
 +
* Completed
 +
|
 +
* 12/14/2018
 +
|
 +
|-
  
|-  
+
! scope="row"| 12
 +
| 12/08/18
 +
|
 +
* Report Completion and Project Demonstration
 +
|
 +
* Completed
 +
|
 +
* 12/19/18
 +
|
 +
|-
  
  
Line 185: Line 279:
 
! scope="col"| Item#
 
! scope="col"| Item#
 
! scope="col"| Part Description
 
! scope="col"| Part Description
! scope="col"| Manufacturer
+
! scope="col"| Vendor
 
! scope="col"| Qty
 
! scope="col"| Qty
 
! scope="col"| Cost
 
! scope="col"| Cost
Line 191: Line 285:
 
! scope="row"| 1
 
! scope="row"| 1
 
| SJOne LPC1758 Microcontroller Board
 
| SJOne LPC1758 Microcontroller Board
| Preet
+
| [http://socialledge.com/sjsu/index.php/SJ_One_Board Preet]
 
| 2
 
| 2
 
| $160.00
 
| $160.00
Line 197: Line 291:
 
! scope="row"| 2
 
! scope="row"| 2
 
| 32x32 RGB LED Matrix
 
| 32x32 RGB LED Matrix
| Amazon [https://www.amazon.com/Adafruit-Medium-32x32-matrix-panel/dp/B00KHBJPIK]
+
| [https://www.amazon.com/Adafruit-Medium-32x32-matrix-panel/dp/B00KHBJPIK/ Amazon]
 
| 1
 
| 1
 
| $56.00
 
| $56.00
Line 203: Line 297:
 
! scope="row"| 3
 
! scope="row"| 3
 
| 5V/4A Power Adapter
 
| 5V/4A Power Adapter
| Amazon [https://www.amazon.com/SMAKN-Switching-Supply-Adapter-100-240/dp/B01N4HYWAM/ref=sr_1_5?ie=UTF8&qid=1541552059&sr=8-5&keywords=5v+4a+power+supply]
+
| [https://www.amazon.com/SMAKN-Switching-Supply-Adapter-100-240/dp/B01N4HYWAM/ref=sr_1_5?ie=UTF8&qid=1541552059&sr=8-5&keywords=5v+4a+power+supply/ Amazon]
 
| 1
 
| 1
 
| $8.99
 
| $8.99
Line 209: Line 303:
 
! scope="row"| 4
 
! scope="row"| 4
 
| DC Barrel Jack Adapter - Female  
 
| DC Barrel Jack Adapter - Female  
| Fry's Electronics [https://www.frys.com/product/7726878?store=6&source=google&gclid=EAIaIQobChMIx8aT2brZ3gIVih-tBh1s3QONEAQYASABEgIyOfD_BwE]
+
| [https://www.frys.com/product/7726878?store=6&source=google&gclid=EAIaIQobChMIx8aT2brZ3gIVih-tBh1s3QONEAQYASABEgIyOfD_BwE/ Fry's Electronics]
 
| 1
 
| 1
 
| $2.90
 
| $2.90
 
|-
 
|-
 +
 
! scope="row"| 5
 
! scope="row"| 5
 
| Osepp Joystick Module
 
| Osepp Joystick Module
| Fry's Electronics [https://www.frys.com/product/8950063?store=6&source=google&gclid=EAIaIQobChMI27PnirXZ3gIVeB-tBh2IIgPTEAQYASABEgLdR_D_BwE]
+
|[https://www.frys.com/product/8950063?store=6&source=google&gclid=EAIaIQobChMI27PnirXZ3gIVeB-tBh2IIgPTEAQYASABEgLdR_D_BwE/ Fry's Electronics]
 
| 1
 
| 1
 
| $6.54
 
| $6.54
 
|-
 
|-
 +
 
! scope="row"| 6
 
! scope="row"| 6
! Total
+
| Right Angle Mini Quad Band Antenna
 +
|[https://www.amazon.com/Antenna-Quad-Right-Angle-Cellular/dp/B00N4Y2C4G/ref=sr_1_3?ie=UTF8&qid=1543091909&sr=8-3&keywords=Antenna-Quad-Right-Angle-Cellular/ Amazon]
 +
| 2
 +
| $16.52
 +
|-
 +
 
 +
! scope="row"| 7
 +
| PCB
 +
|[https://jlcpcb.com/ JLC PCB]
 +
| 5
 +
| $19.36
 +
|-
 +
 
 +
! scope="row"| 8
 +
| PCB Components
 +
|[https://www.digikey.com/ Digikey]
 +
| 5
 +
| $20.96
 +
|-
 +
!
 +
!Total
 
!-
 
!-
 
!-
 
!-
! $234.43
+
! $291.27
 
|}
 
|}
  
 
== Design & Implementation ==
 
== Design & Implementation ==
The design section can go over your hardware and software design.  Organize this section using sub-sections that go over your design and implementation.
 
  
 
=== Hardware Design ===
 
=== Hardware Design ===
Discuss your hardware design here. Show detailed schematics, and the interface here.
+
 
 +
The design flow diagram of the project is shown below.
 +
 
 +
[[File:FlowDiagram.jpg|thumb|1000px|center|Design Flow Diagram]] <br>
 +
 
 +
The Hardware design for baem geim involves the use of two SJone LPC1758 microcontroller boards, 32x32 RGB LED Matrix, Joystick module, Nordic Wireless Transceiver and Active Buzzer as described below. Pin configuration information for integration with PCB and power source have been detailed.
 +
 
 +
'''LED Matrix'''
 +
 
 +
[[File:LED_Matrix.png|thumb|450x550px|left|RGB LED Matrix Organization]]
 +
 
 +
The Project uses 32x32 6mm pitch RGB LED Matrix consisting of 1024 RGB LEDs.
 +
32 rows of LED matrix have been divided into 16 interleaved sections/strips. The first section is the 1st 'row' and 16th 'row' (32 x 2 RGB LEDs = 64 RGB LEDs) the second section is 2nd 'row' and 17th 'row' and so on.
 +
On the PCB is 12 LED driver chips. These are like 74HC595s but they have 16 outputs and they are constant current. 16 outputs * 12 chips = 192 LEDs that can be controlled at once, and 64 * 3 (R G and B) = 192. So now the design comes together. We can have 192 outputs that can control one line at a time, with each of 192 R, G and B LEDs either on or off. The LPC1758 controller selects which section to currently draw (using A, B, C and D address pins - 4 bits can have 16 values). Once the address is set, the controller clocks out 192 bits of data (24 bytes) and latches it. Then it increments the address and clocks out another 192 bits, etc until it gets to address #15, then it sets the address back to #0.
 +
To light up an individual pixel, appropriate row value is loaded on to the address pins A,B,C & D, Clock is set out to traverse the row
 +
and when required pixel is reached, the latch is set high to turn ON the LED.
 +
<br>
 +
We have used the Adaafruit GFX libaray. In this library a 3D array of size 32*32*3= 3072 bytes is used. This 3D array basically represents the whole matrix with the three RGB values. Each function in the library takes the arguments of rows and columns and just makes the corresponding byte high in the matrix. The update display function is called in a repetitive interrupt function. This function loads travesses the 3D array and selects the appropriate row and loads the row using clk pin and finally latches in the value. <br>
 +
Tip: Try to get drawpixel function working first.
 +
<br>
 +
<br>
 +
[[File:wireconnection.jpeg|550x450px|thumb|right|LED Matrix Connection to SJone Board]]
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
'''Technical Specification:'''<br>
 +
Dimensions: 190.5mm x 190.5mm x 14mm / 7.5" x 7.5" x 0.55"<br>
 +
Panel weight with IDC cables and power cable: 357.51g<br>
 +
5V regulated power input, 4A max (all LEDs on)<br>
 +
5V data logic level input<br>
 +
2000 mcd LEDs on 6mm pitch<br>
 +
1/16 scan rate<br>
 +
Indoor display, 150 degree visibility<br>
 +
<br>
 +
 
 +
'''Joystick'''<br>
 +
[[File:JoysticktoSJone.jpeg|450x400px|thumb|left|Pin Connection of Joystick module to SJone]]
 +
The project uses a 2-axis self centered joystick module from osepp. Its goal is to communicate motion in 2D to SJone board. This is achieved by using two independent 10K potentiometers (one per axis) which can be used as dual adjustable voltage dividers, providing 2-axis analog input in a control stick form. The Joystick stick design consists of Gimbal mechanism along with two potentiometers. When joystick is rotated, it moves a narrow rod, that sits in two rotatable shafts (Gimbal). One of the shafts allows motion in the X-axis(left-right) while the other allows motion in the Y-axis (up-down). Tilting it forward and backward pivots the Y-axis shaft, while tilting it left to right pivots X-axis shaft. Moving it diagonally pivots both shafts.
 +
To read the joystick's physical position, we need to measure the change in resistance of potentiometer connected to each joystick shaft. The change can be read by LPC microcontroller's analog pin using ADC. With the microcontroller's 12-bit resolution, the values on each analog channel(axis) can vary from 0 to 4095. Thus, if the stick is moved on X-axis from one end to the other, the values change from 0 to 4095 and similar thing happens when it's moved along Y-axis. At Joystick's default position, its value is about 2048.
 +
<br>
 +
<br>
 +
[[File:Joystick_Schematic.jpg|thumb|450px|right|Joystick Schematic]]
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>'''Technical Specification:'''<br>
 +
Operating voltage output : 3.3V – 5V MAX<br>
 +
Analog input<br>
 +
two 10K potentiometers with common ground per axis<br>
 +
Spring auto return to center on knob<br>
 +
Operating temp range: +32°F to +158°F (0°C to +70°C)<br>
 +
Unit Weight: 4 oz
 +
 
 +
<br>
 +
 
 +
'''Printed Circuit Board (PCB)'''<br>
 +
The PCB schematic and Layout have been designed using Eagle 9.2.2. The circuit board consists of single power supply which will be powering
 +
* Transmitter SJone Board (5.0V)
 +
* Receiver SJOne Board (5.0V)
 +
 
 +
The Circuit uses LM7805 Voltage regulator IC from Texas Instruments to meet the power requirements. The output of voltage regulator will be typically 5V, 1.5A
 +
thus preventing excess current to SJone boards. 9V, 4A adapter was used to power the LED Matrix. The LED Matrix which comes with dual Molex headers. Each of the two were safely connected to LED Matrix and Circuit board to power them accordingly. The circuit design has been made robust and versatile. The voltage regulator uses decoupling capacitors placed in parallel at input and output. They bypass the AC component at the input and improve the transient response at the output. Further, a diode has been connected in shunt across the regulator to provide reverse bias protection.
 +
 
 +
The circuit board also has provisions to LED Matrix, SJone board (Output Module) and Joystick module for integration.
 +
[[File:Schematic.jpg|1000px|thumb|center|Circuit Board Schematic]]
 +
{|
 +
|[[File:Layout.jpg|540px|thumb|left|Circuit Layout]]
 +
|[[File:ManufacturedPCB.png|625px|thumb|right|Fabricated PCB]]
 +
|}
  
 
=== Hardware Interface ===
 
=== Hardware Interface ===
In this section, you can describe how your hardware communicates, such as which BUSes used. You can discuss your driver implementation here, such that the '''Software Design''' section is isolated to talk about high level workings rather than inner working of your project.
+
'''Nordic Wireless'''<br>
 +
The project uses nRF24L01+ single chip 2.4GHZ transceiver rooted on SJone board to establish wireless communication between input and output modules. It uses embedded baseband protocol engine (Enhanced ShockBurst™), suitable for ultra low power wireless applications. The nRF24L01+ is designed for operation in the world wide ISM frequency band at 2.400 - 2.4835GHz. It is configured and operated through Serial Peripheral Interface (SPI0) of LPC1758 microcontroller. The embedded baseband protocol engine (Enhanced ShockBurst™) is based on packet communication and supports various modes from manual operation to advanced autonomous protocol operation. Internal FIFOs ensure a smooth data flow between the radio front end and the system’s MCU. Enhanced Shock-Burst™ reduces system cost by handling all the high speed link layer operations. The radio front end uses GFSK modulation. It consists of 126 different RF channels, which gives a possibility to have a network of 125 independently working modems in one place. It has a power consumption of about 12mA during transmission which is even lower than a single LED. Its operating voltage is between 1.8 and 3.7V.
 +
 
 +
=== Software Design and Implementation ===
 +
 
 +
The general game flow of our project can be represented by the following flow chart.
 +
 
 +
[[File:Game_flow.jpg|thumb|600px|center|Program Flow]]
 +
 
 +
=== Tasks ===
 +
The game design consists of three independent tasks. First task polls on the user to enter the mode of the game (Normal/Arcade) and has been given the highest priority. The second task consists of draw pixel logic to generate the snake on the display console. The third task repeatedly polls for input from joystick which is used as reference for the snake motion.
 +
 
 +
===== Logic Task =====
 +
 
 +
Logic task calculates the length of snake body by using the snake's head as a reference. It also updates the row and column coordinates based on the joystick task input. The spawning of ordinary fruit, bonus fruit and evil fruit along with conditions when any of these fruits are eaten by the snake are also handled in this task. Finally, it checks for the game over condition.
 +
 
 +
===== Snake Body Generation =====
 +
This section of code is used to compute the coordinates of the snake's body. The X-Y coordinates of the snake's head at any particular instant is considered  as a reference to generate the snake body. The current instance of snake head is copied to a register and a loop is run to the length of the snake body. Finally, coordinate values at every index is pushed back by one.<br>
 +
The code snippet for the snake's body generation is given below:<br>
 +
 
 +
<pre>
 +
    int prevX = tailX[0];          // tailX and tailY represents co-ordinates of the snake's body
 +
    int prevY = tailY[0];       
 +
    tailX[0] = x;
 +
    tailY[0] = y;
 +
    int prev2X, prev2Y;                 
 +
    for (int i = 1; i < nTail; i++) {
 +
        prev2X = tailX[i];
 +
        prev2Y = tailY[i];
 +
        tailX[i] = prevX;
 +
        tailY[i] = prevY;
 +
        prevX = prev2X;
 +
        prevY = prev2Y;
 +
</pre>
 +
 
 +
===== Snake Direction =====
 +
The direction of input for snake is obtained from Joystick. Based on the input, new coordinates are computed to determine next state direction of snake. Boundary conditions have been taken into consideration for Normal and Arcade mode.<br>
 +
'''Pseudo Code:'''
 +
<pre>
 +
switch (dir)
 +
    {
 +
        case LEFT:
 +
            y--;
 +
            if(y<0)
 +
            {
 +
                y=width-1;
 +
            }
 +
 
 +
            break;
 +
 
 +
        case RIGHT:
 +
            y++;
 +
 
 +
            if(y>width-1)
 +
            {
 +
                y=0;
 +
            }
 +
            break;
  
=== Software Design ===
+
        case UP:
Show your software design.  For example, if you are designing an MP3 Player, show the tasks that you are using, and what they are doing at a high level.  Do not show the details of the code.  For example, do not show exact code, but you may show psuedocode and fragments of code.  Keep in mind that you are showing DESIGN of your software, not the inner workings of it. 
+
            x--;
 +
            if(x<0)
 +
            {
 +
                x=width-1;
 +
            }
 +
            break;
  
=== Implementation ===
+
        case DOWN:
This section includes implementation, but again, not the details, just the high level. For example, you can list the steps it takes to communicate over a sensor, or the steps needed to write a page of memory onto SPI FlashYou can include sub-sections for each of your component implementation.
+
            x++;
 +
            if(x>width-1)
 +
            {
 +
                x=0;
 +
            }
 +
            break;
 +
        case STOP:
 +
            indexX = x;
 +
            indexY = y;
 +
  }
 +
</pre>
 +
 
 +
===== Game Over Logic =====
 +
In arcade mode, if the snake's head collides either with itself or wall border, the gameover function is called which will print the score. In normal mode, gameover condition is determined only when the snake comes in contact with its own body.<br>
 +
'''Pseudo code:'''
 +
<pre>
 +
//Gameover condition for arcade mode
 +
        if(snake's head touches the wall)
 +
        {
 +
            set 'gameover' flag to true;
 +
           
 +
            Gameover();                                    //this function displays scores
 +
        }
 +
        for (int i = 1; i < snake's body; i++) {
 +
            if (snakes's head == snake's body)            // snake eating itself
 +
            {
 +
              set 'gameover' flag to true;
 +
           
 +
            Gameover();                                    //this function displays scores
 +
            }
 +
 
 +
//Gameover condition for normal mode
 +
 
 +
        for (int i = 1; i < snake's body; i++) {
 +
            if (snakes's head == snake's body)            // snake eating itself
 +
            {
 +
              set 'gameover' flag to true;
 +
           
 +
            Gameover();                                    //this function displays scores
 +
            }
 +
</pre>
 +
 
 +
==== Draw Task ====
 +
Two LED matrix coordinates in a loop scale through all possible rows and columns, at an instance when LED Matrix coordinates equalizes with coordinates of snake head , the corresponding LED pixel(s) is/are turned ON and other pixels are turned OFF.<br>
 +
'''Pseudo code:'''
 +
<pre>
 +
      if (LED Matrix coordinates == snake head's coordinate)                     
 +
      {
 +
          glow_that_pixel;
 +
      }
 +
      else {
 +
                bool print = false;
 +
                for (int k = 0; k < nTail; k++) {
 +
                    if (LED matrix's coordinates == snake body's coordinate) 
 +
                      {
 +
                          glow_those_corresponding_pixels;
 +
                          print = true;
 +
                      }
 +
                }
 +
                if(!print)
 +
                  all_other_pixels_turned_off;
 +
      }
 +
</pre>
 +
 
 +
===== Joystick Task =====
 +
This task receives the direction input for the joystick at delay interval of time. Nordic wireless API is used for wireless reception of the joystick's direction.A record of previous direction of the snake movement is taken into consideration to avoid overlapping of the snake when opposite direction is input.<br>
 +
'''Pseudo code:'''
 +
<pre>
 +
    wireless_get_rx_pkt(&direction, delay);
 +
    if(direction == UP)
 +
    {
 +
        if(precious_direction != DOWN)            //prevents conflict of opposite directions
 +
        {
 +
            direction = UP;
 +
            precious_direction = UP;
 +
        }
 +
        pause_flag = false;
 +
    }
 +
 
 +
    else if(direction == DOWN)
 +
    {
 +
        if(precious_direction != UP)            //prevents conflict of opposite directions
 +
        {
 +
            direction = DOWN;
 +
            precious_direction = DOWN;
 +
        }
 +
        pause_flag = false;
 +
    }
 +
 
 +
    else if(direction == LEFT)
 +
    {
 +
        if(precious_direction != RIGHT)            //prevents conflict of opposite directions
 +
        {
 +
            direction = LEFT;
 +
            precious_direction = LEFT;
 +
        }
 +
        pause_flag = false;
 +
    }
 +
   
 +
    else if(direction == RIGHT)
 +
    {
 +
        if(precious_direction != LEFT)            //prevents conflict of opposite directions
 +
        {
 +
            direction = RIGHT;
 +
            precious_direction = RIGHT;
 +
        }
 +
        pause_flag = false;
 +
    }
 +
     
 +
    }
 +
 
 +
    else if(direction == RESET)                          //Condition to reset the game
 +
    {
 +
        if(gameover_flag == false  && pause_flag==false)
 +
        {
 +
            direction = STOP;
 +
            pause_flag = true;
 +
            direction = previous_direction;
 +
        }
 +
        else if(gameover_flag == true)
 +
        {
 +
            clear_screen();
 +
            StartScreen();
 +
            Setup();
 +
 
 +
        }
 +
    }
 +
</pre>
 +
 
 +
===== Thumb Joystick - SJOne Board interface =====
 +
The thumb joystick communicates with LPC1758 controller via ADC pins. An ADC driver is designed which enables the joystick to be interfaced with the input module .Each of the 2 axes of the joystick makes use of channels 4 and 5 of the ADC respectively. Once the direction input is provided by the user, nordic wireless API wirelessly transmits the data to the output module.
 +
 
 +
'''Pseudo Code:'''
 +
 
 +
<pre>
 +
 
 +
void initialize_ADC_driver()
 +
{
 +
 
 +
  power up the ADC peripheral;
 +
  make A/D converter operational;
 +
  set clock frequency to 12Mhz;
 +
  select channel 4 and 5;
 +
  enable burst mode;
 +
  set channel 4 for x-axis;
 +
  set channel 5 for y-axis;
 +
 
 +
}
 +
 
 +
void joystick_actions()
 +
{
 +
 
 +
  if(x_axis_value > 3500)
 +
    {
 +
   
 +
      set 'directions' enum to right;
 +
      wireless_send(direction); // Nordic wireless 'send' API
 +
    }
 +
 
 +
  else if(x_axis_value < 200)
 +
    {
 +
 
 +
      set ''directions' enum to left;
 +
      wireless_send(direction);
 +
    }
 +
 
 +
  else if(y_axis_value < 3500)
 +
    {
 +
 
 +
      set ''directions' enum to up;
 +
      wireless_send(direction);
 +
    }
 +
 
 +
  else if(y_axis_value < 200)
 +
    {
 +
 
 +
      set ''directions' enum to down;
 +
      wireless_send(direction);
 +
    }
 +
 
 +
  else
 +
    {
 +
       
 +
        do nothing;
 +
    }
 +
}
 +
 +
</pre>
 +
 
 +
=== Features: ===
 +
 
 +
* '''Game Modes''': Normal Mode (Wall absent) and Arcade Mode (Wall present). <br>
 +
* '''Game Pause''': Game pauses if reset button is pressed during gameplay. <br>
 +
* '''Game Reset''': Game resets if reset button is pressed after game over condition. <br>
 +
* '''Sound''': Active buzzer used for generating sound during gameplay. <br>
 +
* '''Bonus Fruit''': When eaten, score increases by 6 and snake's body increases by 2. <br>
 +
* '''Evil Fruit''': When eaten, score decreases by 8 and snake's body increases by 2. <br/>
 +
 
 +
{|
 +
|[[File:StartScreen.gif|thumb|left|Start Screen]]
 +
|[[File:Normal.gif|thumb|Normal Mode of Gameplay]]
 +
|[[File:Arcade.gif|thumb|right| Arcade Mode of Gameplay]]
 +
|}
  
 
== Testing & Technical Challenges ==
 
== Testing & Technical Challenges ==
Describe the challenges of your project.  What advise would you give yourself or someone else if your project can be started from scratch again?
+
===== LED Matrix =====
Make a smooth transition to testing section and described what it took to test your project.
+
* Non-availability of datasheet for ADAFRUIT 32x32 RGB LED Matrix was a major challenge. This made the understanding of pin configuration details difficult. Subsequently, we extracted ADAFRUIT GFX library and created an interface class to make it compatible with our project requirements.
 +
* It was hard to determine the refresh rate for LED Matrix. Low refresh rate would cause display to flicker and a high refresh rate would slow down the display update rate thereby causing overhead. Through trial and error, we came up with a value of 500us for RIT interrupt which was an ideal refresh rate value.
 +
*In order to creatively enhance the game visual at start screen, we decided to display an image of a snake. We generated a bitmap using an 8-bit snake image. We manually determined the appropriate pixel position of the bitmap and provided the pixel colour. This was a tedious task as it had to done through trial and error.
 +
 
 +
===== Nordic Wireless Module and Joystick =====
 +
*Initially, there was a 2-3 second delay to reflect change of direction of the snake received at the output module from the input joystick module. It was hard to determine the cause of delay. After diligent analysis, we found that the task delay at producer and consumer tasks were different which has caused the delay. This was fixed by using the same task delay to achieve synchronization between input and change in snake direction.
 +
 
 +
===== Game Algorithm =====
 +
* It was hard to decide upon the approach to represent the snake's tail and display its real time motion on the screen. We finally came up with the logic of using an array where the first element of the array always indicated the snake's head and subsequent array elements indicated its body. Its real time movement was captured by pushing the elements of the array to the length of the snake's body.
 +
* It was great challenge to implement the pause feature in the game. We had to capture the real time movement of the snake when the user hit pause and resume the game from same position. The push button switch on joystick was used to pause and game would resume with input direction. Initially, when direction opposite to that of current snake's movement was given, it used to reset the game. This was overcome by capturing instant before pause and then resuming from the same instant when direction was input.
 +
* In order to display the player's score on game over, the integer value had to be converted to character and then displayed. Initially, while doing so, redundant values were also displayed along with the score. This was overcome by using ostringstream class which creates a buffer of type string and returns a string pointer. This was followed with the use of cstr function which outputs character to be printed on the console.
 +
* Starting the game with initial snake body length was challenging due to the designed algorithm. This was overcome by calculating the snake body before start of the game by hard-coding the default direction on game start.
  
Include subsections that list out a problem and solution, such as:
+
===== PCB =====
 +
* Approach to design a PCB to meet the project requirements was complex. We had to redesign the circuit schematic and layout thrice to ensure that it was robust. Moreover, We had to import libraries from Sparkfun and adafruit for various components and also make sure that the same were available with Digikey.
  
=== <Bug/issue name> ===
+
==== <Bug/issue name> ====
Discuss the issue and resolution.
+
* The LED Matrix display was intermittently flickering and we couldn't narrow down the reason behind it. Nothing was changed in the code, so we suspect the cause to be unstable circuit connections between output module and PCB.
 +
* The top-leftmost LED blinks whenever the snake eats the fruit in the game. This was bug which couldn't be resolved.
 +
* The PCB was designed presuming all 34 pins on SJone board could be interchangeably as GPIO for the LED Matrix. Post fabrication with some of the connections hard-grounded for LED Matrix, it couldn't be used interchangeably. As a result we had to manually connect the wires from PCB to LED Matrix.
  
 
== Conclusion ==
 
== Conclusion ==
Conclude your project here. You can recap your testing and problems. You should address the "so what" part here to indicate what you ultimately learnt from this project. How has this project increased your knowledge?
+
The project was a success both in terms of output and learnings. We were able to design Snake game which has been controlled wirelessly using handheld joystick. It provided an exposure of developing drivers for 32x32 RGB LED Matrix, Nordic Wireless, Joystick, active buzzer and accelerometer. It helped us utilize the knowledge of FreeRTOS and GPIO driver gained through the CMPE-244 class.
 +
 
 +
Working as a team and having brainstorming sessions helped us in overcoming a lot bugs which we encountered during game design. Furthermore, it helped in expediting the integration, testing and debugging of the project days before final demonstration. On the whole, we had a wonderful experience of working in a team and developing a complete product using professional tools and techniques.
  
 
=== Project Video ===
 
=== Project Video ===
Upload a video of your project and post the link here.
+
* [https://youtu.be/0eNtQu49sGY/Baem Geim video]
  
 
=== Project Source Code ===
 
=== Project Source Code ===
* [https://sourceforge.net/projects/sjsu/files/CmpE_S2016/ Sourceforge Source Code Link]
+
* [https://github.com/NeerajDhavale/Baem-Geim/ Source Code GitHub]
  
 
== References ==
 
== References ==
 
=== Acknowledgement ===
 
=== Acknowledgement ===
Any acknowledgement that you may wish to provide can be included here.
+
We would like to sincerely thank Professor Preetpal Kang for his allround guidance, feedback and support. His classroom lectures were significant in imparting knowledge on embedded systems. Further, we would like to thank ISA team for their advice.
  
 
=== References Used ===
 
=== References Used ===
List any references used in project.
+
[1] [https://cdn-learn.adafruit.com/downloads/pdf/32x16-32x32-rgb-led-matrix.pdf?timestamp=1543806512/ Adafruit 32x32 RGB Matrix] <br>
 +
[2] [https://lastminuteengineers.com/joystick-interfacing-arduino-processing/ 2-Axis Joystick Tutorial] <br>
 +
[3] [https://www.sparkfun.com/datasheets/Components/SMD/nRF24L01Pluss_Preliminary_Product_Specification_v1_0.pdf Nordic Wireless Datasheet]<br>
 +
[4] [http://socialledge.com/sjsu/index.php/Low_Powered_Mesh_Network_stack Low Powered Mesh Network Stack]<br>
  
 
=== Appendix ===
 
=== Appendix ===
You can list the references you used.
 

Latest revision as of 08:48, 18 December 2019

Snake

Project Title

baem geim - Snake Game Reborn

Snake.jpg

Abstract

baem geim is a revived, amusing Snake Game where a player maneuvers a line which grows in length, with the line itself and wall as obstacle. The concept originated in the 1976 arcade game Blockade, and the ease of implementing snake has led to hundreds of versions for many platforms. This is a new version of snake game which we have developed. The player controls the dot using handheld joystick. As it moves forward, it leaves a trail behind, resembling a moving snake. The snake movement is visually displayed in real time on a 32*32 LED Matrix. The player attempts to eat randomly generated dots as fruits by running into it with the head of the snake. Each fruit eaten increases the length of the snake and controlling it becomes progressively difficult. The player loses the game if the snake runs into itself or hits the screen border. The game is designed using two SJone LPC1758 microcontroller boards one each to drive LED Matrix and Joystick module respectively. They communicate wirelessly through RF Nordic transceiver. An additional feature is the use of active buzzer for sound effects.

Objectives & Introduction

The main objective of the project is to develop a 2D single player snake game. Other milestones to achieve the objective are as below.

  • Visually display the movement of the snake on LED Matrix in real time.
  • Control the movement of the Snake through Joystick.
  • Transmit the control data wirelessly from input to output console through Nordic transceiver.
  • Design PCB for distribution of input power appropriately between two SJone boards.

The project is broadly divided into two modules.

1. Input Module: The input module connects SJone board to two-axis joystick which is used to control the snake advancement. Nordic transmitter continuously transmits control packets to the output module.

2. Output Module: The output module connects another SJone board to LED Matrix for visual display. It recursively polls to receive control packets from the input module and updates the snake movement as per user's input.

Team Members & Responsibilities

Schedule

Week# Date Task Status Actual Completion Date
1 09/22/2018
  • Reviewed previous projects of CMPE 244 on Socialledge.
  • Brainstormed ideas and came up with 3 proposals.
  • Completed
  • Completed
  • 09/25/2018
2 09/29/2018
  • Had detailed discussion on second set of project ideas involving LED Matrix.
  • Came up with 2 game projects, prepared abstracts and submitted them for review.
  • Completed
  • Completed
  • 10/02/2018
3 10/06/2018
  • With approved baem geim proposal, discussed approach and list of components.
  • Prepared schedule for project flow.
  • Completed
  • Completed
  • 10/07/2018
4 10/13/2018
  • Division project into modules and assignment of responsibilities for each module to individual team member.
  • Order of essential components to kickstart project work.
  • Completed
  • Completed
  • 10/14/2018
5 10/20/2018
  • Tested LED Matrix using Dash charger, however power was insufficient as a result of which random patterns were generated.
  • Realized requirement of Female Barrel Jack adapter and 5V/4A power supply for complete illumination.
  • Completed
  • Completed
  • 10/21/2018
6 10/27/2018
  • Initiated driver for LED Matrix to display characters/desired pattern.
  • Systematically drafted steps for flow of the game and initiated design of algorithm for snake movement.
  • Completed
  • Completed
  • 11/14/2018
  • 11/10/2018
7 11/03/2018
  • Discussed challenges in writing the complete driver for LED Matrix, switched to the approach of modifying pre-existing library as per requirements.
  • Finalized Joystick module as input for snake control and placed order.
  • Completed
  • Completed
  • 11/04/2018
8 11/10/2018
  • Understand working of Joystick Module and implement simple driver to control directions and push button for game start.
  • Download EagleCAD software for design of schematic and layout. Further explore various options available.
  • Completed
  • Completed
  • 11/15/2018
  • 11/12/2018
8 11/13/2018
  • To read the datasheet of Nordic wireless and understand high level API's used. Realized need for Quad band Antennas and placed order.
  • Finalize components to be on PCB and kickstart with schematic.
  • Completed
  • Completed
  • 11/16/2018
9 11/17/18
  • Develop basic driver to transfer data wirelessly between two SJone boards.
  • Test working of Snake game by utilizing pushbutton switches on SJONE board.
  • Review schematic and PCB layout designs of previous years projects.
  • Completed
  • Completed
  • Completed
  • 11/19/2018
  • 11/19/2018
  • 11/19/2018
9 11/20/18
  • Review and test Nordic wireless driver for successful data transfer between two boards.
  • Schematic and Layout design on Eagle.
  • Completed
  • Completed
  • 11/21/2018
9 11/22/18
  • Integrate driver of Joystick with Nordic to wirelessly transmit control movements to recipient board.
  • Schematic and Layout design on Eagle.
  • Completed
  • Completed
  • 11/23/2018
  • 11/23/2018
9 11/22/18
  • Implement Start Screen and logic for gameover when Snake head comes in contact with its body/tail.
  • Review Schematic and Layout design.
  • Completed
  • Completed
  • 11/26/2018
  • 11/24/2018
10 11/24/18
  • Design algorithm to display score and implement two modes for playing game with/without screen border.
  • Generate PCB gerber files and provide it to manufacturer for fabrication.
  • Completed
  • Completed
  • 12/01/2018
  • 11/30/2018
10 11/27/18
  • Pre-test the complete game diligently and examine errors if any.
  • Test the fabricated PCB.
  • Completed
  • Completed
  • 12/05/18
  • 12/07/18
11 12/01/18
  • Final testing of the game and resolve flaws if any
  • Completed
  • 12/14/2018
12 12/08/18
  • Report Completion and Project Demonstration
  • Completed
  • 12/19/18

Parts List & Cost

Item# Part Description Vendor Qty Cost
1 SJOne LPC1758 Microcontroller Board Preet 2 $160.00
2 32x32 RGB LED Matrix Amazon 1 $56.00
3 5V/4A Power Adapter Amazon 1 $8.99
4 DC Barrel Jack Adapter - Female Fry's Electronics 1 $2.90
5 Osepp Joystick Module Fry's Electronics 1 $6.54
6 Right Angle Mini Quad Band Antenna Amazon 2 $16.52
7 PCB JLC PCB 5 $19.36
8 PCB Components Digikey 5 $20.96
Total - - $291.27

Design & Implementation

Hardware Design

The design flow diagram of the project is shown below.

Design Flow Diagram

The Hardware design for baem geim involves the use of two SJone LPC1758 microcontroller boards, 32x32 RGB LED Matrix, Joystick module, Nordic Wireless Transceiver and Active Buzzer as described below. Pin configuration information for integration with PCB and power source have been detailed.

LED Matrix

RGB LED Matrix Organization

The Project uses 32x32 6mm pitch RGB LED Matrix consisting of 1024 RGB LEDs. 32 rows of LED matrix have been divided into 16 interleaved sections/strips. The first section is the 1st 'row' and 16th 'row' (32 x 2 RGB LEDs = 64 RGB LEDs) the second section is 2nd 'row' and 17th 'row' and so on. On the PCB is 12 LED driver chips. These are like 74HC595s but they have 16 outputs and they are constant current. 16 outputs * 12 chips = 192 LEDs that can be controlled at once, and 64 * 3 (R G and B) = 192. So now the design comes together. We can have 192 outputs that can control one line at a time, with each of 192 R, G and B LEDs either on or off. The LPC1758 controller selects which section to currently draw (using A, B, C and D address pins - 4 bits can have 16 values). Once the address is set, the controller clocks out 192 bits of data (24 bytes) and latches it. Then it increments the address and clocks out another 192 bits, etc until it gets to address #15, then it sets the address back to #0. To light up an individual pixel, appropriate row value is loaded on to the address pins A,B,C & D, Clock is set out to traverse the row and when required pixel is reached, the latch is set high to turn ON the LED.
We have used the Adaafruit GFX libaray. In this library a 3D array of size 32*32*3= 3072 bytes is used. This 3D array basically represents the whole matrix with the three RGB values. Each function in the library takes the arguments of rows and columns and just makes the corresponding byte high in the matrix. The update display function is called in a repetitive interrupt function. This function loads travesses the 3D array and selects the appropriate row and loads the row using clk pin and finally latches in the value.
Tip: Try to get drawpixel function working first.

LED Matrix Connection to SJone Board

















Technical Specification:
Dimensions: 190.5mm x 190.5mm x 14mm / 7.5" x 7.5" x 0.55"
Panel weight with IDC cables and power cable: 357.51g
5V regulated power input, 4A max (all LEDs on)
5V data logic level input
2000 mcd LEDs on 6mm pitch
1/16 scan rate
Indoor display, 150 degree visibility

Joystick

Pin Connection of Joystick module to SJone

The project uses a 2-axis self centered joystick module from osepp. Its goal is to communicate motion in 2D to SJone board. This is achieved by using two independent 10K potentiometers (one per axis) which can be used as dual adjustable voltage dividers, providing 2-axis analog input in a control stick form. The Joystick stick design consists of Gimbal mechanism along with two potentiometers. When joystick is rotated, it moves a narrow rod, that sits in two rotatable shafts (Gimbal). One of the shafts allows motion in the X-axis(left-right) while the other allows motion in the Y-axis (up-down). Tilting it forward and backward pivots the Y-axis shaft, while tilting it left to right pivots X-axis shaft. Moving it diagonally pivots both shafts. To read the joystick's physical position, we need to measure the change in resistance of potentiometer connected to each joystick shaft. The change can be read by LPC microcontroller's analog pin using ADC. With the microcontroller's 12-bit resolution, the values on each analog channel(axis) can vary from 0 to 4095. Thus, if the stick is moved on X-axis from one end to the other, the values change from 0 to 4095 and similar thing happens when it's moved along Y-axis. At Joystick's default position, its value is about 2048.

Joystick Schematic






Technical Specification:
Operating voltage output : 3.3V – 5V MAX
Analog input
two 10K potentiometers with common ground per axis
Spring auto return to center on knob
Operating temp range: +32°F to +158°F (0°C to +70°C)
Unit Weight: 4 oz


Printed Circuit Board (PCB)
The PCB schematic and Layout have been designed using Eagle 9.2.2. The circuit board consists of single power supply which will be powering

  • Transmitter SJone Board (5.0V)
  • Receiver SJOne Board (5.0V)

The Circuit uses LM7805 Voltage regulator IC from Texas Instruments to meet the power requirements. The output of voltage regulator will be typically 5V, 1.5A thus preventing excess current to SJone boards. 9V, 4A adapter was used to power the LED Matrix. The LED Matrix which comes with dual Molex headers. Each of the two were safely connected to LED Matrix and Circuit board to power them accordingly. The circuit design has been made robust and versatile. The voltage regulator uses decoupling capacitors placed in parallel at input and output. They bypass the AC component at the input and improve the transient response at the output. Further, a diode has been connected in shunt across the regulator to provide reverse bias protection.

The circuit board also has provisions to LED Matrix, SJone board (Output Module) and Joystick module for integration.

Circuit Board Schematic
Circuit Layout
Fabricated PCB

Hardware Interface

Nordic Wireless
The project uses nRF24L01+ single chip 2.4GHZ transceiver rooted on SJone board to establish wireless communication between input and output modules. It uses embedded baseband protocol engine (Enhanced ShockBurst™), suitable for ultra low power wireless applications. The nRF24L01+ is designed for operation in the world wide ISM frequency band at 2.400 - 2.4835GHz. It is configured and operated through Serial Peripheral Interface (SPI0) of LPC1758 microcontroller. The embedded baseband protocol engine (Enhanced ShockBurst™) is based on packet communication and supports various modes from manual operation to advanced autonomous protocol operation. Internal FIFOs ensure a smooth data flow between the radio front end and the system’s MCU. Enhanced Shock-Burst™ reduces system cost by handling all the high speed link layer operations. The radio front end uses GFSK modulation. It consists of 126 different RF channels, which gives a possibility to have a network of 125 independently working modems in one place. It has a power consumption of about 12mA during transmission which is even lower than a single LED. Its operating voltage is between 1.8 and 3.7V.

Software Design and Implementation

The general game flow of our project can be represented by the following flow chart.

Program Flow

Tasks

The game design consists of three independent tasks. First task polls on the user to enter the mode of the game (Normal/Arcade) and has been given the highest priority. The second task consists of draw pixel logic to generate the snake on the display console. The third task repeatedly polls for input from joystick which is used as reference for the snake motion.

Logic Task

Logic task calculates the length of snake body by using the snake's head as a reference. It also updates the row and column coordinates based on the joystick task input. The spawning of ordinary fruit, bonus fruit and evil fruit along with conditions when any of these fruits are eaten by the snake are also handled in this task. Finally, it checks for the game over condition.

Snake Body Generation

This section of code is used to compute the coordinates of the snake's body. The X-Y coordinates of the snake's head at any particular instant is considered as a reference to generate the snake body. The current instance of snake head is copied to a register and a loop is run to the length of the snake body. Finally, coordinate values at every index is pushed back by one.
The code snippet for the snake's body generation is given below:

    int prevX = tailX[0];          // tailX and tailY represents co-ordinates of the snake's body
    int prevY = tailY[0];         
    tailX[0] = x;
    tailY[0] = y;
    int prev2X, prev2Y;                  
    for (int i = 1; i < nTail; i++) {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
Snake Direction

The direction of input for snake is obtained from Joystick. Based on the input, new coordinates are computed to determine next state direction of snake. Boundary conditions have been taken into consideration for Normal and Arcade mode.
Pseudo Code:

 switch (dir)
    {
        case LEFT:
            y--;
            if(y<0)
            {
                y=width-1;
            }

            break;

        case RIGHT:
            y++;

            if(y>width-1)
            {
                y=0;
            }
            break;

        case UP:
            x--;
            if(x<0)
            {
                x=width-1;
            }
            break;

        case DOWN:
            x++;
            if(x>width-1)
            {
                x=0;
            }
            break;
        case STOP:
            indexX = x;
            indexY = y;
   }
Game Over Logic

In arcade mode, if the snake's head collides either with itself or wall border, the gameover function is called which will print the score. In normal mode, gameover condition is determined only when the snake comes in contact with its own body.
Pseudo code:

//Gameover condition for arcade mode
        if(snake's head touches the wall)
        {
            set 'gameover' flag to true;
            
            Gameover();                                    //this function displays scores
        }
        for (int i = 1; i < snake's body; i++) {
            if (snakes's head == snake's body)             // snake eating itself
            {
               set 'gameover' flag to true;
            
            Gameover();                                    //this function displays scores
            }

//Gameover condition for normal mode

        for (int i = 1; i < snake's body; i++) {
            if (snakes's head == snake's body)             // snake eating itself
            {
               set 'gameover' flag to true;
            
            Gameover();                                    //this function displays scores
            }

Draw Task

Two LED matrix coordinates in a loop scale through all possible rows and columns, at an instance when LED Matrix coordinates equalizes with coordinates of snake head , the corresponding LED pixel(s) is/are turned ON and other pixels are turned OFF.
Pseudo code:

       if (LED Matrix coordinates == snake head's coordinate)                       
       {
           glow_that_pixel;
       }
       else {
                bool print = false;
                for (int k = 0; k < nTail; k++) {
                    if (LED matrix's coordinates == snake body's coordinate)  
                       {
                          glow_those_corresponding_pixels;
                          print = true;
                       }
                }
                if(!print)
                   all_other_pixels_turned_off;
       }
Joystick Task

This task receives the direction input for the joystick at delay interval of time. Nordic wireless API is used for wireless reception of the joystick's direction.A record of previous direction of the snake movement is taken into consideration to avoid overlapping of the snake when opposite direction is input.
Pseudo code:

    wireless_get_rx_pkt(&direction, delay);
    if(direction == UP)
    {
        if(precious_direction != DOWN)             //prevents conflict of opposite directions
        {
            direction = UP;
            precious_direction = UP;
        }
        pause_flag = false;
    }

    else if(direction == DOWN)
    {
        if(precious_direction != UP)             //prevents conflict of opposite directions
        {
            direction = DOWN;
            precious_direction = DOWN;
        }
        pause_flag = false;
    }

    else if(direction == LEFT)
    {
        if(precious_direction != RIGHT)             //prevents conflict of opposite directions
        {
            direction = LEFT;
            precious_direction = LEFT;
        }
        pause_flag = false;
    }
    
    else if(direction == RIGHT)
    {
        if(precious_direction != LEFT)             //prevents conflict of opposite directions
        {
            direction = RIGHT;
            precious_direction = RIGHT;
        }
        pause_flag = false;
    }
      
    }

    else if(direction == RESET)                          //Condition to reset the game
    {
        if(gameover_flag == false  &&  pause_flag==false)
        {
            direction = STOP;
            pause_flag = true;
            direction = previous_direction;
        }
        else if(gameover_flag == true)
        {
            clear_screen();
            StartScreen();
            Setup();

        }
    }
Thumb Joystick - SJOne Board interface

The thumb joystick communicates with LPC1758 controller via ADC pins. An ADC driver is designed which enables the joystick to be interfaced with the input module .Each of the 2 axes of the joystick makes use of channels 4 and 5 of the ADC respectively. Once the direction input is provided by the user, nordic wireless API wirelessly transmits the data to the output module.

Pseudo Code:


void initialize_ADC_driver()
{

   power up the ADC peripheral;
   make A/D converter operational;
   set clock frequency to 12Mhz;
   select channel 4 and 5;
   enable burst mode;
   set channel 4 for x-axis;
   set channel 5 for y-axis;

}

void joystick_actions()
{

  if(x_axis_value > 3500)
    {
    
       set 'directions' enum to right;
       wireless_send(direction); // Nordic wireless 'send' API
    }

  else if(x_axis_value < 200)
    {

       set ''directions' enum to left;
       wireless_send(direction);
    }

  else if(y_axis_value < 3500)
    {

       set ''directions' enum to up;
       wireless_send(direction);
    }

  else if(y_axis_value < 200)
    {

       set ''directions' enum to down;
       wireless_send(direction);
    }

  else
     {
         
        do nothing;
     }
}
 

Features:

  • Game Modes: Normal Mode (Wall absent) and Arcade Mode (Wall present).
  • Game Pause: Game pauses if reset button is pressed during gameplay.
  • Game Reset: Game resets if reset button is pressed after game over condition.
  • Sound: Active buzzer used for generating sound during gameplay.
  • Bonus Fruit: When eaten, score increases by 6 and snake's body increases by 2.
  • Evil Fruit: When eaten, score decreases by 8 and snake's body increases by 2.
Start Screen
Normal Mode of Gameplay
Arcade Mode of Gameplay

Testing & Technical Challenges

LED Matrix
  • Non-availability of datasheet for ADAFRUIT 32x32 RGB LED Matrix was a major challenge. This made the understanding of pin configuration details difficult. Subsequently, we extracted ADAFRUIT GFX library and created an interface class to make it compatible with our project requirements.
  • It was hard to determine the refresh rate for LED Matrix. Low refresh rate would cause display to flicker and a high refresh rate would slow down the display update rate thereby causing overhead. Through trial and error, we came up with a value of 500us for RIT interrupt which was an ideal refresh rate value.
  • In order to creatively enhance the game visual at start screen, we decided to display an image of a snake. We generated a bitmap using an 8-bit snake image. We manually determined the appropriate pixel position of the bitmap and provided the pixel colour. This was a tedious task as it had to done through trial and error.
Nordic Wireless Module and Joystick
  • Initially, there was a 2-3 second delay to reflect change of direction of the snake received at the output module from the input joystick module. It was hard to determine the cause of delay. After diligent analysis, we found that the task delay at producer and consumer tasks were different which has caused the delay. This was fixed by using the same task delay to achieve synchronization between input and change in snake direction.
Game Algorithm
  • It was hard to decide upon the approach to represent the snake's tail and display its real time motion on the screen. We finally came up with the logic of using an array where the first element of the array always indicated the snake's head and subsequent array elements indicated its body. Its real time movement was captured by pushing the elements of the array to the length of the snake's body.
  • It was great challenge to implement the pause feature in the game. We had to capture the real time movement of the snake when the user hit pause and resume the game from same position. The push button switch on joystick was used to pause and game would resume with input direction. Initially, when direction opposite to that of current snake's movement was given, it used to reset the game. This was overcome by capturing instant before pause and then resuming from the same instant when direction was input.
  • In order to display the player's score on game over, the integer value had to be converted to character and then displayed. Initially, while doing so, redundant values were also displayed along with the score. This was overcome by using ostringstream class which creates a buffer of type string and returns a string pointer. This was followed with the use of cstr function which outputs character to be printed on the console.
  • Starting the game with initial snake body length was challenging due to the designed algorithm. This was overcome by calculating the snake body before start of the game by hard-coding the default direction on game start.
PCB
  • Approach to design a PCB to meet the project requirements was complex. We had to redesign the circuit schematic and layout thrice to ensure that it was robust. Moreover, We had to import libraries from Sparkfun and adafruit for various components and also make sure that the same were available with Digikey.

<Bug/issue name>

  • The LED Matrix display was intermittently flickering and we couldn't narrow down the reason behind it. Nothing was changed in the code, so we suspect the cause to be unstable circuit connections between output module and PCB.
  • The top-leftmost LED blinks whenever the snake eats the fruit in the game. This was bug which couldn't be resolved.
  • The PCB was designed presuming all 34 pins on SJone board could be interchangeably as GPIO for the LED Matrix. Post fabrication with some of the connections hard-grounded for LED Matrix, it couldn't be used interchangeably. As a result we had to manually connect the wires from PCB to LED Matrix.

Conclusion

The project was a success both in terms of output and learnings. We were able to design Snake game which has been controlled wirelessly using handheld joystick. It provided an exposure of developing drivers for 32x32 RGB LED Matrix, Nordic Wireless, Joystick, active buzzer and accelerometer. It helped us utilize the knowledge of FreeRTOS and GPIO driver gained through the CMPE-244 class.

Working as a team and having brainstorming sessions helped us in overcoming a lot bugs which we encountered during game design. Furthermore, it helped in expediting the integration, testing and debugging of the project days before final demonstration. On the whole, we had a wonderful experience of working in a team and developing a complete product using professional tools and techniques.

Project Video

Project Source Code

References

Acknowledgement

We would like to sincerely thank Professor Preetpal Kang for his allround guidance, feedback and support. His classroom lectures were significant in imparting knowledge on embedded systems. Further, we would like to thank ISA team for their advice.

References Used

[1] Adafruit 32x32 RGB Matrix
[2] 2-Axis Joystick Tutorial
[3] Nordic Wireless Datasheet
[4] Low Powered Mesh Network Stack

Appendix