Skip to content

Commit

Permalink
Merge pull request #1 from ElRojo/adding-rewrite-feature
Browse files Browse the repository at this point in the history
Adding rewrite feature
  • Loading branch information
ElRojo authored Aug 16, 2022
2 parents ae7e882 + 7b00859 commit b18dd68
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 79 deletions.
Binary file added .DS_Store
Binary file not shown.
57 changes: 47 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Forked from: [javiwwweb/MisTerRFID](https://github.com/javiwwweb/MisTerRFID)
Most of the ReadMe below is copy/paste from javiwwweb. I have added some notes for my changes.
# MisTerRFID
Enables RFID card launching of games for MiSTer FPGA. Launches games without any menu being display using the MiSTer Game Launcher files (MGL) method. Must use a version of MiSTer from Feb 24, 2022 or after to support this method of launching. I have confirmed this working with all Arcade and Neo Geo cores.
Enables RFID card launching of games for MiSTer FPGA. Launches games without any menu being display using the MiSTer Game Launcher files (MGL) method. Must use a version of MiSTer from Feb 24, 2022 or after to support this method of launching.

*This version allows you to write games to cards without needing to edit the `rfid_process.sh` file.*

## Hardware Needed
- Arduino Nano V3.0
Expand All @@ -18,9 +22,13 @@ Enables RFID card launching of games for MiSTer FPGA. Launches games without any
|VCC|3.3V|
|GRD|GRD|

The first step is to write the misterrfid.ino file to your Arduino. If you find out that that you would like to extend the distance the card is picked up, You can adjust the receiver gain by editing the following line of the misterrfid.ino before writing it:
:warning: Skipping steps below will cause your code to not function! :warning:

Install the `Easy MFRC522` library through your Arduino IDE. Write the misterrfid.ino file to your Arduino. If you find out that that you would like to extend the distance the card is picked up, You can adjust the receiver gain by editing line `21` of the `misterrfid.ino` and upload the code.

Line that needs to be edited : rfid.PCD_SetRegisterBitMask(rfid.RFCfgReg, (0x03<<4));
```
rfid.PCD_SetRegisterBitMask(rfid.RFCfgReg, (0x03<<4)); // RFID Gain
```

The HEX 0x03 control the gain. In my case that was the value that would penetrate the wooden bezel and the glass at the perfect distance. You may need to adjust this value set the receiver's gain to your desired level

Expand All @@ -34,18 +42,47 @@ The HEX 0x03 control the gain. In my case that was the value that would penetrat
| 0x06 = 43 dB HEX |
| 0x07 = 48 dB HEX |

On your computer, attach the serial monitor to your Arduino and you should see it repeating `. rfid_process.sh noscan` about every second. As soon as your scan a RFID card, it should output `. rfid_process.sh 12345678`. The number is that card's unique ID. The reader will not scan the same card two times in a row. Make note of the card's unique id.
On your computer, attach the serial monitor to your Arduino and you should see it repeating `. rfid_process.sh noscan` about every second. As soon as your scan a RFID card, it should output `. rfid_process.sh 12345678`. The number is that card's unique ID. The reader will not scan the same card two times in a row, but you can scan another card, and then the original in order to see a card's number again.

### Write Card Setup
As you are gathering numbers from your cards or RFID tags, choose an RFID device that you'd like to use as a `write card` This card acts as a trigger to put your Arduino code into a loop that will run the `rfid_write.sh` file. The number of this RFID device needs to be replace `12346789` in `misterrfid.ino` at line `58`. After you have done this, overwrite your Arduino with the new code. Set that card aside for later.

```
const uint32_t wCard = 123456789;
```

## MiSTer Setup
Copy the files to your MiSTer SD card based on the structure of this repo. Edit case statement of the "rfid_process.sh" script to associate your RFID card UIDs with the roms files on your MiSTer. (Sample cores and UID's are provided in the file)
Copy the files to your MiSTer SD card based on the structure of this repo. Edit case statement of the `rfid_process.sh` script. The line-structure is as follows:

```
case "$1" in
"1234567890") play "Street Fighter II' Hyper Fighting -World 921209-" ;;
esac
```

Note that the roms need to be the *filename* only, without the extension (no .mra). Spaces are important and so are quotations. Take care when adding files this way.

:warning: When adding files manually be careful not to move the beginning of the `case` statement from line 13.:warning:

Doing so will cause the `rfid_write.sh` file to break. If you *do* need to move the `case` block around, please check the comment in `rfid_write.sh` to move your injection point for creating new games."

*This function is set up for _Arcade games only, but can be easily adapted to other cores. I would suggest looking at [illusion-pasure-program](https://github.com/illusion-pasture-program/snesRFID)'s `original rfid_process.sh` file*

## Use
After editing the rfid_process.sh script, turn off your MiSTer. Plug your Arduino into an available USB port on your USB board module and turn on your MiSTer. Depending on how many scripts you have running, it can take up to 30 seconds from first turning on the power to the RFID reader becoming available. Once the RFID reader is available, you can scan your RFID card to launch any core.
After editing the `rfid_process.sh` script, turn off your MiSTer. Plug your Arduino into an available USB port on your USB board module and turn on your MiSTer. Depending on how many scripts you have running, it can take up to 30 seconds from first turning on the power to the RFID reader becoming available. Once the RFID is available, you can start lunching games (if you added cases to the `rfid_process.sh` file), or begin [Assigning Games to Cards](#Assigning-Games-to-Cards)

Note: This can be combined with MisTer.ini option bootcore= to automatically launch an arcade core (MRA file) upon starting up your MisTer. The `rfid_process.sh` will run in the background waiting for a card to be presented.

## Assigning Games to Cards
First, launch a game using the core menu. Once the game has booted, scan your `write card`. This will tell the Arduino that it needs to run the `rfid_write.sh` file on the next card scan. Scan a new (or already assigned) card. The card will be programaticaly added to `rfid_process.sh` and the next time you scan that card, it will boot the game. Remember that you cannot scan the same card twice, though. Scan a different card before scanning the just-written one in order to test it.

Note: This can be combined with MisTer.ini option bootcore= to automatically launch an arcade core (MRA file) upon starting up your MisTer. The rfid_process.sh will run in the background waiting for a card to be presented.
*Cards can be overwritten. If you attempt to scan a card that is already added to the `rfid_process.sh` file, the entry will be deleted and then reassigned to the new game. You can do this as often as you'd like.*

### Disclaimer
I'm not a Programmer, but the code works for me.
## Troubleshooting
- If your cards don't seem to be scanning in MiSTer, make sure that `serial_listen.sh` actually started. I have had issues with that not booting in the past.
- If games aren't being added to the right spot, or being injected in odd places in `rfid_process.sh` make sure you didn't inadvertently move the `case` statement. Read [MiSTer Setup](#MiSTer-Setup).
- If your write card doesn't function. Make sure you added the card number to the Arduino code **and** re-uploaded after making that change.

### THANK YOU
A word of Thanks to illusion-pasture-program from all of this code. I just adapted it to work inside an Arcade cabinet to my liking!
*Thanks to illusion-pasture-program and javiwwweb for the initial code and ideas.*
Binary file added fat/.DS_Store
Binary file not shown.
Binary file added fat/media/.DS_Store
Binary file not shown.
38 changes: 7 additions & 31 deletions fat/media/Scripts/rfid_process.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
#!/bin/bash

qload_rom()
{
if [ "$LASTROM" != "$3" ]
then
export LASTROM="$3"
echo "$LASTROM"
play() {
echo "load_core /media/fat/_Arcade/"$1".mra" > /dev/MiSTer_cmd
}

if [ ! -f /media/fat/Games/"$1"/"$2".mgl ]
then
echo "<mistergamedescription><rbf>_console/"$1"</rbf><file delay=\"2\" type=\"f\" index=\"0\" path=\"../../games/"$1"/"$2"\"/></mistergamedescription>" > "/media/fat/Games/"$1"/"$2".mgl"
fi
echo "load_core /media/fat/Games/"$1"/"$2".mgl" > /dev/MiSTer_cmd

fi
ha_cmd() {
TOKEN="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkYmY4ZmIxMWEyNjU0MzVkYmZhODY3NjA0YWIwNTc5OCIsImlhdCI6MTY2MDQ1NjI0NSwiZXhwIjoxOTc1ODE2MjQ1fQ.3Lvgl5ke4m55NNQxnLsEQSMM7OEkyWvL5mtTve6VnhE"
curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d $1 $2
}

case "$1" in
"2208192798") echo load_core /media/fat/_Arcade/'Robocop (World revision 4).mra' > /dev/MiSTer_cmd;;
"1944463134") echo load_core /media/fat/_Arcade/'Final Fight -World, set 1-.mra' > /dev/MiSTer_cmd;;
"2212386078") echo load_core /media/fat/_Arcade/'Ghosts'\''n Goblins (JT).mra' > /dev/MiSTer_cmd;;
"3841642020") echo load_core /media/fat/_Arcade/'Double Dragon.mra' > /dev/MiSTer_cmd;;
"2788154149") echo load_core /media/fat/_Arcade/'Double Dragon II - The Revenge.mra' > /dev/MiSTer_cmd;;
"2785325093") echo load_core /media/fat/_Arcade/'Kung-Fu Master.mra' > /dev/MiSTer_cmd;;
"2532548645") echo load_core /media/fat/_Arcade/'Shinobi (Set 6, World, S16A) [No Protection].mra' > /dev/MiSTer_cmd;;
"3571898404") echo load_core /media/fat/_Arcade/'R-Type (World).mra' > /dev/MiSTer_cmd;;
"3836830500") echo load_core /media/fat/_Arcade/'Vigilante (World, Rev E).mra' > /dev/MiSTer_cmd;;
"4095029796") echo load_core /media/fat/_Arcade/'Bad Dudes vs. Dragonninja (US revision 1).mra' > /dev/MiSTer_cmd;;
"4100998436") echo load_core /media/fat/_Arcade/"Ghouls'n Ghosts -World-.mra" > /dev/MiSTer_cmd;;
"4095293732") echo load_core /media/fat/_Arcade/'Rush'\''n Attack (US).mra' > /dev/MiSTer_cmd;;
"4097148196") echo load_core /media/fat/_Arcade/'Altered Beast (set 8) (8751 317-0078).mra' > /dev/MiSTer_cmd;;
"3841431588") echo load_core /media/fat/_Arcade/'Golden Axe (set 6, US) (8751 317-123A).mra' > /dev/MiSTer_cmd;;
"3570877988") echo load_core /media/fat/_Arcade/"Street Fighter II' Hyper Fighting -World 921209-.mra" > /dev/MiSTer_cmd;;
"2793426725") echo load_core /media/fat/_Arcade/'Rastan (World Rev 1).mra' > /dev/MiSTer_cmd;;
"2790045733") qload_rom NEOGEO mslugx "$1";;
"3837547300") qload_rom NEOGEO samsho2 "$1";;

