element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Raspberry Pi XBOX 360 RF Module c# Programming Questions ??? please read again!! rewritten.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Answers 1 answer
  • Subscribers 674 subscribers
  • Views 1112 views
  • Users 0 members are here
  • 360
  • xbox
  • controller
  • raspi
  • programming
  • c#
  • rf
Related

Raspberry Pi XBOX 360 RF Module c# Programming Questions ??? please read again!! rewritten.

CrazyFace
CrazyFace over 10 years ago

Hi, and thanks for taking the time to look at this,

I guess after 50+ views before this edit and not even a "your doing it all wrong", I needed to go about this differently, So let me start with, I am not looking for somebody to do all the work for me where is the satisfaction in that, I would just like some advice/direction/guidance and I apologize if my original post did not give that impression. so here goes,  I basically found some code that allows me to sync my xbox 360 wireless controllers to my raspberry pi whilst using a recycled RF module from a xbox 360 as a receiver , this code works great if I only want to sync 1 controller but so far when I needed to sync another I've had to stop the program and restart it, where ideally I want to be able to sync as many controllers as I want in 1 session, So I started to look at the code to see if I could work out how to make it reset automatically, Now this isn't my first attempt at at doing this I tried a few years ago for my pc but failed to even sync a controller and left it alone, until I got my RPi and started again this time being able to sync a controller and use it, but apart from the sync reset issue I decided I wanted to Include a few other things I had acquired in my research into the code, so that's what I'm trying to do some of it seems straight forward in theory but not having any sort of programming background I'm looking for some advice and guidance on how to proceed, I tried to do it alone just reading tutorials and forums for clues and answers but got lost as to which way was best for me to get the results I wanted, and not actually realizing this was all in c# and trying to search for how to do it in c++ hasn't helped me.

The original code can be found here: http://pastebin.com/AiAiWYdh

Q1) Solved - my mistake this does reset itself you just have to give it enough time.

Q2) Relates to Q1 in a way, but this time it will be to add a pin that when the button is pressed, it sends the command to Remotely shuts down controllers, what I want to know is if just adding this as a while loop after the sync process will work or do I need to set up pins specifically for this command to work ? until I can get the pins to reset, I see this loop being useless if I have to sync a controller during use, but will work fine I if sync isn't used with the code the way it is now and my while loop added, am i right in thinking this?

while(1) {
// Wait For Event in power btn
wait_for(pwr_pin, 0);
send_cmd(ctrl_off);
}

obviously I will add these lines of code in where necessary::

#define pwr_pin         0  // => physical pin 11 - bcm pin 17 ==>RF Module pin 5.

unsigned char ctrl_off[10] = {0,0,0,0,0,0,1,0,0,1}; //Shuts Down Controllers.

pinMode(pwr_pin, INPUT);

I guess my real question is will be adding my button really be as easy as it looks?

Q3) Is just a branch off from Q2, I want to be able to issue the command to console telling the RPi to start safe shut down 10secs after the ctrl_off cmd, will adding these to lines into the above while loop work?

                delay(1000);

                std::system("sudo shutdown -h now")

Q4) Now here is where I really over complicated things for myself, whilst using my controller I noticed that the Ring Of Light leds on the RF Module did not illuminate but somehow xboxdrv was issuing the command to light up the correct ROL leds on the controllers, I had the commands to illuminate the leds but no way to automatically identify which controller/s were connected and issue the commands, so after hours of rummaging around files on my RPi and a few tests I realized that xboxdrv was storing info in /etc on a file called rc.local, I became aware that parts of the info changed automatically when more controllers were added but if there were no controllers that line would be left empty, so I got it into my head that variables was the route forward, I knew what info was getting changed so I could create commands based on that info.

This is the first part of the line that it stores in the file which is all I think I need to identify the how many controllers are connected:

xboxdrv --daemon --id 0

