Compare commits
8 Commits
v1.0.1
...
master.web
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1cbc07e9c | ||
|
|
6e5fc8ea10 | ||
|
|
a48db73abb | ||
|
|
28f029ce03 | ||
|
|
cb42a45aa7 | ||
|
|
c47291e258 | ||
|
|
fec4db0c5d | ||
|
|
7c065d8799 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -7,6 +7,8 @@ on:
|
|||||||
- 'arduino_pong.ino'
|
- 'arduino_pong.ino'
|
||||||
- 'Makefile'
|
- 'Makefile'
|
||||||
- '.github/workflows/ci.yml'
|
- '.github/workflows/ci.yml'
|
||||||
|
- 'src/*.cpp'
|
||||||
|
- 'src/*.h'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
.secrets
|
|
||||||
.arduino_cache/
|
.arduino_cache/
|
||||||
bin/
|
bin/
|
||||||
build/
|
build/
|
||||||
|
src/secrets.h
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ A classic implementation of the Pong game developed specifically for the Arduino
|
|||||||
|
|
||||||
# 📹 Preview
|
# 📹 Preview
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Youtube: https://youtu.be/ouLBTDjpKqc
|
Youtube: https://youtu.be/ouLBTDjpKqc
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
#include "Arduino_LED_Matrix.h"
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "src/config.h"
|
||||||
#include "pong_render.h"
|
#include "src/pong_render.h"
|
||||||
#include "pong_player.h"
|
#include "src/pong_player.h"
|
||||||
#include "pong_ball.h"
|
#include "src/pong_ball.h"
|
||||||
|
#include "src/arduino_network.h"
|
||||||
|
|
||||||
// create LED matrix object
|
// create LED matrix object
|
||||||
ArduinoLEDMatrix matrix;
|
ArduinoLEDMatrix matrix;
|
||||||
|
|
||||||
// initial pong frame
|
// initial pong frame matrix
|
||||||
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
@@ -30,32 +31,39 @@ int ball_y= BALL_RESET_Y;
|
|||||||
|
|
||||||
int need_refresh= 1;
|
int need_refresh= 1;
|
||||||
|
|
||||||
int loop_delay= INITIAL_LOOP_DELAY;
|
int ball_delay= INITIAL_BALL_DELAY;
|
||||||
|
|
||||||
long exec_t2= millis();
|
long exec_t2= millis();
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
//Serial.begin(115200);
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// stard LED matrix
|
// start LED matrix
|
||||||
matrix.begin();
|
matrix.begin();
|
||||||
pinMode(P1_BTN_UP, INPUT_PULLUP);
|
pinMode(P1_BTN_UP, INPUT_PULLUP);
|
||||||
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
|
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
|
||||||
pinMode(P2_BTN_UP, INPUT_PULLUP);
|
pinMode(P2_BTN_UP, INPUT_PULLUP);
|
||||||
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
|
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
|
||||||
|
|
||||||
randomSeed(analogRead(0));
|
randomSeed(millis());
|
||||||
|
|
||||||
|
setup_network();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
long exec_t1= millis();
|
long exec_t1= millis();
|
||||||
pong_move_p1(p1_start, need_refresh);
|
pong_move_p1(p1_start, need_refresh);
|
||||||
pong_move_p2(p2_start, need_refresh);
|
pong_move_p2(p2_start, need_refresh);
|
||||||
render_matrix(frame, p1_start, p2_start, need_refresh, ball_x, ball_y);
|
if (exec_t1 - exec_t2 > ball_delay) {
|
||||||
if (exec_t1 - exec_t2 > loop_delay) {
|
move_ball(ball_x, ball_y, ball_delay, p1_start, p2_start, need_refresh);
|
||||||
move_ball(ball_x, ball_y, loop_delay, p1_start, p2_start, need_refresh);
|
|
||||||
exec_t2= exec_t1;
|
exec_t2= exec_t1;
|
||||||
}
|
}
|
||||||
|
if (need_refresh) {
|
||||||
|
render_matrix(frame, p1_start, p2_start, ball_x, ball_y);
|
||||||
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
need_refresh= 0;
|
||||||
|
}
|
||||||
delay(50);
|
delay(50);
|
||||||
|
|
||||||
|
web_server();
|
||||||
}
|
}
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 847 KiB After Width: | Height: | Size: 847 KiB |
11
config.h
11
config.h
@@ -1,11 +0,0 @@
|
|||||||
#define P1_BTN_UP 13
|
|
||||||
#define P1_BTN_BOTTOM 12
|
|
||||||
#define P2_BTN_UP 11
|
|
||||||
#define P2_BTN_BOTTOM 10
|
|
||||||
|
|
||||||
#define MATRIX_WIDTH 12
|
|
||||||
#define MATRIX_HEIGHT 8
|
|
||||||
#define BALL_RESET_X (MATRIX_WIDTH / 2)
|
|
||||||
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
|
|
||||||
#define BAR_LENGTH 3
|
|
||||||
#define INITIAL_LOOP_DELAY 200
|
|
||||||
141
src/arduino_network.cpp
Normal file
141
src/arduino_network.cpp
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
#include "config.h"
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "WiFiS3.h"
|
||||||
|
|
||||||
|
char ssid[]= SECRET_SSID;
|
||||||
|
char pass[]= SECRET_PASS;
|
||||||
|
int status= WL_IDLE_STATUS;
|
||||||
|
WiFiServer server(80);
|
||||||
|
|
||||||
|
|
||||||
|
void printMacAddress(byte mac[]) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
Serial.print(":");
|
||||||
|
}
|
||||||
|
if (mac[i] < 16) {
|
||||||
|
Serial.print("0");
|
||||||
|
}
|
||||||
|
Serial.print(mac[i], HEX);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void printWifiData() {
|
||||||
|
// print your board's IP address:
|
||||||
|
IPAddress ip = WiFi.localIP();
|
||||||
|
Serial.print("IP Address: ");
|
||||||
|
|
||||||
|
Serial.println(ip);
|
||||||
|
|
||||||
|
// print your MAC address:
|
||||||
|
byte mac[6];
|
||||||
|
WiFi.macAddress(mac);
|
||||||
|
Serial.print("MAC address: ");
|
||||||
|
printMacAddress(mac);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printCurrentNet() {
|
||||||
|
// print the SSID of the network you're attached to:
|
||||||
|
Serial.print("SSID: ");
|
||||||
|
Serial.println(WiFi.SSID());
|
||||||
|
|
||||||
|
// print the MAC address of the router you're attached to:
|
||||||
|
byte bssid[6];
|
||||||
|
WiFi.BSSID(bssid);
|
||||||
|
Serial.print("BSSID: ");
|
||||||
|
printMacAddress(bssid);
|
||||||
|
|
||||||
|
// print the received signal strength:
|
||||||
|
long rssi = WiFi.RSSI();
|
||||||
|
Serial.print("signal strength (RSSI):");
|
||||||
|
Serial.println(rssi);
|
||||||
|
|
||||||
|
// print the encryption type:
|
||||||
|
byte encryption = WiFi.encryptionType();
|
||||||
|
Serial.print("Encryption Type:");
|
||||||
|
Serial.println(encryption, HEX);
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_network() {
|
||||||
|
// check for the WiFi module:
|
||||||
|
if (WiFi.status() == WL_NO_MODULE) {
|
||||||
|
Serial.println("Communication with WiFi module failed!");
|
||||||
|
// don't continue
|
||||||
|
while (true);
|
||||||
|
}
|
||||||
|
String fv = WiFi.firmwareVersion();
|
||||||
|
Serial.print("Firmware version: ");
|
||||||
|
Serial.println(fv);
|
||||||
|
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
|
||||||
|
Serial.println("Please upgrade the firmware");
|
||||||
|
}
|
||||||
|
|
||||||
|
// attempt to connect to WiFi network:
|
||||||
|
// Definisci i parametri di rete prima del loop di connessione
|
||||||
|
IPAddress local_IP(192, 168, 1, 200);
|
||||||
|
IPAddress gateway(192, 168, 1, 1);
|
||||||
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
|
IPAddress dns(9,9,9,9);
|
||||||
|
WiFi.config(local_IP, gateway, subnet, dns);
|
||||||
|
while (status != WL_CONNECTED) {
|
||||||
|
Serial.print("Attempting to connect to WPA SSID: ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
// Connect to WPA/WPA2 network:
|
||||||
|
status = WiFi.begin(ssid, pass);
|
||||||
|
|
||||||
|
// wait 10 seconds for connection:
|
||||||
|
delay(10000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// you're connected now, so print out the data:
|
||||||
|
Serial.println("You're connected to the network");
|
||||||
|
printCurrentNet();
|
||||||
|
printWifiData();
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_server() {
|
||||||
|
WiFiClient client = server.available();
|
||||||
|
|
||||||
|
if (client) { // if you get a client,
|
||||||
|
Serial.println("new client"); // print a message out the serial port
|
||||||
|
String currentLine = ""; // make a String to hold incoming data from the client
|
||||||
|
while (client.connected()) { // loop while the client's connected
|
||||||
|
if (client.available()) { // if there's bytes to read from the client,
|
||||||
|
char c = client.read(); // read a byte, then
|
||||||
|
Serial.write(c); // print it out to the serial monitor
|
||||||
|
if (c == '\n') { // if the byte is a newline character
|
||||||
|
|
||||||
|
// if the current line is blank, you got two newline characters in a row.
|
||||||
|
// that's the end of the client HTTP request, so send a response:
|
||||||
|
if (currentLine.length() == 0) {
|
||||||
|
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||||
|
// and a content-type so the client knows what's coming, then a blank line:
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-type:text/html");
|
||||||
|
client.println();
|
||||||
|
|
||||||
|
// the content of the HTTP response follows the header:
|
||||||
|
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
|
||||||
|
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
|
||||||
|
|
||||||
|
// The HTTP response ends with another blank line:
|
||||||
|
client.println();
|
||||||
|
// break out of the while loop:
|
||||||
|
break;
|
||||||
|
} else { // if you got a newline, then clear currentLine:
|
||||||
|
currentLine = "";
|
||||||
|
}
|
||||||
|
} else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||||
|
currentLine += c; // add it to the end of the currentLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// close the connection:
|
||||||
|
client.stop();
|
||||||
|
Serial.println("client disconnected");
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/arduino_network.h
Normal file
11
src/arduino_network.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef PONG_NETWORK_H
|
||||||
|
#define PONG_NETWORK_H
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
void setup_network();
|
||||||
|
void printMacAddress(byte mac[]);
|
||||||
|
void printWifiData();
|
||||||
|
void printCurrentNet();
|
||||||
|
void web_server();
|
||||||
|
|
||||||
|
#endif
|
||||||
19
src/config.h
Normal file
19
src/config.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#if __has_include("secrets.h")
|
||||||
|
#include "secrets.h"
|
||||||
|
#else
|
||||||
|
#define SECRET_SSID "Fallback_SSID"
|
||||||
|
#define SECRET_PASS "Fallback_PASS"
|
||||||
|
#warning "⚠️ secrets.h not found"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define P1_BTN_UP 12
|
||||||
|
#define P1_BTN_BOTTOM 11
|
||||||
|
#define P2_BTN_UP 10
|
||||||
|
#define P2_BTN_BOTTOM 9
|
||||||
|
|
||||||
|
#define MATRIX_WIDTH 12
|
||||||
|
#define MATRIX_HEIGHT 8
|
||||||
|
#define BALL_RESET_X (MATRIX_WIDTH / 2)
|
||||||
|
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
|
||||||
|
#define BAR_LENGTH 3
|
||||||
|
#define INITIAL_BALL_DELAY 200
|
||||||
@@ -13,7 +13,7 @@ int p2_score= 0;
|
|||||||
int ball_move_x= 0;
|
int ball_move_x= 0;
|
||||||
int ball_move_y= 0;
|
int ball_move_y= 0;
|
||||||
|
|
||||||
void point_scored(int &ball_x, int &ball_y, int &loop_delay) {
|
void point_scored(int &ball_x, int &ball_y, int &ball_delay) {
|
||||||
ball_x= BALL_RESET_X;
|
ball_x= BALL_RESET_X;
|
||||||
ball_y= BALL_RESET_Y;
|
ball_y= BALL_RESET_Y;
|
||||||
Serial.print("P1: ");
|
Serial.print("P1: ");
|
||||||
@@ -24,10 +24,10 @@ void point_scored(int &ball_x, int &ball_y, int &loop_delay) {
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
hits= 0;
|
hits= 0;
|
||||||
loop_delay= INITIAL_LOOP_DELAY;
|
ball_delay= INITIAL_BALL_DELAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_start, int &need_refresh) {
|
void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_start, int &need_refresh) {
|
||||||
need_refresh= 1;
|
need_refresh= 1;
|
||||||
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
|
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
|
||||||
// ball out of matrix limits
|
// ball out of matrix limits
|
||||||
@@ -52,7 +52,7 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
|
|||||||
// else p2 score, reset board
|
// else p2 score, reset board
|
||||||
p2_score += 1;
|
p2_score += 1;
|
||||||
Serial.println("Player 2 Scores");
|
Serial.println("Player 2 Scores");
|
||||||
point_scored(ball_x, ball_y, loop_delay);
|
point_scored(ball_x, ball_y, ball_delay);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
hits += 1;
|
hits += 1;
|
||||||
@@ -64,7 +64,7 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
|
|||||||
// else p1 score, reset board
|
// else p1 score, reset board
|
||||||
p1_score += 1;
|
p1_score += 1;
|
||||||
Serial.println("Player 1 Scores");
|
Serial.println("Player 1 Scores");
|
||||||
point_scored(ball_x, ball_y, loop_delay);
|
point_scored(ball_x, ball_y, ball_delay);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
hits += 1;
|
hits += 1;
|
||||||
@@ -77,10 +77,10 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
|
|||||||
ball_move_y= ball_move_y * -1;
|
ball_move_y= ball_move_y * -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hits >= 6 && loop_delay >= 80) {
|
if (hits >= 6 && ball_delay >= 80) {
|
||||||
// increase ball speed
|
// increase ball speed
|
||||||
hits = 0;
|
hits = 0;
|
||||||
loop_delay -= 20;
|
ball_delay -= 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
ball_x+= ball_move_x;
|
ball_x+= ball_move_x;
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int &need_refresh, int ball_x, int ball_y) {
|
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y) {
|
||||||
if (!need_refresh) return;
|
|
||||||
need_refresh= 0;
|
|
||||||
// clear
|
// clear
|
||||||
for (int x=0; x < MATRIX_WIDTH; x++) {
|
for (int x=0; x < MATRIX_WIDTH; x++) {
|
||||||
for (int y=0; y < MATRIX_HEIGHT; y++) {
|
for (int y=0; y < MATRIX_HEIGHT; y++) {
|
||||||
@@ -4,6 +4,6 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "Arduino_LED_Matrix.h"
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
|
||||||
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int &need_refresh, int ball_x, int ball_y);
|
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user