esac
19 changes: 19 additions & 0 deletions fat/media/Scripts/rfid_write.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
write_rom()
{
cardNumber="$1"
runningGame=\"$(ps aux | grep .mra)\"
get_title() {
trimPath=${1##*".rbf /media/fat/_Arcade/"}
removeExtra=${trimPath%%.mra*}
echo $removeExtra
}
gameTitle=$(get_title "$runningGame")

rfidFile=rfid_process.sh
sed -i "/$cardNumber/d" "$rfidFile"
# '13i...' is the starting line for the case-statement. Change this if you add code or newlines to rfid_process.sh
sed -i "13i \"$cardNumber\") "play" \"$gameTitle\" ;;" "$rfidFile"
}

write_rom "$1"

86 changes: 48 additions & 38 deletions misterrfid/misterrfid.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;
MFRC522 rfid(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

int codeRead = 0;
uint32_t cardid = 0;
int cardBeenRead = 0;
uint32_t lastCardRead = 0;
int waitForIt = 0;
String uidString;
void setup() {
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
rfid.PCD_SetRegisterBitMask(rfid.RFCfgReg, (0x03<<4));
rfid.PCD_SetRegisterBitMask(rfid.RFCfgReg, (0x03<<4)); // RFID Gain
pinMode(8,OUTPUT);
pinMode(A0,OUTPUT);
Serial.println("loaded");
Expand All @@ -34,49 +35,58 @@ void loop() {

if (waitForIt >= 9) {
waitForIt = 0;
}
if( rfid.PICC_IsNewCardPresent())
}

if ( rfid.PICC_IsNewCardPresent())
{
readRFID();
readRFID();
}
delay(100);
}

void cardLogic(String proc, uint32_t cardNum) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.print(proc);
Serial.println(cardNum);
lastCardRead = cardNum;
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
}

void readRFID()
{
rfid.PICC_ReadCardSerial();
{
const uint32_t wCard = 3817941294; // Writing Card
rfid.PICC_ReadCardSerial();
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {

if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
//tag is a MIFARE Classic."
return;
}

uint32_t cardid = rfid.uid.uidByte[0];
cardid <<= 8;
cardid |= rfid.uid.uidByte[1];
cardid <<= 8;
cardid |= rfid.uid.uidByte[2];
cardid <<= 8;
cardid |= rfid.uid.uidByte[3];

cardBeenRead = 1;

if (cardid != lastCardRead) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.print(". rfid_process.sh ");
Serial.println(cardid);
lastCardRead = cardid;
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}

// Halt PICC
}

cardid = rfid.uid.uidByte[0];
cardid <<= 8;
cardid |= rfid.uid.uidByte[1];
cardid <<= 8;
cardid |= rfid.uid.uidByte[2];
cardid <<= 8;
cardid |= rfid.uid.uidByte[3];

cardBeenRead = 1;

if (lastCardRead == wCard && cardid != wCard) {
cardLogic(". rfid_write.sh ", cardid);
}
else if (cardid != lastCardRead) {
cardLogic(". rfid_process.sh ", cardid);
}


// Halt PICC
rfid.PICC_HaltA();

// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
}

0 comments on commit b18dd68

Please sign in to comment.