Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: 12 position rotary switch > 2 keyboard encoder inputs, simple hardware solution?  (Read 16669 times)

0 Members and 1 Guest are viewing this topic.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
My next bit of tinkering is going to be a 3D printed rotary mod for Sanwa JLF and maybe JLW joysticks.
I'm using a 12 position ALPS rotary switch.

GroovyGameGear has a program for their GPWiz that converts inputs from 3 wires (+ground) to 2 possible gamepad presses which are mapped in MAME to turn left and turn right.
To get the 3 inputs, you wire the 12 tabs on the rotary switch like the attached picture.

I don't have a GPWiz.  I could probably accomplish something useable with an autohotkey script, but official MAME builds don't recognize fake keyboard input.  They recognize fake gamepad input, but I'd have to install fake gamepads and it's already hard enough keeping the current gamepad order straight accross multiple programs without throwing fake gamepads into the mix.

So I'm thinking maybe a hardware solution, but using something cheap like logic gates rather than a microcontroller.

The way it needs to work is:

Lets use x and y for variables (not the keys X and Y).

Last input pulled to ground = x
Currently pulled to ground = y

if x=1 & y=2, then Right (an output to the keyboard encoder pulled to ground)
if x=1 & y=3, then Left
if x=2 & y=3, then Right
if x=2 & y=1, then Left
if x=3 & y=1, then Right
if x=3 & y=2, then Left

It would also be nice if the output were only sent momentarily so that MAME doesn't see a key as being pressed all the time.  Pretty sure I can figure that part out.

My question for you electronics guys is does a specific component come to mind for such an application?

EDIT: ....or should I just buy a teensy board and learn how to program it?
« Last Edit: December 29, 2016, 11:48:58 am by BadMouth »

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
I came across this: http://www.rototron.info/projects/rotary-interface/
A cheaper microcontroller, but still a microcontroller.

I guess if there was a solution with a handful of simple gates, some smart person would have gone that route already.

There are lots of ways to go with this.  I'll probably just go with a sub $10 arduino knock-off.
I bought a full size arduino, another knock off with a ton of sensors, and book months ago.
Maybe my anti-social new years weekend can be spent learning.



PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9503
  • Last login:Today at 03:34:15 am
  • Designated spam hunter
You may also want to look at the KADE source code.

The rotary firmware can be configured as either a one-joystick version or a two-joystick version in Loader.

Source code is at https://github.com/kadevice/KADE.

There is a "getting started" guide at http://kadevice.com/forum/viewtopic.php?f=22&t=413.

Jimmer has been doing some work recently with recompiling KADE firmwares for use with atmel 32u4 boards -- he may have some useful tips.


Scott

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
I was curious as to what you ended up doing as a solution for this. 

If you are still in want of one, I would like to offer helping with programming a small micro-controller such as an Attiny45 that could do what you need, depending on what you are interfacing it to all the discrete parts you should need would be a filter cap on the power line.  I would be happy to donate the chip and time to do it. I'm excited to see how your DIY rotary works out and would be happy to be a part of it.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
I was curious as to what you ended up doing as a solution for this. 

If you are still in want of one, I would like to offer helping with programming a small micro-controller such as an Attiny45 that could do what you need, depending on what you are interfacing it to all the discrete parts you should need would be a filter cap on the power line.  I would be happy to donate the chip and time to do it. I'm excited to see how your DIY rotary works out and would be happy to be a part of it.

I appreciate the offer.  I haven't ended up doing anything yet...still tinkering with the hardware maybe one evening every two weeks.
The rotary sticks aren't going to fit in my own cab because the CP is too shallow.
So this is more just a making it for the enjoyment of working on it thing with no real timeframe and no use for it once it's done.   :lol
I just want it to exist!!!

Would using the Attiny45 get rid of the need for a custom PCB like the older microcontroller I linked to above?
I guess what I'm asking is are premade dev boards available for it that would work?

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Would using the Attiny45 get rid of the need for a custom PCB like the older microcontroller I linked to above?
I guess what I'm asking is are premade dev boards available for it that would work?

Yes and no, essentially it would be performing the same function as the attiny2313 that they used on that board, except that it wouldn't have a board and only be for one joystick so you would have to either use piece of a prototype board or attach the wires directly to the chip or a socket.  Though it also wouldn't be difficult to get a small board printed by oshpark.  The programming of the chip would be done with a standard zif socket programmer.  The advantage of it over a solution like you posted above is the cost per piece, an Artiny45 will set you back $1.25 or less plus a few cents for the cap.  It does require more external hardware to program it, but even that is pretty cheap $2- $5 on ebay. ex http://www.ebay.com/sch/?_nkw=avr%20usbasp.  If it was desired, it would be easy to create a entry at say OSHPark where you could print off 3 circuit boards for about $5.  So it could be as simple as hooking it up dead bug style with wires directly to the chip or print a professional board for less than $10 for 3.

