• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
RetroPie forum home
  • Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login

NFC enabled EmulationStation

Scheduled Pinned Locked Moved Ideas and Development
emulationstationfc
13 Posts 5 Posters 6.7k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E
    ElBartoME
    last edited by ElBartoME 13 Nov 2017, 19:52

    Hi!

    For my custom SNES mini (https://www.thingiverse.com/thing:2492366) I was looking for a way to read and write NFC tags on the Raspberry Pi in order to emulate a cartridge slot.

    I saw some solutions to this problem, for example a python script that kills EmulationStation and starts the game. I didn't like that solution and I wanted to be able to do everything inside EmulationStation.

    So what I did was modifying EmulationStation by adding libnfc for handling the NFC stuff and pigpiod for reading the GPIOs of the Pi.

    The code is very basic for now. I'm going to rework parts of it and make the integration a little bit nicer. But it is working! I tested it with MIFARE Ultralight C tags inside SNES cartridges with no problem. I to a NFC tag reader and hooked it up via I2C to the Pi. By pressing the reset switch on my "SNES" (in this case GPIO 20) for over 3 seconds it will write the game that you have selected in EmulationStation to the tag. It will write the filename and the system. For now only NES, SNES, GB, GBC, GBA and GENESIS are supported, but that can be easily changed. If you push the button under 3 seconds it will read the data from the NFC tag and start the game.

    I was planning to use the NDEF format to save the data on the NFC tag but that was more complicated than I thought so I instead write the data directly into the tag. Works without any problems, but the downside is that you are only able to read and write tags inside EmulationStation.

    The only problems I have right now are:

    • Reading and writing are not that fast. It will take several seconds until a game is started. Was able to speed up the read process quite a bit. Writing still takes some time.
    • No notification when we are done with writing to the tag. Added a notification when the write process is complete!
    • There is an odd bug. Everytime I start a game with an NFC tag this particular game will disappear from the Favorites menu. After a restart the game will appear again. Anybody have an idea what is going on? Fixed! Thanks pjft!

    Link to the repository: https://github.com/ElBartoME/EmulationStation/

    Feedback would be welcome!

    In action:

    1 Reply Last reply Reply Quote 3
    • P
      pjft
      last edited by 15 Nov 2017, 07:34

      This is a really cool setup you have there, and the NFC read/write looks spot on. I wonder, without having read the code, what are you calling to launch the game and why would it disappear from the favorites menu.

      What does the button in your case actually invoke? I see that if you hold it it writes, and if you press it it reads the game?

      E 1 Reply Last reply 17 Nov 2017, 19:01 Reply Quote 1
      • E
        ElBartoME @pjft
        last edited by ElBartoME 17 Nov 2017, 19:01

        @pjft

        Thanks!

        I do everything in the main.cpp of es-app because I'm lazy. I check if the button hold time was long or short. If it is short (< 4 s) then I will read the tag. If long, I will get the selected game info and write it into the tag. The tag has filename and system information of the game.

        I then create a string with the filepath of the game and search for the system. Then I'll start the game through the ViewController.

        std::string path = "/home/pi/RetroPie/roms/" + temp.gametype + "/" + temp.filename;
        boost::filesystem::path filepath{path.c_str()};
        std::vector<SystemData*> Systems = SystemData::sSystemVector;
        for(auto i = Systems.begin(); i != Systems.end(); i++)
        {
        	if((*i)->getName() == temp.gametype)
        	{
        		FileData *game = new FileData(GAME, filepath.generic_string(),(*i)->getSystemEnvData(),(*i));
        		ViewController::get()->launch(game);
        	}
        }
        

        Somehow this code is responsible for temporarily removing entries out of the favorites menu...but why?

        P.S.: I know creating the string with a fixed path is not a good idea. But I'm not sure where the default rom folder is stored in EmulationStation. Anybody got an idea?

        P 1 Reply Last reply 18 Nov 2017, 07:04 Reply Quote 1
        • P
          pjft @ElBartoME
          last edited by 18 Nov 2017, 07:04

          @elbartome yeah. I'm pretty sure I can help with this. Maybe when I'm on my laptop on Monday, would that work?

          Basically the fact that you're creating a new file data means that you're not using the current one that exists. As such it may be that it may replace the existing one in the favorites when you update its play count as there can only be one file data per path in ES.

          If you're using collections, you can get the right file data by accessing the all games collection using the path as key.

          Let me know if this helps or if you need more specifics, but that's my gut feeling. I can look into the code on Monday or so.

          1 Reply Last reply Reply Quote 1
          • P
            pjft
            last edited by 23 Nov 2017, 16:25

            Sorry, just noticed I had left this waiting for a while now.

            So, you have two options there.

            Probably the easiest one for you, in this case, is after finding the right system, to get the in-memory FileData games in the system by running

            const std::unordered_map<std::string, FileData*>& children = (*i)->getRootFolder()->getChildrenByFilename()
            

            and then, depending on how they're keyed (I don't know off the top of my head) you can either iterate on them and compare the file name until you find the one that matches, and then use that to launch the game, or ideally you'd be able to access it by key:

            bool found = children.find(temp.filename) != children.end();
            if (found) {
                FileData* game = children.at(temp.filename);
                ViewController::get()->launch(game);
            }
            

            but I'm not sure if the key is the filename only, or if it has the longer path.

            Might be worth trying it or even logging the keys as you iterate, just to see what to expect.

            Best.

            E 1 Reply Last reply 23 Nov 2017, 21:18 Reply Quote 1
            • E
              ElBartoME @pjft
              last edited by 23 Nov 2017, 21:18

              @pjft
              Thank you very much! Unfortunately work is keeping me busy and I wasn't able to modify the code.

              I will probably try that this weekend and get back to you if it works. I can step through the code and look at the keys!

              Is going trough every game not going to cost a lot of time? Especially if you have 500+ games in one system.

              P 1 Reply Last reply 24 Nov 2017, 09:38 Reply Quote 0
              • P
                pjft @ElBartoME
                last edited by 24 Nov 2017, 09:38

                @elbartome It shouldn't take that long, really. It's not ideal in concept, but it's not the end of the world.

                E 1 Reply Last reply 26 Nov 2017, 16:34 Reply Quote 1
                • E
                  ElBartoME @pjft
                  last edited by 26 Nov 2017, 16:34

                  @pjft
                  Thank you very much for your help. I never would have been able to figure that out!

                  It works perfectly with your suggested changes! I will push the update to git after some cleanup in the code.

                  P 1 Reply Last reply 28 Nov 2017, 11:34 Reply Quote 1
                  • P
                    pjft @ElBartoME
                    last edited by 28 Nov 2017, 11:34

                    @elbartome Glad to hear! Out of curiosity, what was the solution you ended up pursuing?

                    I'm curious about what causes the delay in reading the cartridge and starting the game - is it the reading part, or is it actually launching the game in ES?

                    Either way, well done - and thanks for sharing!

                    E 1 Reply Last reply 29 Nov 2017, 23:01 Reply Quote 1
                    • E
                      ElBartoME @pjft
                      last edited by 29 Nov 2017, 23:01

                      @pjft I used the filename as a key. Works flawlessly. It is more or less your code 1:1.

                      The delay is caused by the NFC routines. I was able to shrink it down to about 5 seconds. I don't think I can go down much more, but I will look into the NFC routines when I have more time.

                      Launching the game in ES is really quick!

                      E 1 Reply Last reply 6 Sept 2023, 02:21 Reply Quote 1
                      • B
                        barrymossel
                        last edited by 3 Jan 2018, 19:04

                        This might be cool with a coin door as well. Toss in a (round) NFC tag and attach a NFC reader inside... Like this!

                        1 Reply Last reply Reply Quote 0
                        • E
                          esbat1 @ElBartoME
                          last edited by 6 Sept 2023, 02:21

                          @ElBartoME could you helpme to do the same with snes classic mini console with hakchi mod installed?

                          M 1 Reply Last reply 6 Sept 2023, 02:53 Reply Quote 0
                          • M
                            mitu Global Moderator @esbat1
                            last edited by 6 Sept 2023, 02:53

                            @esbat1 I don't think this is the right forum for your issue. The original poster hasn't been here in over 5 years, so I don't think you'll get an answer. Maybe try Reddit's forum for hakchi / snesmini mods ?

                            1 Reply Last reply Reply Quote 0
                            • M mitu locked this topic on 6 Sept 2023, 02:53
                            • First post
                              Last post

                            Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                            Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.

                              This community forum collects and processes your personal information.
                              consent.not_received