Need Help With Writing C Code to Write to GPIOs


4 posts
by bass0324 » Fri Nov 02, 2012 9:11 pm
So let me start off by saying I am new to compiling in C, but I have programmed in C before. I am looking to interface my Raspberry Pi with a LED Matrix display I created for use with a an Arduino. I have a full code for the Arduino (with much help from a friend) that works great with the matrix, but I would really like to control it from my Raspberry Pi. I have installed both wiringPi and BCM2835 C tools on my Raspberry Pi and have ported my Arduino code into both, which can be seen here (https://github.com/bass0324/LED_MATRIX). Now in my Arduino code, I was able to use the timer1 interrupt such that my drawMap() command can occur in exact timed intervals. Is there a way this can also be done with the Raspberry Pi? Also, for those who have C experience and have taken a look at my code, is there anything that I can do better (i.e. seperate my functions from the main file into another one?) and also how will I go about compiling this code and adding it to my pi to act as I want it to? Thanks for the help.
Posts: 12
Joined: Fri Aug 31, 2012 2:32 pm
by bgirardot » Sat Nov 03, 2012 2:15 am
I compiled my first C program 2 days ago, so take it for what it is worth:

gcc -o filename yoursource.c

That will create a compiled version of your source code named 'filename'

That will get you started. It isn't going to compile the first time through, but you should be able to read the warnings and errors and get it working.

One error I see is line 92: return 0

no ending semi-colon.

Hope that helps a bit.
Posts: 517
Joined: Wed Oct 10, 2012 6:20 am
Location: Switzerland
by gordon@drogon.net » Sat Nov 03, 2012 1:36 pm
bass0324 wrote:So let me start off by saying I am new to compiling in C, but I have programmed in C before. I am looking to interface my Raspberry Pi with a LED Matrix display I created for use with a an Arduino. I have a full code for the Arduino (with much help from a friend) that works great with the matrix, but I would really like to control it from my Raspberry Pi. I have installed both wiringPi and BCM2835 C tools on my Raspberry Pi and have ported my Arduino code into both, which can be seen here (https://github.com/bass0324/LED_MATRIX). Now in my Arduino code, I was able to use the timer1 interrupt such that my drawMap() command can occur in exact timed intervals. Is there a way this can also be done with the Raspberry Pi? Also, for those who have C experience and have taken a look at my code, is there anything that I can do better (i.e. seperate my functions from the main file into another one?) and also how will I go about compiling this code and adding it to my pi to act as I want it to? Thanks for the help.


What I think you're looking for is 'threads'. Specifically Posix threads which are supported by Linux, and I have included some simple helpers to make thing easy in wiringPi too.

So what you'd do is write your drawMap() function and inside it run it as a continuous loop. Inside the loop, you need to work out how long the loop takes, subtract this from your desired update time, then sleep for that difference.

But if you call that, it'll loop forever, and never return, so you can start the function not by calling it, but by creating a thread. Then it will run concurrently with your "main" program. It will share all global variables too, so you can use globals to pass data to/from it.

Have a look at my 'wfi.c' program in wiringPi/examples - it will show you how to start a thread using my simplified approach - but for more complicated things, you might need to start reading the man pages...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1507
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by bass0324 » Sat Nov 03, 2012 3:20 pm
Thank you for your help, I will look into that, but it seems to look very promising with what I am trying to accomplish and I will start reading up on those topics. There are two other things that I would like to accomplish as well and if you can point me to how to do it that would be great! First, I would like to be able to have multiple modes, and be able to switch between them while the program is running. Second is, I would like to be able to get the time and temp from the raspberry pi (temp as internal temp of pi) and store them as variables in my program. Is there any easy way to do this? I looked into popen but that seems like a very confusing way to just get a few numbers but if that is all I can do, I will make it work.
Posts: 12
Joined: Fri Aug 31, 2012 2:32 pm