TLDR: it's a chip with a filter cap and you could attach it to the wires in any number of different ways from printing a board to direct.

It just looked like it would be a fun quick project.  I don't get to play around with my electronics as much as I would like to and when I get a chance I don't know what to make...

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
It sounds like a good solution for how I want things to work. 
Lets do it! (or rather you do it and then I'll buy the parts and enjoy the results of your work...)
I'll try to make more time to get the 3D printed parts closer to a final product.

  :cheers:

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Sounds good.  I'll get started experimenting.  At this point the outstanding unknown for me is how long to hold the left/right signal once the switch is turned.  I'll keep you posted on progress.

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9503
  • Last login:Today at 03:34:15 am
  • Designated spam hunter
At this point the outstanding unknown for me is how long to hold the left/right signal once the switch is turned.
I'm not sure how long Jon held the signal for the KADE Rotary firmware, but he used extra cycles for debouncing.

From the main.c file, line 167:
Code: [Select]
//ensure that we debounce the rotary inputs effectively by adding extra cycles


Scott

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Thanks Scott, I'll take a look at what he did for debouncing the rotary switch.

Though I guess I didn't state my current theoretical issue very plainly.  The issue I'm thinking of (and it may not even really be an issue) is that we are designing a secondary encoder that would sit between say an ipac and a rotary switch that would take the input from the 3 wire interface and determine direction then out put it by pulling one of the L/R lines to ground.  My current concern is how long to pull down the L/R lines so that the keyboard/gamepad encoder recognizes it as an input/button press, but doesn't hold it more frames/poll cycles than necessary and counts one rotation twice.

Though this may never be a great solution because of that problem and if it can't be satisfactorily solved, it may just be easier to either figure out how to get the KADE firmware working on a cheap ATMEGA32U4 board, or use a digispark to create the interface board and have it attach as a usb keyboard.

Either way, it should be fun to figure it out and get something to play with.

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9503
  • Last login:Today at 03:34:15 am
  • Designated spam hunter
it may just be easier to either figure out how to get the KADE firmware working on a cheap ATMEGA32U4 board
Well, the easiest way would be to buy a Minimus from MaxRod.   ;D

If my preliminary research is right, and you use the single-joystick P1 or P2 versions of the KADE rotary firmware, you should be able to load the hex firmware on a Pro Micro and run it as-is.

The KADE rotary inputs are terminals B1-B3

Term.|32u2 Pin|32u4 Pin|Pro Micro Board Label
B1         16         10         16
B2         17         11         14
B3         18         28         8

That would allow the Pro Micro to handle the rotary keystrokes and you could use another encoder for the buttons and joy up/down/left/right.

The 2-joystick version uses terminals A8-A10 (P1) and B8-B10 (P2) for rotary.

As you can see here, I have no idea where 4 of those 6 pins would end up without doing some more testing.    :dizzy:


Scott

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
My current concern is how long to pull down the L/R lines so that the keyboard/gamepad encoder recognizes it as an input/button press, but doesn't hold it more frames/poll cycles than necessary and counts one rotation twice.

I could be oversimplifying, but.....as long as a key press.  Browsing google results, 20 milliseconds seems to be a popular number.

I'm not sure what MAME does if you just hold down the button, but I can test in the next few days.
(long time for such a simple thing, but I am a busy with work and other projects...and too lazy at the moment)

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
I could be overthinking it.  Let me know what your tests reveal.

On another note, I finished designing a schematic and circuit board in eagle.  I'll get started on the programming this weekend or early next week.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
Fired up Ikari Warriors and Victory road on MAME v.146 which is the revision my cab is on. MAME isn't picky.  Quick tap, long press, hold forever...the guy only rotates on position then stops.

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Great, that's very helpful.  Once this week is over I'll work on getting a prototype together.

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Ok, finished the first draft of the code.  I haven't tested it yet.  I'll get a circuit put together soon to test it.  Posting it here in case you are interested.

Code: [Select]
#include <avr/io.h>
#include <util/delay.h>

void ioinit(void); //Initializes IO
void Go(void); //Main Program
void Signal(uint8_t direction); //Signals line 0 R, 1 L
uint8_t GetDirection(void); //process curline and lastline returns direction

uint8_t curLine = 0;
uint8_t lastLine = 0;
uint8_t testLine = 0;

int main (void)
{
    ioinit(); //Setup IO pins and defaults

    _delay_ms(100);
    curLine = ((PINB & 0x07) ^ 0x07);
    lastLine = curLine;

    while(1)
    {
Go();
    }
   
    return(0);
}
 
void Go(void)
{
//Code assumes it is hooked up to a rotary switch configured in a 4 wire configuration (1 ground, 3 signal)

testLine = PINB & 0x07;
if (testLine != 0x07) //See if any lines have been pulled low.
{
if ((testLine ^ 0x07) != curLine) //check if line was already low.
{
curLine = (testLine ^ 0x07); //update curLine low with line currently pulled low
Signal(GetDirection());
lastLine = curLine;
}
}
}
uint8_t GetDirection(void)
{
if (lastLine == 0x01)
{
if (curLine == 0x02)
return 0;
else if (curLine == 0x04)
return 1;
}

if (lastLine == 0x02)
{
if (curLine == 0x04)
return 0;
else if (curLine == 0x01)
return 1;
}

if (lastLine == 0x04)
{
if (curLine == 0x01)
return 0;
else if (curLine == 0x02)
return 1;
}
return 2; //something unexpected happened.
}

void Signal(uint8_t direction)
{
if (direction == 0)
PORTB = 0x17; //Pull PB3 low
else if (direction == 1)
PORTB = 0x0f; //Pull PB4 Low

_delay_ms(20);
PORTB = 0x1f; //set PB3 and PB4 back
}


void ioinit (void)
{   
DDRB = 0b00011000;  //set PB3,4 to output
PORTB = 0b00011111; //set internal pullups on PB0-2 and set PB3,4 high
}

<Update code to final version>
« Last Edit: March 03, 2017, 12:56:06 pm by melvinbates »

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
I should be ready to play test next week.  Guess I should have ordered some microprocessors and programmer already.  I should have the other parts on hand.

Thanks!

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Here is the proposed board layout and schematic.  Once I test them and confirm they are working I'll get the boards made and send you one along with a programmer (in case the code doesn't work as intended, I'm still waiting on these from China though... I ordered some along with some diodes I needed).  Turn around to get a board made is generally 2-3 weeks, though I've had it as fast as 1 week.

If you decide not to wait and get your own programmer and such, I'd also be happy to help you get it setup and answer any questions you have.


Basic schematic, with the current code you could use an attiny25/45/85.  though it's a bit tight on the attiny25.


Quick render of the board layout.  It's actually quite small and less than an inch square.
« Last Edit: February 10, 2017, 12:03:49 pm by melvinbates »

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Ok, everything finally arrived and I've assembled and tested the circuit.  I had to make a minor modification to the code, which I've updated above.  It seems to be working exactly like I hoped it would, pulsing the R line when a sequence like 12 is found and the L line when 32.  So I think for the intent of this adventure it's done.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
I received the rotary encoders today.  Wow, they look professional!

I probably won't have time to try them until this weekend, but I am looking forward to it.  (Also looking forward to learning how to use the programmer you sent, as I'm sure I'll come up with other projects once I get a taste).


melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Wow, I'm surprised they got there so fast... I was just about to send you the tracking number...

Useful links for the programmer:

WinAVR This is the IDE I use, it's very simple and lightweight (read low features) but it gets the job done. Also, if I'm not mistaken, it includes AVRDude which does the actual programming of the chip.

Drivers for programmer
AdaFruit USBTiny drivers Drivers for the USBTiny from Adafruit

As for how to hook up the chip to program, there are a bunch of images that can be searched for that can show you how to do that. (easy if you have a bread board, more fun if not).

<updated to include links for programmer stuff>
« Last Edit: March 07, 2017, 01:42:54 pm by melvinbates »

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
Sigh.  Ended up working all weekend and didn't get a chance to solder this up and test. I'll try to find time one night this week.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9243
  • Last login:Today at 07:56:49 am
  • ...
I found a little time this evening to test out one of your boards and it worked flawlessly.  R & L were reversed, but that would be a function of which direction I wired the 1,2,3 inputs.

I didn't get a chance to really play a long game due to quickly throwing together a temporary test setup.  I thought I'd just swap the sanwa jlf wiring harness over from the joystick in my cab to the modified one bolted to the edge of my cp, but they were oriented different so the directions were all screwed up.  Due to the positioning of the potentiometer and arm, I couldn't just rotate the joystick body.

The good news is that it works and the temporary joystick mount demonstrated that I can fit the rotary mods in my cab if I cut a few holes in the bottom of the CP for clearance.

When I get time.  I'll post pics of the test setup in my "cheap 3D printed rotary mod" thread in project announcements.

melvinbates

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 106
  • Last login:April 26, 2023, 11:39:02 am
  • Another day in paradise
Awesome, I'm glad to hear they work for you.  I can't wait to see them actually in place and in action.  It's been a fun little project.