my question is what is the best way for me to extract that info into a int, array or string etc,and save it as ctrl_id  and then use in a boolean condition to match it up with these commands:

unsigned char ply1_led[10] = {0,0,1,0,1,0,0,1,0,0}; //Illuminates RF Module Controller 1 LED ==> Lower Left

unsigned char ply2_led[10] = {0,0,1,0,1,0,0,0,0,1}; //Illuminates RF Module Controller 2 LED ==> Upper Left

unsigned char ply3_led[10] = {0,0,1,0,1,0,0,0,1,0}; //Illuminates RF Module Controller 3 LED ==> Upper Right

unsigned char ply4_led[10] = {0,0,1,0,1,0,1,0,0,0}; //Illuminates RF Module Controller 4 LED ==> Lower Right

unsigned char ctrl_err[10} = {0,0,1,1,0,1,0,0,0,1}; //Illuminates RF Module 4 Flashing RED LED'S ==> No Controller Warning.

which would be triggered by these these:

ctrl_one = {xboxdrv --daemon --id 0 };

ctrl_two = {xboxdrv --daemon --id 1 };

ctrl_three = {xboxdrv --daemon --id 2 };

ctrl_four = {xboxdrv --daemon --id 3 };

or something similar.

in this:

                // check the boolean condition

                if( ctlr_id == ctrl_one )

                {

                // if condition is true

                send_cmd(ply1_cmd);

                }

                else if( ctrl_id == ctrl_two )

                {

                // if else if condition is true

                send_cmd(ply1_cmd,ply2_cmd);

                }

                else if( ctlr_id == ctrl_three )

                {

                // if else if condition is true

                send_cmd(ply1_cmd,ply2_cmd,ply3_cmd);

                }

                else if( ctrl_id == ctrl_four )

                {

                // if else if condition is true

                send_cmd(ply1_cmd,ply2_cmd,ply3_cmd,ply4_cmd);

                }

                else

                {

                // if none of the conditions is true

               send_cmd(ctrl_err);

                }

And if possible how would I go about getting it to repeat this process regularly for changes so it can adjust accordingly, some advice in this area would be greatly appreciated, not even sure I'm heading in the right direction, or if it can be done, all seemed so easy in my head but the theory has me stumped.

Many thanks for taking the time to read this, especially if you read it the first time.

Mark.

 

