Advertisement
BeamNG_IRC

Untitled

Nov 21st, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. #include <ps2.h>
  2. #include <LiquidCrystal.h>
  3. // OLED stuff
  4. #include <SPI.h>
  5. #include <Wire.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8. #define OLED_RESET 4
  9. Adafruit_SSD1306 display(OLED_RESET);
  10.  
  11. // initialize the library with the numbers of the interface pins
  12. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  13. int number;
  14. int max = 15;
  15. int score1 = 0;
  16. const int buttonPin_left = 13;
  17. const int buttonPin_up = 8;
  18. const int buttonPin_right = 9;
  19. int buttonState_up = 0;
  20. int buttonState_left = 0;
  21. int buttonState_right = 0;
  22.  
  23. /*
  24. * an arduino sketch to interface with a ps/2 mouse.
  25. * Also uses serial protocol to talk back to the host
  26. * and report what it finds.
  27. */
  28.  
  29. /*
  30. * Pin 7 is the mouse data pin, pin 6 is the clock pin
  31. * Feel free to use whatever pins are convenient.
  32. */
  33. PS2 mouse(6, 7);
  34.  
  35. /*
  36. * initialize the mouse. Reset it, and place it into remote
  37. * mode, so we can get the encoder data on demand.
  38. */
  39. void mouse_init() {
  40.     mouse.write(0xff);  // reset
  41.     mouse.read();  // ack byte
  42.     mouse.read();  // blank */
  43.     mouse.read();  // blank */
  44.     mouse.write(0xf0);  // remote mode
  45.     mouse.read();  // ack
  46.     delayMicroseconds(100);
  47. }
  48.  
  49. void oled_write(int line, int pos, int size, String text) {
  50.     display.clearDisplay(); // clear oled display
  51.     display.setTextSize(size);
  52.     display.setTextColor(WHITE);
  53.     display.setCursor(line, pos);
  54.     display.println(text);
  55.     display.display();
  56. }
  57.  
  58. // vars for our scene
  59. int plank1_pos = 20;
  60. int plank1_line = 0;
  61. int plank_buttom_pos = 40;
  62. int plank_buttom_line = 0;
  63. String plank1 = "___";
  64. String plank_buttom = "_____________________";
  65. void render() { // renders our scene
  66.     display.setCursor(plank1_line, plank1_pos);
  67.     display.println(plank1);
  68.     display.setCursor(plank_buttom_line, plank_buttom_pos);
  69.     display.println(plank_buttom);
  70.     display.setTextSize(1);
  71.     display.setTextColor(WHITE);
  72.     display.setCursor(0, 55);
  73.     display.println("Made by Dan Jones");
  74.     display.setCursor(100, 1);
  75.     display.println(score1);
  76.     display.display();
  77. }
  78.  
  79. // vars for character below
  80. int char_line = 0;
  81. int char_pos = 10;
  82. int length = 7;
  83. String chara = "*";
  84. void character(String direction) { // move a character around
  85.     score(1);
  86.     display.setTextSize(1);
  87.     display.setTextColor(WHITE);
  88.     display.clearDisplay(); // clear oled display
  89.     // now lets draw our new position
  90.     if (direction == "up") {
  91.         char_line = char_line - length;
  92.         display.setCursor(char_pos, char_line);
  93.         display.println(chara);
  94.         render(); // re-render our scene
  95.     }
  96.     if (direction == "left") {
  97.         char_pos = char_pos - length;
  98.         display.setCursor(char_pos, char_line);
  99.         display.println(chara);
  100.         render(); // re-render our scene
  101.     }
  102.     if (direction == "right") {
  103.         char_pos = char_pos + length;
  104.         display.setCursor(char_pos, char_line);
  105.         display.println(chara);
  106.         render(); // re-render our scene
  107.     }
  108.     render(); // re-render our scene
  109. }
  110. void score(int value) { // simple score for HUD
  111.     score1 = score1 + value;
  112.     render(); // re-render our scene
  113. }
  114.  
  115. // vars for gravity
  116. int speed = 5;
  117. void gravity(int jump) { // lets try add gravity to our script....
  118.     // is the user jumping?
  119.     if (jump == 1) { // yes, lets jump
  120.         display.clearDisplay();
  121.         char_line = char_line - speed * 2;
  122.         display.setCursor(char_pos, char_line);
  123.         display.println(chara);
  124.         render();
  125.     }
  126.     // collision detection is hard
  127.     if (!(char_line >= plank1_pos)) {
  128.         display.clearDisplay();
  129.         char_line = char_line + speed;
  130.         display.setCursor(char_pos, char_line);
  131.         display.println(chara);
  132.         render();
  133.     }
  134.     if (!(char_line >= plank_buttom_pos) && (char_pos >= plank1_pos)) {
  135.         display.clearDisplay();
  136.         char_line = char_line + speed;
  137.         display.setCursor(char_pos, char_line);
  138.         display.println(chara);
  139.         render();
  140.     }
  141.     // lets check if the character is out of bounds
  142.     if (char_line > plank_buttom_pos) { // reset
  143.         char_line = 5;
  144.         char_pos = 10;
  145.         display.setCursor(char_pos, char_line);
  146.         display.println(chara);
  147.         render();
  148.     }  
  149. }
  150.  
  151. void setup() {
  152.     render(); // setup our scene
  153.     lcd.begin(16, 2);
  154.     Serial.begin(9600);
  155.     mouse_init();
  156.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // OLED display
  157.     //oled_write(1, 0, 1, "Arduino TrackPad");
  158. }
  159.  
  160. /*
  161. * get a reading from the mouse and report it back to the
  162. * host via the serial line.
  163. */
  164. void loop() {
  165.     buttonState_up = digitalRead(buttonPin_up);
  166.     buttonState_left = digitalRead(buttonPin_left);
  167.     buttonState_right = digitalRead(buttonPin_right);
  168.     char mstat;
  169.     char mx;
  170.     char my;
  171.     /* get a reading from the mouse */
  172.     mouse.write(0xeb);  // give me data!
  173.     mouse.read();      // ignore ack
  174.     mstat = mouse.read();
  175.     mx = mouse.read();
  176.     my = mouse.read();
  177.     /* send the data back up */
  178.     lcd.setCursor(0, 0);
  179.     lcd.print(" X = ");
  180.     lcd.print(mx, DEC);
  181.     lcd.print("         ");
  182.     lcd.setCursor(0, 1);
  183.     lcd.print(" Y = ");
  184.     lcd.print(my, DEC);
  185.     lcd.print("         ");
  186.     if (my > max) {
  187.         my = max;
  188.     }
  189.     if (mx > max) {
  190.         mx = max;
  191.     }
  192.     while (my > 10) {
  193.         number = number + 1;
  194.         //Serial.print(number);
  195.         //Serial.print("\n");
  196.         lcd.setCursor(10, 0);
  197.         //lcd.print("up");
  198.         //oled_write(4, 0, 2, "Up");
  199.         Serial.print("1");
  200.         Serial.print("\n");
  201.         //character("up");
  202.         gravity(1);
  203.         my = 0;
  204.         //my = my - 1;
  205.     }  
  206.     while (my < -10) {
  207.         number = number - 1;
  208.         //Serial.print(number);
  209.         //Serial.print("\n");
  210.         lcd.setCursor(10, 0);
  211.         //lcd.print("down");
  212.         //oled_write(4, 0, 2, "Down");
  213.         Serial.print("2");
  214.         Serial.print("\n");
  215.         //character("down");
  216.         my = 0;
  217.         //my = my + 1;
  218.     }
  219.     while (mx < -10) {
  220.         lcd.setCursor(10, 1);
  221.         //lcd.print("left");
  222.         //oled_write(4, 0, 2, "Left");
  223.         Serial.print("4");
  224.         Serial.print("\n");
  225.         character("left");
  226.         mx = 0;
  227.     }
  228.     while (mx > 10) {
  229.         lcd.setCursor(10, 1);
  230.         //lcd.print("right");
  231.         //oled_write(4, 0, 2, "Right");
  232.         Serial.print("3");
  233.         Serial.print("\n");
  234.         character("right");
  235.         mx = 0;
  236.     }
  237.  
  238.     // the touch pad controls suck, lets add buttons
  239.     if (buttonState_up == HIGH) {        
  240.         gravity(1);
  241.     }
  242.     if (buttonState_left == HIGH) {        
  243.         character("left");
  244.     }
  245.     if (buttonState_right == HIGH) {        
  246.         character("right");
  247.     }
  248.    
  249.    
  250.    
  251.     gravity(0); // run our gravity script
  252.     //delay(500);
  253.    
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement