tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 11:41 am

PeterO wrote:Can a discussion of the history of C go in another thread please ? Let's leave this one clear to discuss the content of any future tutorials or books.
PeterO
I double that.
And corrections to the current magPi C guide should be PM'ed directly to the author IMO.
- Tim

User avatar
Mortimer
Posts: 923
Joined: Sun Jun 10, 2012 3:57 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 12:30 pm

Can someone fix the "Learn C Essentials" webpage : https://www.raspberrypi.org/magpi/issue ... ials-c-v1/

I don't know about the Google Play link, but the Apple App Store link doesn't go to the eBook but takes you instead to the MagPi Magazine.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

User avatar
karrika
Posts: 1070
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: The MagPi does C :-)

Fri Oct 14, 2016 12:57 pm

I just read through the MagPi C booklet. Well written.

Personally I would have left out the hairy details of pointers. You could have written the code in a way to avoid using pointers and saved that stuff for the last chapter. In my opinion pointers is the most difficult thing to understand in C. Most of the time you don't need to use them. It would have been enough to introduce just &data and data[index]. Save *data for the super duper advanced gurus. Not to even talk about **argv.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23708
Joined: Sat Jul 30, 2011 7:41 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 1:02 pm

karrika wrote:I just read through the MagPi C booklet. Well written.

Personally I would have left out the hairy details of pointers. You could have written the code in a way to avoid using pointers and saved that stuff for the last chapter. In my opinion pointers is the most difficult thing to understand in C. Most of the time you don't need to use them. It would have been enough to introduce just &data and data[index]. Save *data for the super duper advanced gurus. Not to even talk about **argv.
Er, "Most of the time you don't need to use them"? I use them in practically everything I do! Do you never use 'malloc'? But I am almost exclusively embedded, perhaps that makes a difference.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
karrika
Posts: 1070
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: The MagPi does C :-)

Fri Oct 14, 2016 1:15 pm

jamesh wrote: Er, "Most of the time you don't need to use them"? I use them in practically everything I do! Do you never use 'malloc'? But I am almost exclusively embedded, perhaps that makes a difference.
Nope. I cannot remember using dynamic memory allocation for years. Of course you have to learn to deal with memory allocation at some stage. It is a very difficult subject and the main cause of errors. So why start with it at the beginners level?

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 1:38 pm

IMO understanding pointers is essential to C. and OTOH, it's now as it is, so why not accept it as it's been written and published? A discussion about that wouldn't lead us anywhere.
PeterO wrote: Let's leave this one clear to discuss the content of any future tutorials or books.
- Tim

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 2:26 pm

The early treatment of pointers is useful and important. It helps debunk the myth that they are hard to understand and use.
It would be hard to treat later subjects like lists, or GUI widgets without using pointers.

A chapter on more advanced uses of pointers (such situations where pointers to pointers are useful ;) ) would be a good one to start with.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23708
Joined: Sat Jul 30, 2011 7:41 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 2:43 pm

karrika wrote:
jamesh wrote: Er, "Most of the time you don't need to use them"? I use them in practically everything I do! Do you never use 'malloc'? But I am almost exclusively embedded, perhaps that makes a difference.
Nope. I cannot remember using dynamic memory allocation for years. Of course you have to learn to deal with memory allocation at some stage. It is a very difficult subject and the main cause of errors. So why start with it at the beginners level?
malloc is just one example (although how do you get away with not using allocation in C? Even a basic linked list needs allocation), What about passing large data into functions as a pointer? Or returning information from a function by inserting it in to a pointed to results field? Or, how about passing strings of characters to any of the standard string functions?

The Linux kernel uses pointers all the time. And I mean ALL the time.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 3:01 pm

as people might wish to learn programming the PI with C (e.g., instead of Python, and perhaps also not just learning C in general, also for PCs and even domain servers and MCUs), and then using their own C programs on the Pi for proprietary tasks and applications (robots, weather stations, LED cubes, ambient lighting, IoT, home automization, gaming, multimedia), it's perhaps a good advice to focus on how to use GPIO pins, input, output, serial communication and Wifi to achieve a practical goal, not to flog the detailed C functions and syntax until to aversion.
But I'm curious about what the author(s) might think and wish to establish for the next tutorial's step.
(just my2ct)
- Tim

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 3:33 pm

