Permalink
Browse files

Support both the tinyscreen video game kit and the tinyarcade.

  • Loading branch information...
dulsi committed Jan 28, 2018
1 parent ba81d3c commit 14dcbe3857f2bc017d09ffc841b83e3fb2fa072d
Showing with 256 additions and 66 deletions.
  1. +1 −0 Sprite.cpp
  2. +0 −1 Sprite.h
  3. +99 −0 TinyArcade.h
  4. +24 −0 TinyConfig.h
  5. +109 −0 TinyGameKit.h
  6. +23 −65 viobyte.ino
View
@@ -1,6 +1,7 @@
#include <TinyScreen.h>
#include <SPI.h>
#include <Wire.h>
#include "TinyConfig.h"
#include "Sprite.h"
View
@@ -1,5 +1,4 @@
#pragma once
#include <avr/pgmspace.h>
#define ALPHA 0xBA
View
@@ -0,0 +1,99 @@
/*
TinyArcade.h - Last modified 6 September 2016
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Written by Ben Rose for TinyCircuits.
The latest version of this library can be found at https://tinycircuits.com/
*/
#ifndef TINYARCADE_H
#define TINYARCADE_H
#define TinyArcadePin1 45 //Left Button
#define TinyArcadePin2 44 //Right Button
#define TinyArcadePinX 42 //Analog Joystick x-axis
#define TinyArcadePinY 1 //Analog Joystick y-axis
#define TinyArcadePinUp 42 //Joystick UP
#define TinyArcadePinDown 19 //Joystick DOWN
#define TinyArcadePinLeft 25 //Joystick LEFT
#define TinyArcadePinRight 15 //Joystick RIGHT
const uint8_t TAJoystickUp = 1 << 0; //Mask
const uint8_t TAJoystickDown = 1 << 1; //Mask
const uint8_t TAJoystickLeft = 1 << 2; //Mask
const uint8_t TAJoystickRight = 1 << 3; //Mask
const uint8_t TAButton1 = 1 << 0; //Mask
const uint8_t TAButton2 = 1 << 1; //Mask
uint8_t arcadeVersion = 1;
void arcadeInit() {
pinMode(4, INPUT_PULLUP);
if (!digitalRead(4)) {
arcadeVersion = 2;
}
analogWrite(A0, analogRead(A0));//Set audio output to avoid picking up noise, even though audio isn't used
pinMode(TinyArcadePin2, INPUT_PULLUP);
pinMode(TinyArcadePin1, INPUT_PULLUP);
if (arcadeVersion == 2) {
pinMode(TinyArcadePinUp, INPUT_PULLUP);
pinMode(TinyArcadePinDown, INPUT_PULLUP);
pinMode(TinyArcadePinLeft, INPUT_PULLUP);
pinMode(TinyArcadePinRight, INPUT_PULLUP);
}
}
uint8_t checkButton(uint8_t btn) {
uint8_t rVal = 0;
if ((btn & TAButton1) && !digitalRead(TinyArcadePin1))
rVal |= TAButton1;
if ((btn & TAButton2) && !digitalRead(TinyArcadePin2))
rVal |= TAButton2;
return rVal;
}
uint8_t checkJoystick(uint8_t joystickDir) {
if (arcadeVersion == 1) {
int analogVal = analogRead(TinyArcadePinY);
uint8_t rVal = 0;
if ((joystickDir & TAJoystickUp) && analogVal < 512 - 250)
rVal |= TAJoystickUp;
else if ((joystickDir & TAJoystickDown) && analogVal > 512 + 250)
rVal |= TAJoystickDown;
analogVal = analogRead(TinyArcadePinX);
if ((joystickDir & TAJoystickLeft) && analogVal > 512 + 250)
rVal |= TAJoystickLeft;
else if ((joystickDir & TAJoystickRight) && analogVal < 512 - 250)
rVal |= TAJoystickRight;
return rVal;
} else if (arcadeVersion == 2) {
uint8_t rVal = 0;
if ((joystickDir & TAJoystickUp) && !digitalRead(TinyArcadePinUp))
rVal |= TAJoystickUp;
if ((joystickDir & TAJoystickDown) && !digitalRead(TinyArcadePinDown))
rVal |= TAJoystickDown;
if ((joystickDir & TAJoystickLeft) && !digitalRead(TinyArcadePinLeft))
rVal |= TAJoystickLeft;
if ((joystickDir & TAJoystickRight) && !digitalRead(TinyArcadePinRight))
rVal |= TAJoystickRight;
return rVal;
}
}
#endif
View
@@ -0,0 +1,24 @@
#ifndef TINYCONFIG_H
#define TINYCONFIG_H
//#define TINYARCADE_CONFIG
#define TINYSCREEN_GAMEKIT_CONFIG
#ifdef TINYARCADE_CONFIG
#define pgm_read_ptr(x) (*(x))
const uint8_t PROGMEM TAJoystick2Up = 1 << 4; //Mask
const uint8_t PROGMEM TAJoystick2Down = 1 << 5; //Mask
const uint8_t PROGMEM TAJoystick2Left = 1 << 6; //Mask
const uint8_t PROGMEM TAJoystick2Right = 1 << 7; //Mask
#define TINYSCREEN_TYPE TinyScreenPlus
#endif
#ifdef TINYSCREEN_GAMEKIT_CONFIG
#include <avr/pgmspace.h>
#define TINYSCREEN_TYPE 0
#define SerialUSB Serial
#endif
#endif
View
@@ -0,0 +1,109 @@
#ifndef TINYGAMEKIT_H
#define TINYGAMEKIT_H
const uint8_t PROGMEM TAJoystickUp = 1 << 0; //Mask
const uint8_t PROGMEM TAJoystickDown = 1 << 1; //Mask
const uint8_t PROGMEM TAJoystickLeft = 1 << 2; //Mask
const uint8_t PROGMEM TAJoystickRight = 1 << 3; //Mask
const uint8_t PROGMEM TAJoystick2Up = 1 << 4; //Mask
const uint8_t PROGMEM TAJoystick2Down = 1 << 5; //Mask
const uint8_t PROGMEM TAJoystick2Left = 1 << 6; //Mask
const uint8_t PROGMEM TAJoystick2Right = 1 << 7; //Mask
const uint8_t TAButton1 = 1 << 0; //Mask
const uint8_t TAButton2 = 1 << 1; //Mask
#define GAMEKIT_HASJOYSTICK 1
#define GAMEKIT_HASBUTTONS 2
byte gamekit_read = 0;
int gamekit_data[4];
byte gamekit_buttons;
void arcadeInit()
{
Wire.begin();
}
void readJoystickButtons()
{
Wire.requestFrom(0x22,6);
for(int i=0;i<4;i++)
{
gamekit_data[i]=Wire.read();
}
byte lsb=Wire.read();
for(int i=0;i<4;i++)
{
gamekit_data[i]<<=2;
gamekit_data[i]|= ((lsb>>(i*2))&3);
gamekit_data[i]-=511;
}
gamekit_buttons=~Wire.read();
gamekit_read = GAMEKIT_HASJOYSTICK | GAMEKIT_HASBUTTONS;
}
uint8_t checkButton(uint8_t btn)
{
if (0 == (gamekit_read & GAMEKIT_HASBUTTONS))
{
readJoystickButtons();
gamekit_read = gamekit_read & (~GAMEKIT_HASBUTTONS);
}
uint8_t answer = 0;
if ((btn & TAButton1) && (gamekit_buttons & 4))
answer |= TAButton1;
if ((btn & TAButton2) && (gamekit_buttons & 8))
answer |= TAButton2;
return answer;
}
uint8_t checkJoystick(uint8_t joystickDir)
{
if (0 == (gamekit_read & GAMEKIT_HASJOYSTICK))
{
readJoystickButtons();
gamekit_read = gamekit_read & (~GAMEKIT_HASJOYSTICK);
}
uint8_t answer = 0;
int RX=gamekit_data[0];
int RY=gamekit_data[1];
int LX=gamekit_data[2];
int LY=gamekit_data[3];
if ((joystickDir & TAJoystickLeft) && (LX > 100))
{
answer |= TAJoystickLeft;
}
else if ((joystickDir & TAJoystickRight) && (LX < -100))
{
answer |= TAJoystickRight;
}
if ((joystickDir & TAJoystickUp) && (LY > 100))
{
answer |= TAJoystickUp;
}
else if ((joystickDir & TAJoystickDown) && (LY < -100))
{
answer |= TAJoystickDown;
}
if ((joystickDir & TAJoystick2Left) && (RX > 100))
{
answer |= TAJoystick2Left;
}
else if ((joystickDir & TAJoystick2Right) && (RX < -100))
{
answer |= TAJoystick2Right;
}
if ((joystickDir & TAJoystick2Up) && (RY > 100))
{
answer |= TAJoystick2Up;
}
else if ((joystickDir & TAJoystick2Down) && (RY < -100))
{
answer |= TAJoystick2Down;
}
return answer;
}
#endif
View
@@ -1,9 +1,17 @@
#include <TinyScreen.h>
#include <SPI.h>
#include <Wire.h>
#include "TinyConfig.h"
#include "Sprite.h"
#include "viobytetitle.h"
#ifdef TINYARCADE_CONFIG
#include "TinyArcade.h"
#endif
#ifdef TINYSCREEN_GAMEKIT_CONFIG
#include "TinyGameKit.h"
#endif
#define BLACK 0x00
#define BLUE 0xE0
#define YELLO 0x1F
@@ -19,7 +27,7 @@
#define STATE_DIEING 2
#define STATE_GAMEOVER 3
TinyScreen display = TinyScreen(0);
TinyScreen display = TinyScreen(TINYSCREEN_TYPE);
const uint8_t PROGMEM s_BlueWall1Data[] {
BLUE, BLUE, BLUE, BLUE, BLUE,
@@ -422,15 +430,6 @@ static Sprite s_GameOver = {87,10, _image_game_over_data};
SpriteInst sprites[5];
const uint8_t PROGMEM TAJoystickUp = 1 << 0; //Mask
const uint8_t PROGMEM TAJoystickDown = 1 << 1; //Mask
const uint8_t PROGMEM TAJoystickLeft = 1 << 2; //Mask
const uint8_t PROGMEM TAJoystickRight = 1 << 3; //Mask
const uint8_t PROGMEM TAJoystick2Up = 1 << 4; //Mask
const uint8_t PROGMEM TAJoystick2Down = 1 << 5; //Mask
const uint8_t PROGMEM TAJoystick2Left = 1 << 6; //Mask
const uint8_t PROGMEM TAJoystick2Right = 1 << 7; //Mask
int score = 0;
int dotsLeft = 0;
int lives = 1;
@@ -494,69 +493,28 @@ void reset()
void setup()
{
Wire.begin();
Serial.begin(9600);
arcadeInit();
display.begin();
display.setBitDepth(TSBitDepth8);
display.setBrightness(15);
display.setFlip(false);
#ifdef TINYARCADE_CONFIG
USBDevice.init();
USBDevice.attach();
#endif
SerialUSB.begin(9600);
reset();
lastTime = millis();
}
void updateJoystick(){
Wire.requestFrom(0x22,6);
int data[4];
for(int i=0;i<4;i++){
data[i]=Wire.read();
}
byte lsb=Wire.read();
byte buttons=~Wire.read();
uint8_t btn = checkButton(TAButton1 | TAButton2);
joyDir = checkJoystick(TAJoystickUp | TAJoystickDown | TAJoystickLeft | TAJoystickRight);
leftButtonPrev = leftButton;
rightButtonPrev = rightButton;
leftButton=buttons&4;
rightButton=buttons&8;
for(int i=0;i<4;i++){
data[i]<<=2;
data[i]|= ((lsb>>(i*2))&3);
data[i]-=511;
}
int RX=data[0];
int RY=data[1];
int LX=data[2];
int LY=data[3];
joyDir = 0;
if (LX > 100)
{
joyDir |= TAJoystickLeft;
}
else if (LX < -100)
{
joyDir |= TAJoystickRight;
}
if (LY > 100)
{
joyDir |= TAJoystickUp;
}
else if (LY < -100)
{
joyDir |= TAJoystickDown;
}
if (RX > 100)
{
joyDir |= TAJoystick2Left;
}
else if (RX < -100)
{
joyDir |= TAJoystick2Right;
}
if (RY > 100)
{
joyDir |= TAJoystick2Up;
}
else if (RY < -100)
{
joyDir |= TAJoystick2Down;
}
leftButton=btn & TAButton1;
rightButton=btn & TAButton2;
randomSeed(analogRead(0));
}

0 comments on commit 14dcbe3

Please sign in to comment.