mirror of https://github.com/MLXXXp/Arduboy2.git
Migrate example sketches
Renamed Tunes to PlayTune and enhanced it to better demonstrate the ArduboyPlaytune library's capabilities.
This commit is contained in:
parent
6c5b2cd783
commit
3b96b7ba24
|
@ -9,13 +9,10 @@
|
|||
version 2.1 of the License, or (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <Arduboy.h>
|
||||
#include <ArduboyPlaytune.h>
|
||||
#include <Arduboy2.h>
|
||||
#include "breakout_bitmaps.h"
|
||||
|
||||
Arduboy arduboy;
|
||||
ArduboyPlaytune tunes;
|
||||
AbPrinter text(arduboy);
|
||||
Arduboy2 arduboy;
|
||||
|
||||
const unsigned int COLUMNS = 13; //Columns of bricks
|
||||
const unsigned int ROWS = 4; //Rows of bricks
|
||||
|
@ -56,10 +53,7 @@ byte tick;
|
|||
void setup()
|
||||
{
|
||||
arduboy.begin();
|
||||
arduboy.setFrameRate(60);
|
||||
|
||||
tunes.initChannel(PIN_SPEAKER_1);
|
||||
tunes.initChannel(PIN_SPEAKER_2);
|
||||
arduboy.setFrameRate(25);
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -179,7 +173,7 @@ void moveBall()
|
|||
{
|
||||
yb = 2;
|
||||
dy = -dy;
|
||||
tunes.tone(523, 250);
|
||||
playTone(523, 250);
|
||||
}
|
||||
|
||||
//Lose a life if bottom edge hit
|
||||
|
@ -191,7 +185,7 @@ void moveBall()
|
|||
released = false;
|
||||
lives--;
|
||||
drawLives();
|
||||
tunes.tone(175, 250);
|
||||
playTone(175, 250);
|
||||
if (random(0, 2) == 0)
|
||||
{
|
||||
dx = 1;
|
||||
|
@ -207,7 +201,7 @@ void moveBall()
|
|||
{
|
||||
xb = 2;
|
||||
dx = -dx;
|
||||
tunes.tone(523, 250);
|
||||
playTone(523, 250);
|
||||
}
|
||||
|
||||
//Bounce off right side
|
||||
|
@ -215,7 +209,7 @@ void moveBall()
|
|||
{
|
||||
xb = WIDTH - 4;
|
||||
dx = -dx;
|
||||
tunes.tone(523, 250);
|
||||
playTone(523, 250);
|
||||
}
|
||||
|
||||
//Bounce off paddle
|
||||
|
@ -227,7 +221,7 @@ void moveBall()
|
|||
if (dx == 0) {
|
||||
dx = (random(0,2) == 1) ? 1 : -1;
|
||||
}
|
||||
tunes.tone(200, 250);
|
||||
playTone(200, 250);
|
||||
}
|
||||
|
||||
//Bounce off Bricks
|
||||
|
@ -261,7 +255,7 @@ void moveBall()
|
|||
dy =- dy;
|
||||
yb += dy;
|
||||
bounced = true;
|
||||
tunes.tone(261, 250);
|
||||
playTone(261, 250);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,7 +268,7 @@ void moveBall()
|
|||
dx =- dx;
|
||||
xb += dx;
|
||||
bounced = true;
|
||||
tunes.tone(261, 250);
|
||||
playTone(261, 250);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -313,9 +307,9 @@ void moveBall()
|
|||
|
||||
void drawBall()
|
||||
{
|
||||
// text.setCursor(0,0);
|
||||
// text.print(arduboy.cpuLoad());
|
||||
// text.print(" ");
|
||||
// arduboy.setCursor(0,0);
|
||||
// arduboy.print(arduboy.cpuLoad());
|
||||
// arduboy.print(" ");
|
||||
arduboy.drawPixel(xb, yb, 0);
|
||||
arduboy.drawPixel(xb+1, yb, 0);
|
||||
arduboy.drawPixel(xb, yb+1, 0);
|
||||
|
@ -339,8 +333,8 @@ void drawPaddle()
|
|||
void drawLives()
|
||||
{
|
||||
sprintf(text_buffer, "LIVES:%u", lives);
|
||||
text.setCursor(0, 90);
|
||||
text.print(text_buffer);
|
||||
arduboy.setCursor(0, 90);
|
||||
arduboy.print(text_buffer);
|
||||
}
|
||||
|
||||
void drawGameOver()
|
||||
|
@ -349,10 +343,10 @@ void drawGameOver()
|
|||
arduboy.drawPixel(xb+1, yb, 0);
|
||||
arduboy.drawPixel(xb, yb+1, 0);
|
||||
arduboy.drawPixel(xb+1, yb+1, 0);
|
||||
text.setCursor(52, 42);
|
||||
text.print( "Game");
|
||||
text.setCursor(52, 54);
|
||||
text.print("Over");
|
||||
arduboy.setCursor(52, 42);
|
||||
arduboy.print( "Game");
|
||||
arduboy.setCursor(52, 54);
|
||||
arduboy.print("Over");
|
||||
arduboy.display();
|
||||
delay(4000);
|
||||
}
|
||||
|
@ -361,8 +355,8 @@ void pause()
|
|||
{
|
||||
paused = true;
|
||||
//Draw pause to the screen
|
||||
text.setCursor(52, 45);
|
||||
text.print("PAUSE");
|
||||
arduboy.setCursor(52, 45);
|
||||
arduboy.print("PAUSE");
|
||||
arduboy.display();
|
||||
while (paused)
|
||||
{
|
||||
|
@ -383,8 +377,8 @@ void Score()
|
|||
{
|
||||
score += (level*10);
|
||||
sprintf(text_buffer, "SCORE:%u", score);
|
||||
text.setCursor(80, 90);
|
||||
text.print(text_buffer);
|
||||
arduboy.setCursor(80, 90);
|
||||
arduboy.print(text_buffer);
|
||||
}
|
||||
|
||||
void newLevel(){
|
||||
|
@ -417,8 +411,8 @@ void newLevel(){
|
|||
|
||||
//Draws the initial score
|
||||
sprintf(text_buffer, "SCORE:%u", score);
|
||||
text.setCursor(80, 90);
|
||||
text.print(text_buffer);
|
||||
arduboy.setCursor(80, 90);
|
||||
arduboy.print(text_buffer);
|
||||
}
|
||||
|
||||
//Used to delay images while reading button input
|
||||
|
@ -448,15 +442,15 @@ boolean displayHighScores(byte file)
|
|||
int address = file*10*5;
|
||||
byte hi, lo;
|
||||
arduboy.clear();
|
||||
text.setCursor(32, 0);
|
||||
text.print("HIGH SCORES");
|
||||
arduboy.setCursor(32, 0);
|
||||
arduboy.print("HIGH SCORES");
|
||||
arduboy.display();
|
||||
|
||||
for(int i = 0; i < 10; i++)
|
||||
{
|
||||
sprintf(text_buffer, "%2d", i+1);
|
||||
text.setCursor(x,y+(i*8));
|
||||
text.print(text_buffer);
|
||||
arduboy.setCursor(x,y+(i*8));
|
||||
arduboy.print(text_buffer);
|
||||
arduboy.display();
|
||||
hi = EEPROM.read(address + (5*i));
|
||||
lo = EEPROM.read(address + (5*i) + 1);
|
||||
|
@ -477,8 +471,8 @@ boolean displayHighScores(byte file)
|
|||
if (score > 0)
|
||||
{
|
||||
sprintf(text_buffer, "%c%c%c %u", initials[0], initials[1], initials[2], score);
|
||||
text.setCursor(x + 24, y + (i*8));
|
||||
text.print(text_buffer);
|
||||
arduboy.setCursor(x + 24, y + (i*8));
|
||||
arduboy.print(text_buffer);
|
||||
arduboy.display();
|
||||
}
|
||||
}
|
||||
|
@ -494,10 +488,10 @@ boolean titleScreen()
|
|||
{
|
||||
//Clears the screen
|
||||
arduboy.clear();
|
||||
text.setCursor(16,22);
|
||||
text.setSize(2);
|
||||
text.print("ARAKNOID");
|
||||
text.setSize(1);
|
||||
arduboy.setCursor(16,22);
|
||||
arduboy.setTextSize(2);
|
||||
arduboy.print("ARAKNOID");
|
||||
arduboy.setTextSize(1);
|
||||
arduboy.display();
|
||||
if (pollFireButton(25))
|
||||
{
|
||||
|
@ -509,8 +503,8 @@ boolean titleScreen()
|
|||
{
|
||||
//Draws "Press FIRE"
|
||||
//arduboy.bitmap(31, 53, fire); arduboy.display();
|
||||
text.setCursor(31, 53);
|
||||
text.print("PRESS FIRE!");
|
||||
arduboy.setCursor(31, 53);
|
||||
arduboy.print("PRESS FIRE!");
|
||||
arduboy.display();
|
||||
|
||||
if (pollFireButton(50))
|
||||
|
@ -519,10 +513,10 @@ boolean titleScreen()
|
|||
}
|
||||
//Removes "Press FIRE"
|
||||
arduboy.clear();
|
||||
text.setCursor(16,22);
|
||||
text.setSize(2);
|
||||
text.print("ARAKNOID");
|
||||
text.setSize(1);
|
||||
arduboy.setCursor(16,22);
|
||||
arduboy.setTextSize(2);
|
||||
arduboy.print("ARAKNOID");
|
||||
arduboy.setTextSize(1);
|
||||
arduboy.display();
|
||||
|
||||
arduboy.display();
|
||||
|
@ -551,17 +545,17 @@ void enterInitials()
|
|||
arduboy.display();
|
||||
arduboy.clear();
|
||||
|
||||
text.setCursor(16,0);
|
||||
text.print("HIGH SCORE");
|
||||
arduboy.setCursor(16,0);
|
||||
arduboy.print("HIGH SCORE");
|
||||
sprintf(text_buffer, "%u", score);
|
||||
text.setCursor(88, 0);
|
||||
text.print(text_buffer);
|
||||
text.setCursor(56, 20);
|
||||
text.print(initials[0]);
|
||||
text.setCursor(64, 20);
|
||||
text.print(initials[1]);
|
||||
text.setCursor(72, 20);
|
||||
text.print(initials[2]);
|
||||
arduboy.setCursor(88, 0);
|
||||
arduboy.print(text_buffer);
|
||||
arduboy.setCursor(56, 20);
|
||||
arduboy.print(initials[0]);
|
||||
arduboy.setCursor(64, 20);
|
||||
arduboy.print(initials[1]);
|
||||
arduboy.setCursor(72, 20);
|
||||
arduboy.print(initials[2]);
|
||||
for(byte i = 0; i < 3; i++)
|
||||
{
|
||||
arduboy.drawLine(56 + (i*8), 27, 56 + (i*8) + 6, 27, 1);
|
||||
|
@ -578,7 +572,7 @@ void enterInitials()
|
|||
index = 0;
|
||||
} else
|
||||
{
|
||||
tunes.tone(1046, 250);
|
||||
playTone(1046, 250);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -589,14 +583,14 @@ void enterInitials()
|
|||
{
|
||||
index = 2;
|
||||
} else {
|
||||
tunes.tone(1046, 250);
|
||||
playTone(1046, 250);
|
||||
}
|
||||
}
|
||||
|
||||
if (arduboy.pressed(DOWN_BUTTON))
|
||||
{
|
||||
initials[index]++;
|
||||
tunes.tone(523, 250);
|
||||
playTone(523, 250);
|
||||
// A-Z 0-9 :-? !-/ ' '
|
||||
if (initials[index] == '0')
|
||||
{
|
||||
|
@ -619,7 +613,7 @@ void enterInitials()
|
|||
if (arduboy.pressed(UP_BUTTON))
|
||||
{
|
||||
initials[index]--;
|
||||
tunes.tone(523, 250);
|
||||
playTone(523, 250);
|
||||
if (initials[index] == ' ') {
|
||||
initials[index] = '?';
|
||||
}
|
||||
|
@ -639,9 +633,9 @@ void enterInitials()
|
|||
if (index < 2)
|
||||
{
|
||||
index++;
|
||||
tunes.tone(1046, 250);
|
||||
playTone(1046, 250);
|
||||
} else {
|
||||
tunes.tone(1046, 250);
|
||||
playTone(1046, 250);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -718,3 +712,12 @@ void enterHighScore(byte file)
|
|||
}
|
||||
}
|
||||
|
||||
// Wrap the Arduino tone() function so that the pin doesn't have to be
|
||||
// specified each time. Also, don't play if audio is set to off.
|
||||
void playTone(unsigned int frequency, unsigned long duration)
|
||||
{
|
||||
if (arduboy.audio.enabled() == true)
|
||||
{
|
||||
tone(PIN_SPEAKER_1, frequency, duration);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,10 @@ License as published by the Free Software Foundation; either
|
|||
version 2.1 of the License, or (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <Arduboy.h>
|
||||
#include <Arduboy2.h>
|
||||
|
||||
// Make an instance of arduboy used for many functions
|
||||
Arduboy arduboy;
|
||||
AbPrinter text(arduboy);
|
||||
Arduboy2 arduboy;
|
||||
|
||||
// Variables for your game go here.
|
||||
char title[] = "Press Buttons!";
|
||||
|
@ -97,10 +96,10 @@ void loop() {
|
|||
arduboy.clear();
|
||||
|
||||
// we set our cursor x pixels to the right and y down from the top
|
||||
text.setCursor(x, y);
|
||||
arduboy.setCursor(x, y);
|
||||
|
||||
// then we print to screen what is stored in our text variable we declared earlier
|
||||
text.print(title);
|
||||
// then we print to screen what is stored in our title variable we declared earlier
|
||||
arduboy.print(title);
|
||||
|
||||
// then we finaly we tell the arduboy to display what we just wrote to the display.
|
||||
arduboy.display();
|
||||
|
|
|
@ -11,11 +11,10 @@ License as published by the Free Software Foundation; either
|
|||
version 2.1 of the License, or (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <Arduboy.h>
|
||||
#include <Arduboy2.h>
|
||||
|
||||
// make an instance of arduboy used for many functions
|
||||
Arduboy arduboy;
|
||||
AbPrinter text(arduboy);
|
||||
Arduboy2 arduboy;
|
||||
|
||||
|
||||
// This function runs once in your game.
|
||||
|
@ -42,10 +41,10 @@ void loop() {
|
|||
|
||||
// we set our cursor 5 pixels to the right and 10 down from the top
|
||||
// (positions start at 0, 0)
|
||||
text.setCursor(4, 9);
|
||||
arduboy.setCursor(4, 9);
|
||||
|
||||
// then we print to screen what is in the Quotation marks ""
|
||||
text.print(F("Hello, world!"));
|
||||
arduboy.print(F("Hello, world!"));
|
||||
|
||||
// then we finaly we tell the arduboy to display what we just wrote to the display
|
||||
arduboy.display();
|
||||
|
|
|
@ -1,4 +1,25 @@
|
|||
#include <Arduboy.h>
|
||||
/***************************************************************
|
||||
Play a musical composition in the background while
|
||||
the main sketch code runs in the foreground.
|
||||
|
||||
The ArduboyPlaytune library must be installed to use this sketch
|
||||
https://github.com/Arduboy/ArduboyPlayTune
|
||||
|
||||
The D-Pad buttons will move the text and play a tone.
|
||||
|
||||
The A button mutes the sound.
|
||||
The screen is inverted when sound is muted.
|
||||
|
||||
The B button will turn sound back on if it's muted.
|
||||
|
||||
Note: The score that is played contains three parts.
|
||||
The ArduboyPlaytune library can only play the first two
|
||||
parts and ignores the third, so it sounds a bit strange
|
||||
in some places. With the DevKit, only the first part plays.
|
||||
|
||||
***************************************************************/
|
||||
|
||||
#include <Arduboy2.h>
|
||||
#include <ArduboyPlaytune.h>
|
||||
|
||||
const byte PROGMEM score [] = {
|
||||
|
@ -155,29 +176,34 @@ const byte PROGMEM score [] = {
|
|||
0x91,0x47, 0,53, 0,141, 0x80, 0x81, 0x82, 0x90,0x47, 0x91,0x44, 0x92,0x28, 0,95, 1,77, 0,202,
|
||||
0x80, 0x81, 0x90,0x45, 0,91, 0,136, 0x80, 0x82, 0x90,0x45, 0x91,0x2D, 7,83, 0x80, 0x81, 0xf0};
|
||||
|
||||
Arduboy arduboy;
|
||||
AbPrinter text(arduboy);
|
||||
ArduboyPlaytune tunes;
|
||||
Arduboy2 arduboy;
|
||||
ArduboyPlaytune tunes(arduboy.audio.enabled);
|
||||
|
||||
void setup()
|
||||
{
|
||||
arduboy.begin();
|
||||
text.setSize(4);
|
||||
text.setCursor(0,0);
|
||||
text.print("Music\nDemo");
|
||||
|
||||
arduboy.setFrameRate(10);
|
||||
arduboy.setTextSize(3);
|
||||
|
||||
// audio setup
|
||||
arduboy.audio.on();
|
||||
tunes.initChannel(PIN_SPEAKER_1);
|
||||
#ifndef AB_DEVKIT
|
||||
// if not a DevKit
|
||||
tunes.initChannel(PIN_SPEAKER_2);
|
||||
#else
|
||||
// if it's a Devkit
|
||||
tunes.initChannel(PIN_SPEAKER_1); // use the same pin for both channels
|
||||
tunes.toneMutesScore(true); // mute the score when a tone is sounding
|
||||
#endif
|
||||
|
||||
arduboy.display();
|
||||
arduboy.invert(!arduboy.audio.enabled()); // invert display if sound muted
|
||||
}
|
||||
|
||||
|
||||
int x = 0, y = 0;
|
||||
int x = 20, y = 10; // initial text position
|
||||
|
||||
void loop ()
|
||||
void loop()
|
||||
{
|
||||
// pause render until it's time for the next frame
|
||||
if (!(arduboy.nextFrame()))
|
||||
|
@ -185,23 +211,31 @@ void loop ()
|
|||
|
||||
if (arduboy.pressed(UP_BUTTON)) {
|
||||
y-=1;
|
||||
tunes.tone(1175,300);
|
||||
} else if (arduboy.pressed(DOWN_BUTTON)) {
|
||||
y+=1;
|
||||
tunes.tone(1397,300);
|
||||
} else if (arduboy.pressed(LEFT_BUTTON)) {
|
||||
x-=1;
|
||||
tunes.tone(1047,300);
|
||||
} else if (arduboy.pressed(RIGHT_BUTTON)) {
|
||||
x+=1;
|
||||
tunes.tone(1319,300);
|
||||
}
|
||||
|
||||
if (arduboy.pressed(A_BUTTON)) {
|
||||
arduboy.invert(true);
|
||||
arduboy.audio.off();
|
||||
} else if (arduboy.pressed(B_BUTTON)) {
|
||||
arduboy.invert(false);
|
||||
arduboy.audio.on();
|
||||
}
|
||||
|
||||
arduboy.clear();
|
||||
text.setCursor(x,y);
|
||||
text.print("Music\nDemo");
|
||||
arduboy.setCursor(x,y);
|
||||
arduboy.print("Music");
|
||||
arduboy.setCursor(x+8,y+24);
|
||||
arduboy.print("Demo");
|
||||
arduboy.display();
|
||||
|
||||
// play the tune if we aren't already
|
|
@ -0,0 +1,18 @@
|
|||
# PlayTune
|
||||
|
||||
Play a musical composition using the Arduboy.
|
||||
|
||||
Demonstrates playing music in the background while the "real" sketch code runs in the foreground.
|
||||
|
||||
The ArduboyPlaytune library must be installed to use this sketch
|
||||
|
||||
https://github.com/Arduboy/ArduboyPlayTune
|
||||
|
||||
A small composition is stored by `byte PROGMEM score`. The score is started in the sketch loop using `playScore(score)`.
|
||||
|
||||
D-Pad buttons will move the text and play a tone.
|
||||
|
||||
The A button mutes the sound. The screen is inverted when sound is muted.
|
||||
|
||||
The B button will turn sound back on if it's muted.
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
# Tunes
|
||||
Play a musical composition using the Arduboy.
|
||||
|
||||
A small composition is stored by `byte PROGMEM score`. The score is played in the sketch loop using `playScore(score)`.
|
Loading…
Reference in New Issue