Earlier I said "I think we should also produce a list of "C topics" as well as "application areas" to consider. The two things go together, but learners are going to need some more advanced things beyond the MagPi book before tackling topics like networking or serial comms or GPIO programming.

I think an interesting question to ask is "What other knowledge beyond the MagPi book is needed to get a beginner to the point where they can 'flash a led in C' ?"

PeterO

PS: It might be worth thinking about and producing a set of example programmes, then looking at the additional topics needed to be covered for each one. That way we can start off by writing some code (which we all enjoy) and then thinking about how to cover the required new topics.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ElEscalador
Posts: 680
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Elescalador

Fri Oct 14, 2016 3:43 pm

tito-t wrote: Hopefully there will be some extensions and follow-ups:
C for GPIOs
C for UART, i2c and SPI
C for HATs
C for graphics
C for the Pi Cam
C for audio

8)
This. Please.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

User avatar
karrika
Posts: 1070
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: The MagPi does C :-)

Fri Oct 14, 2016 3:46 pm

jamesh wrote:
karrika wrote:
jamesh wrote: Er, "Most of the time you don't need to use them"? I use them in practically everything I do! Do you never use 'malloc'? But I am almost exclusively embedded, perhaps that makes a difference.
Nope. I cannot remember using dynamic memory allocation for years. Of course you have to learn to deal with memory allocation at some stage. It is a very difficult subject and the main cause of errors. So why start with it at the beginners level?
malloc is just one example (although how do you get away with not using allocation in C? Even a basic linked list needs allocation), What about passing large data into functions as a pointer? Or returning information from a function by inserting it in to a pointed to results field? Or, how about passing strings of characters to any of the standard string functions?

The Linux kernel uses pointers all the time. And I mean ALL the time.
This may be a bit off topic. Pointers are useful and I also use them all the time. This depends a lot of what kind of application you are writing and how you are thinking about the problem.

What I wanted to say is that there is many ways to describe things.

Code: Select all

data
*data or data[]
*data[] or **data or data[][]
&data
&data[]
data.data->data
data->data
**data->data[]
This is confusing for a beginner. It is even confusing for me. Which style you use may also change from one function to the next. Teaching just one variant in the introduction would be enough.

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 3:47 pm

there is not needed much to know for this:
I think an interesting question to ask is "What other knowledge beyond the MagPi book is needed to get a beginner to the point where they can 'flash a led in C' ?"
One would need a breadboard, a pi, 1 led, 1 1kOhm resistor and some jump wires - that's it.
And then explain what's it all about with wiringPi and GPIO control
alternatively, using pigpio instead.
or just bare-metal then, additionally.

https://www.raspberrypi.org/documentati ... nd-raspi2/
Image
Image
https://github.com/WiringPi/WiringPi/bl ... es/blink.c

Code: Select all

/*
 * blink.c:
 *	Standard "blink" program in wiringPi. Blinks an LED connected
 *	to the first GPIO pin.
 *
 * Copyright (c) 2012-2013 Gordon Henderson. <[email protected]>
 ***********************************************************************
 * This file is part of wiringPi:
 *	https://projects.drogon.net/raspberry-pi/wiringpi/
 *
 *    wiringPi is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    wiringPi is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************
 */

#include <stdio.h>
#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define	LED	0

int main (void)
{
  printf ("Raspberry Pi blink\n") ;

  wiringPiSetup () ;
  pinMode (LED, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED, HIGH) ;	// On
    delay (500) ;		// mS
    digitalWrite (LED, LOW) ;	// Off
    delay (500) ;
  }
  return 0 ;
}
- Tim

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 4:32 pm

Ok, so for that example the question becomes "What else is needed to use the wiringPi library ?"