Message was edited by: Mark Nicholson, to reword and better explain my questions, I rewrote my whole post after getting views but no reply and realizing my questions needed to be asked in a better way.

  • Sign in to reply
  • Cancel
  • CrazyFace
    0 CrazyFace over 10 years ago

    Questions 1, 2 and 3 I've managed to answer myself.

    Q1) Irrelevant, The sync button and pins do reset it just needs some time to do it.

    Q2) No it isn't as easy as it looked, but its not out of question, The command works great, shuts the controller down instantly but it cant share a wait_for function with the sync_pin, looks like I will have to add a wait_until to accommodate the command.

    Q3) std::system("sudo shutdown -h now") wont work, but system ("sudo shutdown -h now") does work.

     

    Thanks to all those who have read the post and I look forward to hopefully getting some help with my last question which still has me wondering which way to go about achieving what I set out to do, although I think I now have a fair idea of what to do I'm not confident enough to just do it until I get some advice

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 10 years ago

    Your questions [and title] were so specific (xbox 360, RF module and some ring of lights? controller) that anyone without these items would have a hard time following what you meant.

    There may be 50+ views, but probably none of the 50 viewers have this combination of products, so it helps to make the question(s) easier to follow for those who won't have your

    specific scenario/hardware.

    .

    I don't have an xbox or RF module or ring of light controller, and I've read Q4 multiple times and I'm still unsure what you mean.

     

    I *think* (not sure) that your Question 4 is saying, that you have a file which contains some content, and you wish your program (which is written in C or C++) to place that content (or a part of it) into a variable.

     

    That being the case, you can achieve your aim using the file I/O tutorial here. It will allow you to read a line into a string. Then, you can perform manipulation using the string library and perform

    a string compare (strcmp) for your condition checks.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • CrazyFace
    0 CrazyFace over 10 years ago

    Thanks for your reply Shabaz, I understand the title covers way too much but I really wanted people to understand it wasn't just about writing a program to do a few sums or say hello, which most noobs like myself would be asking about, and this certainly isn't one of them questions, thanks for the link, I will have a look at that later, although I'm fairly sure it's one of the many I've looked at over the last few days, I'm not sure how to explain my last question, im basically asking for advice on what would be the best way to extract so much data off a certain line e.g, the first 24 characters inc whitespace of line 20 in rc.local identifies how many controllers are connected, and I want use that data as ctrl_id to compare with the info stored in static variables(if that's what they are called) in the boolean condition I posted above, and when a match is made it sends the relevant command associated with that string of data and lights up the relevant amount of led's on the ring of light. but if no controllers are connected it sends a error warning, and I want it to check the file regularly for changes so it can adjust the number of leds accordingly, hope that explains it a bit better.

     

    While i'm here might as well give a little update for those who may be interested in my progress, well after throwing an all nighter, editing and re-editing and more editing, I've finally got something to show for all the hours I've spent researching, The sync problem was all sorted and would allow me to sync 4 controllers without having to keep starting the program, but this didn't last long and I will probably have a question or 2 further along.

    I managed to successfully implement the power button which works fine but at the cost of being able to sync again, it now seems to freeze up after first sync, but i'm not worried about that right now, so I can now sync a controller on 1 button press, I can shut off my controllers remotely saving my battery power, then turn off all illuminated leds on the RF Module and start a delayed safe shutdown of the raspberry pi by pressing the power button on the RF Module, which as left me over the moon  with my progress, but here's the cherry on top, and I haven't a clue how I managed to do it, but I can also turn my Raspberry Pi back on again without having to touch it, all I have to do is Press the guide button in the center of the controller and it turns both the RPi and the controller on, so I'm feeling rather pleased with myself this morning, to of achieved so much so quickly, with my lack of knowledge in this area it kind of makes me glad nobody answered my questions straight away, feeling rather proud with what I've done on my own. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 10 years ago in reply to CrazyFace

    Hi Mark,

     

    I think your Q4 does reduce down to how to read a line in a file, and how to perform string manipulation and string comparison.

     

    File I/O is covered in the link in the earlier response. String manipulation can be done using the C string library, using the strncpy

    function to copy n characters from one string to another (e.g. your first 24), or strncpy or strcpy to do string comparisons (or if you've got C++ then

    there is a useful substr method).

     

    References:

    strcmp: http://www.programmingsimplified.com/c-program-compare-two-strings

    strncmp: http://www.tutorialspoint.com/ansi_c/c_strncmp.htm

    strncpy: http://www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm

    substr: http://www.cplusplus.com/reference/string/string/substr/

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • CrazyFace
    0 CrazyFace over 10 years ago in reply to shabaz

    Hi Shabaz, Thanks for those links they did help a little bit, in that I know how to do all the things individually and print results to screen, but I just cant work out how to put it all together and make it work, I've spent 2 full days, reading tutorial after tutorial but not of them actually show me how to put it all together, I've fried my brain trying to work this out, if at all possible and its not too much trouble could someone maybe do a example script to help me figure out how to structure it, I need to open a file, skip to line 20, take the first 24 characters from that line, save them as a string called ctrl_id, Then take that string into a boolean condition or a loop to be compared to 4 named strings in the post above,(which i'm not even sure how to define) to either send the command associated with the string it is equal to or if no line is found it sends another command that will signal an error.

    I forgot to mention before that when this program is finished it will be set to start automatically on power up and run in the background with no user interaction other than button presses to power on/off and sync controllers, all errors get reported to the user with commands that illuminate the XBOX360 RF Modules ROL LED's to signal errors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube