Migrate example sketches

Renamed Tunes to PlayTune and enhanced it to better demonstrate the
ArduboyPlaytune library's capabilities.
This commit is contained in:
Scott Allen 2016-06-30 19:30:33 -04:00
parent 6c5b2cd783
commit 3b96b7ba24
6 changed files with 140 additions and 91 deletions

View File

@ -9,13 +9,10 @@
version 2.1 of the License, or (at your option) any later version. version 2.1 of the License, or (at your option) any later version.
*/ */
#include <Arduboy.h> #include <Arduboy2.h>
#include <ArduboyPlaytune.h>
#include "breakout_bitmaps.h" #include "breakout_bitmaps.h"
Arduboy arduboy; Arduboy2 arduboy;
ArduboyPlaytune tunes;
AbPrinter text(arduboy);
const unsigned int COLUMNS = 13; //Columns of bricks const unsigned int COLUMNS = 13; //Columns of bricks
const unsigned int ROWS = 4; //Rows of bricks const unsigned int ROWS = 4; //Rows of bricks
@ -56,10 +53,7 @@ byte tick;
void setup() void setup()
{ {
arduboy.begin(); arduboy.begin();
arduboy.setFrameRate(60); arduboy.setFrameRate(25);
tunes.initChannel(PIN_SPEAKER_1);
tunes.initChannel(PIN_SPEAKER_2);
} }
void loop() void loop()
@ -179,7 +173,7 @@ void moveBall()
{ {
yb = 2; yb = 2;
dy = -dy; dy = -dy;
tunes.tone(523, 250); playTone(523, 250);
} }
//Lose a life if bottom edge hit //Lose a life if bottom edge hit
@ -191,7 +185,7 @@ void moveBall()
released = false; released = false;
lives--; lives--;
drawLives(); drawLives();
tunes.tone(175, 250); playTone(175, 250);
if (random(0, 2) == 0) if (random(0, 2) == 0)
{ {
dx = 1; dx = 1;
@ -207,7 +201,7 @@ void moveBall()
{ {
xb = 2; xb = 2;
dx = -dx; dx = -dx;
tunes.tone(523, 250); playTone(523, 250);
} }
//Bounce off right side //Bounce off right side
@ -215,7 +209,7 @@ void moveBall()
{ {
xb = WIDTH - 4; xb = WIDTH - 4;
dx = -dx; dx = -dx;
tunes.tone(523, 250); playTone(523, 250);
} }
//Bounce off paddle //Bounce off paddle
@ -227,7 +221,7 @@ void moveBall()
if (dx == 0) { if (dx == 0) {
dx = (random(0,2) == 1) ? 1 : -1; dx = (random(0,2) == 1) ? 1 : -1;
} }
tunes.tone(200, 250); playTone(200, 250);
} }
//Bounce off Bricks //Bounce off Bricks
@ -261,7 +255,7 @@ void moveBall()
dy =- dy; dy =- dy;
yb += dy; yb += dy;
bounced = true; bounced = true;
tunes.tone(261, 250); playTone(261, 250);
} }
} }
@ -274,7 +268,7 @@ void moveBall()
dx =- dx; dx =- dx;
xb += dx; xb += dx;
bounced = true; bounced = true;
tunes.tone(261, 250); playTone(261, 250);
} }
} }
} }
@ -313,9 +307,9 @@ void moveBall()
void drawBall() void drawBall()
{ {
// text.setCursor(0,0); // arduboy.setCursor(0,0);
// text.print(arduboy.cpuLoad()); // arduboy.print(arduboy.cpuLoad());
// text.print(" "); // arduboy.print(" ");
arduboy.drawPixel(xb, yb, 0); arduboy.drawPixel(xb, yb, 0);
arduboy.drawPixel(xb+1, yb, 0); arduboy.drawPixel(xb+1, yb, 0);
arduboy.drawPixel(xb, yb+1, 0); arduboy.drawPixel(xb, yb+1, 0);
@ -339,8 +333,8 @@ void drawPaddle()
void drawLives() void drawLives()
{ {
sprintf(text_buffer, "LIVES:%u", lives); sprintf(text_buffer, "LIVES:%u", lives);
text.setCursor(0, 90); arduboy.setCursor(0, 90);
text.print(text_buffer); arduboy.print(text_buffer);
} }
void drawGameOver() void drawGameOver()
@ -349,10 +343,10 @@ void drawGameOver()
arduboy.drawPixel(xb+1, yb, 0); arduboy.drawPixel(xb+1, yb, 0);
arduboy.drawPixel(xb, yb+1, 0); arduboy.drawPixel(xb, yb+1, 0);
arduboy.drawPixel(xb+1, yb+1, 0); arduboy.drawPixel(xb+1, yb+1, 0);
text.setCursor(52, 42); arduboy.setCursor(52, 42);
text.print( "Game"); arduboy.print( "Game");
text.setCursor(52, 54); arduboy.setCursor(52, 54);
text.print("Over"); arduboy.print("Over");
arduboy.display(); arduboy.display();
delay(4000); delay(4000);
} }
@ -361,8 +355,8 @@ void pause()
{ {
paused = true; paused = true;
//Draw pause to the screen //Draw pause to the screen
text.setCursor(52, 45); arduboy.setCursor(52, 45);
text.print("PAUSE"); arduboy.print("PAUSE");
arduboy.display(); arduboy.display();
while (paused) while (paused)
{ {
@ -383,8 +377,8 @@ void Score()
{ {
score += (level*10); score += (level*10);
sprintf(text_buffer, "SCORE:%u", score); sprintf(text_buffer, "SCORE:%u", score);
text.setCursor(80, 90); arduboy.setCursor(80, 90);
text.print(text_buffer); arduboy.print(text_buffer);
} }
void newLevel(){ void newLevel(){
@ -417,8 +411,8 @@ void newLevel(){
//Draws the initial score //Draws the initial score
sprintf(text_buffer, "SCORE:%u", score); sprintf(text_buffer, "SCORE:%u", score);
text.setCursor(80, 90); arduboy.setCursor(80, 90);
text.print(text_buffer); arduboy.print(text_buffer);
} }
//Used to delay images while reading button input //Used to delay images while reading button input
@ -448,15 +442,15 @@ boolean displayHighScores(byte file)
int address = file*10*5; int address = file*10*5;
byte hi, lo; byte hi, lo;
arduboy.clear(); arduboy.clear();
text.setCursor(32, 0); arduboy.setCursor(32, 0);
text.print("HIGH SCORES"); arduboy.print("HIGH SCORES");
arduboy.display(); arduboy.display();
for(int i = 0; i < 10; i++) for(int i = 0; i < 10; i++)
{ {
sprintf(text_buffer, "%2d", i+1); sprintf(text_buffer, "%2d", i+1);
text.setCursor(x,y+(i*8)); arduboy.setCursor(x,y+(i*8));
text.print(text_buffer); arduboy.print(text_buffer);
arduboy.display(); arduboy.display();
hi = EEPROM.read(address + (5*i)); hi = EEPROM.read(address + (5*i));
lo = EEPROM.read(address + (5*i) + 1); lo = EEPROM.read(address + (5*i) + 1);
@ -477,8 +471,8 @@ boolean displayHighScores(byte file)
if (score > 0) if (score > 0)
{ {
sprintf(text_buffer, "%c%c%c %u", initials[0], initials[1], initials[2], score); sprintf(text_buffer, "%c%c%c %u", initials[0], initials[1], initials[2], score);
text.setCursor(x + 24, y + (i*8)); arduboy.setCursor(x + 24, y + (i*8));
text.print(text_buffer); arduboy.print(text_buffer);
arduboy.display(); arduboy.display();
} }
} }
@ -494,10 +488,10 @@ boolean titleScreen()
{ {
//Clears the screen //Clears the screen
arduboy.clear(); arduboy.clear();
text.setCursor(16,22); arduboy.setCursor(16,22);
text.setSize(2); arduboy.setTextSize(2);
text.print("ARAKNOID"); arduboy.print("ARAKNOID");
text.setSize(1); arduboy.setTextSize(1);
arduboy.display(); arduboy.display();
if (pollFireButton(25)) if (pollFireButton(25))
{ {
@ -509,8 +503,8 @@ boolean titleScreen()
{ {
//Draws "Press FIRE" //Draws "Press FIRE"
//arduboy.bitmap(31, 53, fire); arduboy.display(); //arduboy.bitmap(31, 53, fire); arduboy.display();
text.setCursor(31, 53); arduboy.setCursor(31, 53);
text.print("PRESS FIRE!"); arduboy.print("PRESS FIRE!");
arduboy.display(); arduboy.display();
if (pollFireButton(50)) if (pollFireButton(50))
@ -519,10 +513,10 @@ boolean titleScreen()
} }
//Removes "Press FIRE" //Removes "Press FIRE"
arduboy.clear(); arduboy.clear();
text.setCursor(16,22); arduboy.setCursor(16,22);
text.setSize(2); arduboy.setTextSize(2);
text.print("ARAKNOID"); arduboy.print("ARAKNOID");
text.setSize(1); arduboy.setTextSize(1);
arduboy.display(); arduboy.display();
arduboy.display(); arduboy.display();
@ -551,17 +545,17 @@ void enterInitials()
arduboy.display(); arduboy.display();
arduboy.clear(); arduboy.clear();
text.setCursor(16,0); arduboy.setCursor(16,0);
text.print("HIGH SCORE"); arduboy.print("HIGH SCORE");
sprintf(text_buffer, "%u", score); sprintf(text_buffer, "%u", score);
text.setCursor(88, 0); arduboy.setCursor(88, 0);
text.print(text_buffer); arduboy.print(text_buffer);
text.setCursor(56, 20); arduboy.setCursor(56, 20);
text.print(initials[0]); arduboy.print(initials[0]);
text.setCursor(64, 20); arduboy.setCursor(64, 20);
text.print(initials[1]); arduboy.print(initials[1]);
text.setCursor(72, 20); arduboy.setCursor(72, 20);
text.print(initials[2]); arduboy.print(initials[2]);
for(byte i = 0; i < 3; i++) for(byte i = 0; i < 3; i++)
{ {
arduboy.drawLine(56 + (i*8), 27, 56 + (i*8) + 6, 27, 1); arduboy.drawLine(56 + (i*8), 27, 56 + (i*8) + 6, 27, 1);
@ -578,7 +572,7 @@ void enterInitials()
index = 0; index = 0;
} else } else
{ {
tunes.tone(1046, 250); playTone(1046, 250);
} }
} }
@ -589,14 +583,14 @@ void enterInitials()
{ {
index = 2; index = 2;
} else { } else {
tunes.tone(1046, 250); playTone(1046, 250);
} }
} }
if (arduboy.pressed(DOWN_BUTTON)) if (arduboy.pressed(DOWN_BUTTON))
{ {
initials[index]++; initials[index]++;
tunes.tone(523, 250); playTone(523, 250);
// A-Z 0-9 :-? !-/ ' ' // A-Z 0-9 :-? !-/ ' '
if (initials[index] == '0') if (initials[index] == '0')
{ {
@ -619,7 +613,7 @@ void enterInitials()
if (arduboy.pressed(UP_BUTTON)) if (arduboy.pressed(UP_BUTTON))
{ {
initials[index]--; initials[index]--;
tunes.tone(523, 250); playTone(523, 250);
if (initials[index] == ' ') { if (initials[index] == ' ') {
initials[index] = '?'; initials[index] = '?';
} }
@ -639,9 +633,9 @@ void enterInitials()
if (index < 2) if (index < 2)
{ {
index++; index++;
tunes.tone(1046, 250); playTone(1046, 250);
} else { } else {
tunes.tone(1046, 250); playTone(1046, 250);
return; 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);
}
}

View File

@ -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. 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 // Make an instance of arduboy used for many functions
Arduboy arduboy; Arduboy2 arduboy;
AbPrinter text(arduboy);
// Variables for your game go here. // Variables for your game go here.
char title[] = "Press Buttons!"; char title[] = "Press Buttons!";
@ -97,10 +96,10 @@ void loop() {
arduboy.clear(); arduboy.clear();
// we set our cursor x pixels to the right and y down from the top // 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 // then we print to screen what is stored in our title variable we declared earlier
text.print(title); arduboy.print(title);
// then we finaly we tell the arduboy to display what we just wrote to the display. // then we finaly we tell the arduboy to display what we just wrote to the display.
arduboy.display(); arduboy.display();

View File

@ -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. 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 // make an instance of arduboy used for many functions
Arduboy arduboy; Arduboy2 arduboy;
AbPrinter text(arduboy);
// This function runs once in your game. // 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 // we set our cursor 5 pixels to the right and 10 down from the top
// (positions start at 0, 0) // (positions start at 0, 0)
text.setCursor(4, 9); arduboy.setCursor(4, 9);
// then we print to screen what is in the Quotation marks "" // 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 // then we finaly we tell the arduboy to display what we just wrote to the display
arduboy.display(); arduboy.display();

View File

@ -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> #include <ArduboyPlaytune.h>
const byte PROGMEM score [] = { 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, 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}; 0x80, 0x81, 0x90,0x45, 0,91, 0,136, 0x80, 0x82, 0x90,0x45, 0x91,0x2D, 7,83, 0x80, 0x81, 0xf0};
Arduboy arduboy; Arduboy2 arduboy;
AbPrinter text(arduboy); ArduboyPlaytune tunes(arduboy.audio.enabled);
ArduboyPlaytune tunes;
void setup() void setup()
{ {
arduboy.begin(); arduboy.begin();
text.setSize(4);
text.setCursor(0,0); arduboy.setFrameRate(10);
text.print("Music\nDemo"); arduboy.setTextSize(3);
// audio setup // audio setup
arduboy.audio.on();
tunes.initChannel(PIN_SPEAKER_1); tunes.initChannel(PIN_SPEAKER_1);
#ifndef AB_DEVKIT
// if not a DevKit
tunes.initChannel(PIN_SPEAKER_2); 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 // pause render until it's time for the next frame
if (!(arduboy.nextFrame())) if (!(arduboy.nextFrame()))
@ -185,23 +211,31 @@ void loop ()
if (arduboy.pressed(UP_BUTTON)) { if (arduboy.pressed(UP_BUTTON)) {
y-=1; y-=1;
tunes.tone(1175,300);
} else if (arduboy.pressed(DOWN_BUTTON)) { } else if (arduboy.pressed(DOWN_BUTTON)) {
y+=1; y+=1;
tunes.tone(1397,300);
} else if (arduboy.pressed(LEFT_BUTTON)) { } else if (arduboy.pressed(LEFT_BUTTON)) {
x-=1; x-=1;
tunes.tone(1047,300);
} else if (arduboy.pressed(RIGHT_BUTTON)) { } else if (arduboy.pressed(RIGHT_BUTTON)) {
x+=1; x+=1;
tunes.tone(1319,300);
} }
if (arduboy.pressed(A_BUTTON)) { if (arduboy.pressed(A_BUTTON)) {
arduboy.invert(true); arduboy.invert(true);
arduboy.audio.off();
} else if (arduboy.pressed(B_BUTTON)) { } else if (arduboy.pressed(B_BUTTON)) {
arduboy.invert(false); arduboy.invert(false);
arduboy.audio.on();
} }
arduboy.clear(); arduboy.clear();
text.setCursor(x,y); arduboy.setCursor(x,y);
text.print("Music\nDemo"); arduboy.print("Music");
arduboy.setCursor(x+8,y+24);
arduboy.print("Demo");
arduboy.display(); arduboy.display();
// play the tune if we aren't already // play the tune if we aren't already

View File

@ -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.

View File

@ -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)`.