It's a library, so header files and gcc command line options to link th e library.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Fri Oct 14, 2016 4:46 pm

yes, provide practical C programming tools, for application-oriented programming, step by step, just like the new magPi C book does.
in principle, not much different from what is written in the Raspi Python tutorials, but now using C code instead.
Porting the Python code to C and then explaining what it's about, that would be practical learning C by doing, and addtionally then providing background knowledge for C "internals".
There are many books I once knew doing it that way, e.g., for Borland Turbo C, Borland C++ Game programming for Kids, proceeding from simple to sophisticated. Every experienced teacher is supposed to know how to do and to achieve that, and the magPi author showed and proved already how to do it actually.
- Tim

swampdog
Posts: 239
Joined: Fri Dec 04, 2015 11:22 am

Re: The MagPi does C :-)

Sat Oct 15, 2016 1:11 am

PeterO wrote:Can a discussion of the history of C go in another thread please ? Let's leave this one clear to discuss the content of any future tutorials or books.
PeterO
Agreed. Back to the plot. The most obvious complex C item, is strings. Every man & his dog has written "a brilliant string library". The concept does not require much math but much logical thought.

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Sat Oct 15, 2016 8:06 am

here is a typical example for programming issues in C to Pi users,
viewtopic.php?f=33&t=162800&sid=6f603c9 ... 2049373d71

this could be covered by chapter 2 of my list:

C for GPIOs
C for UART, i2c and SPI
C for HATs
C for graphics
C for the Pi Cam
C for audio
- Tim

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: The MagPi does C :-)

Sat Oct 15, 2016 8:57 am

I would say there are more than enough good books on C, but a good introduction on getting things done in GTK+ is very welcome.
Of course it's easy to bundle the MagPi articles on C into a single book and use that as a foundation.

A bit strange that a moderator tries to start a flame war on a worn out issue like bracing styles ;)
Just use a source formatter like astyle to go from one style to another.

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Sat Oct 15, 2016 10:14 am

tito-t wrote:here is a typical example for programming issues in C to Pi users,
viewtopic.php?f=33&t=162800&sid=6f603c9 ... 2049373d71

this could be covered by chapter 2 of my list:

C for GPIOs
C for UART, i2c and SPI
C for HATs
C for graphics
C for the Pi Cam
C for audio
Yes, that's a good thread to use as an example for topics to cover.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Sat Oct 15, 2016 10:20 am

swampdog wrote: The most obvious complex C item, is strings. Every man & his dog has written "a brilliant string library". The concept does not require much math but much logical thought.
My personal favourite "string library" is glib's GString https://developer.gnome.org/glib/stable ... rings.html if only for the simple reason that they grow by themselves :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: The MagPi does C :-)

Sat Oct 15, 2016 4:21 pm

tito-t wrote:there is not needed much to know for this:
I think an interesting question to ask is "What other knowledge beyond the MagPi book is needed to get a beginner to the point where they can 'flash a led in C' ?"
One would need a breadboard, a pi, 1 led, 1 1kOhm resistor and some jump wires - that's it.
And then explain what's it all about with wiringPi and GPIO control
alternatively, using pigpio instead.
or just bare-metal then, additionally.

https://www.raspberrypi.org/documentati ... nd-raspi2/
Image
Image
https://github.com/WiringPi/WiringPi/bl ... es/blink.c

Code: Select all

/*
 * blink.c:
 *	Standard "blink" program in wiringPi. Blinks an LED connected
 *	to the first GPIO pin.
 *
 * Copyright (c) 2012-2013 Gordon Henderson. <[email protected]>
 ***********************************************************************
 * This file is part of wiringPi:
 *	https://projects.drogon.net/raspberry-pi/wiringpi/
 *
 *    wiringPi is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    wiringPi is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************
 */

#include <stdio.h>
#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define	LED	0

