Kyle Yancey / Mbed 2 deprecated SNES_Controller

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 DigitalOut Data(D2);
00005 DigitalIn Latch(D3);
00006 DigitalIn Clock(D4);
00007 
00008 void waitUntilHigh(DigitalIn x)
00009 {
00010     while(x != 1) {};
00011 }
00012 
00013 void waitUntilLow(DigitalIn x)
00014 {
00015     while(x == 1) {};
00016 }
00017 
00018 void sendButton(int button)
00019 {
00020     Data = button;
00021     waitUntilLow(Clock);
00022     waitUntilHigh(Clock);
00023 }
00024 
00025 // The SNES expects unpressed buttons to have a high state.
00026 // 1 = unpressed, 0 = pressed
00027 void sendButtonsPressed(int b, int y, int select, int start,
00028                         int up, int down, int left, int right,
00029                         int a, int x, int l1, int r1)
00030 {
00031     Data = 1; // Seems sensible to start with data being high
00032     waitUntilHigh(Latch);
00033     waitUntilLow(Latch);
00034 
00035     sendButton(b);
00036     sendButton(y);
00037     sendButton(select);
00038     sendButton(start);
00039     sendButton(up);
00040     sendButton(down);
00041     sendButton(left);
00042     sendButton(right);
00043     sendButton(a);
00044     sendButton(x);
00045     sendButton(l1);
00046     sendButton(r1);
00047 }
00048 
00049 int main()
00050 {
00051     while(1) {
00052         // Repeatedly press A
00053         sendButtonsPressed(1, 1, 1, 1,      //b,y,select,start
00054                            1, 1, 1, 1,     //up,down,left,right
00055                            0, 1, 1, 1);    //a,x,l1,r1
00056 
00057         sendButtonsPressed(1, 1, 1, 1,      //b,y,select,start
00058                            1, 1, 1, 1,     //up,down,left,right
00059                            1, 1, 1, 1);    //a,x,l1,r1
00060     }
00061 }