int main (void)
{
  printf ("Raspberry Pi blink\n") ;

  wiringPiSetup () ;
  pinMode (LED, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED, HIGH) ;	// On
    delay (500) ;		// mS
    digitalWrite (LED, LOW) ;	// Off
    delay (500) ;
  }
  return 0 ;
}
While I appreciate the link to wiringPi here, and I know this is a thread about C programming, I advocate a more bottom up approach to this sort of problem rather than jumping in and writing code...

When demonstrating this, I start with getting the LED to light using the +3.3v pin and (a) 0v pin. Demonstrate that the LED is a diode that only works one way.

Then move to a GPIO pin, and rather than write a program, use tools already there, so as Raspbian has wiringPi installed with the handy gpio program, then it's simply a matter of typing:

Code: Select all

gpio -g mode 17 out; gpio -g write 17 1
(use -g to make wiringPi use BCM_GPIO pin numbers, -1 will use physical pin numbers - then the pin number would be 11 here, or no -g nor -1 to use wiringPi pin numbers when it would be pin 0)

and when that works, then you can write some code...

Incidentally wiringPi IS NOT HELD ON GITHUB. The official source of wiringPi is at http://wiringpi.com/ The version on github is not maintained by me.

-Gordon
--
Gordons projects: https://projects.drogon.net/

tito-t
Posts: 298
Joined: Thu Jan 07, 2016 5:14 pm

Re: The MagPi does C :-)

Sat Oct 15, 2016 5:55 pm

it was not my intention to c+p a code which was meant to be c+p'ed that way into a C tutorial.
It was just a quickly sketched reply to PeterO's (irrational? hypocritical?) question
PeterO wrote:"What other knowledge beyond the MagPi book is needed to get a beginner to the point where they can 'flash a led in C' ?"
A C tutorial should show the way from the very start and then guide the student along to get to some result, do I really have to emphasize that?
Finally it might end up with something like this code and then the student should have understood, what actually is happening by this code and what is executed then afterwards on the machine.
I underlined that by my next sentences where I wrote
"...explain what's it all about with wiringPi and GPIO control
alternatively, using pigpio instead.
or just bare-metal then, additionally."
- Tim

mob-i-l
Posts: 267
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
Contact: Website Facebook Google+ Twitter YouTube

Re: The MagPi does C :-)

Sun Oct 16, 2016 2:55 pm

I’ve written a Tetris-program for Raspberry Pi that is beyond this book because it uses function pointers and longjmp. https://github.com/mobluse/blockis
It also uses SPI to a MAX7219 8x8 LED matrix.

I think the book should include function pointers and longjmp, because it’s what’s needed to emulate polymorphism and exceptions from C++ in C, respectively.

Longjmp is used to emulate exceptions, but you can do it simpler in special cases than this example: http://www.di.unipi.it/~nids/docs/longj ... catch.html
Have Pi0&1A&1B&1B+&2B&3B&4B w/ Raspbian. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: The MagPi does C :-)

Sun Oct 16, 2016 3:41 pm

mob-i-l wrote:I think the book should include function pointers and longjmp, because it’s what’s needed to emulate polymorphism and exceptions from C++ in C, respectively.
You do know that the book is intended for beginners don't you? I can't see why you would think such topics are suitable for a beginners book.
Why aren't you using C++ if you want C++'s OO and exception handling ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23708
Joined: Sat Jul 30, 2011 7:41 pm

Re: The MagPi does C :-)

Mon Oct 17, 2016 6:24 am

mob-i-l wrote:I’ve written a Tetris-program for Raspberry Pi that is beyond this book because it uses function pointers and longjmp. https://github.com/mobluse/blockis
It also uses SPI to a MAX7219 8x8 LED matrix.

I think the book should include function pointers and longjmp, because it’s what’s needed to emulate polymorphism and exceptions from C++ in C, respectively.

Longjmp is used to emulate exceptions, but you can do it simpler in special cases than this example: http://www.di.unipi.it/~nids/docs/longj ... catch.html
I've been programming in C for 30 years, fairly successfully. Used longjump once that I can remember.

If you want polymorphism and exceptions, use C++?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Return to “C/C++”