An Introduction To Gui Raspberry Pi-Free PDF

  • Date:24 Oct 2020
  • Views:3
  • Downloads:0
  • Pages:156
  • Size:8.99 MB

Share Pdf : An Introduction To Gui Raspberry Pi

Download and Preview : An Introduction To Gui Raspberry Pi

Report CopyRight/DMCA Form For : An Introduction To Gui Raspberry Pi


2 AN INTRODUCTION TO C AND GUI PROGRAMMING,AN INTRODUCTION TO C AND GUI PROGRAMMING. First published in 2019 by Raspberry Pi Trading Ltd Maurice Wilkes Building. St John s Innovation Park Cowley Road Cambridge CB4 0DS. Publishing Director Russell Barnes Editor Phil King. Author Simon Long Design Critical Media,CEO Eben Upton. ISBN 978 1 912047 65 9, The publisher and contributors accept no responsibility in respect of any omissions. or errors relating to goods products or services referred to or advertised in this book. Except where otherwise noted the content of this book is licensed under a Creative. Commons Attribution NonCommercial ShareAlike 3 0 Unported. CC BY NC SA 3 0, The GTK logo is copyright The GNOME Foundation and licensed under LGPL v2 1. 4 AN INTRODUCTION TO C AND GUI PROGRAMMING,AN INTRODUCTION TO C AND GUI PROGRAMMING.
Welcome to,An Introduction to C,GUI Programming, he C programming language was invented in the early 1970s and since then has. become one of the most popular and widely used general purpose languages C can. be used to create simple command line programs or embedded code to operate the. tiny microcontrollers in toasters and watches At the other extreme it can be used to create. rich graphical desktop applications in fact most of Linux and Raspbian itself is written. in it It can give you control over the smallest details of how a processor operates but is still. simple to learn and read The first part of this book is an introduction to programming in C for. absolute beginners the second part shows how to use C to create desktop applications for. Raspbian using the GTK toolkit You don t need any programming experience and a Raspberry. Pi running Raspbian is all you need to get started. About the Author,S imon Long is an engineer working. for Raspberry Pi He is responsible,for the Raspberry Pi Desktop and its. associated applications Before joining,Raspberry Pi he worked for Broadcom. where he first met Eben Upton and before,that spent ten years working as a software.
engineer and user interface designer for,a major consultancy firm In his spare. time he enjoys solving those really hard,crosswords without any black squares. 6 AN INTRODUCTION TO C AND GUI PROGRAMMING,AN INTRODUCTION TO C AND GUI PROGRAMMING. Chapter 1 Getting started 010,Learn how to use C to program the Raspberry Pi. Chapter 2 Variables and arithmetic 014,Create variables and do maths.
Chapter 3 Conditions and comparisons 019,Control the flow of your C programs. Chapter 4 More advanced flow control 024,For loops and case statements. Chapter 5 Pointers 030,Variables have addresses too. Chapter 6 Functions 035,Split your code into bite sized chunks. Chapter 7 Arrays and strings 041,Handle lists of values and letters.
Chapter 8 The string l ibrary 046,Simplify common operations on strings. Chapter 9 User Input 052,Reading and interpreting user input. Chapter 10 File input and output 058,Learn to read from and write to files. Chapter 11 More about types and variables 063,Type definitions enumerations and more. Chapter 12 Header files and the preprocessor 068,Splitting code up into multiple files.
Chapter 13 Introduction to GTK 074,Get ready to start creating GUIs. Chapter 14 Your first GTK program 077,Start coding in C with the GTK library. Chapter 15 Buttons 081, Make your window more interesting by adding a button. Chapter 16 Labels and layout 085, Use a box widget to add a text label to your window. Chapter 17 More advanced layout 091, Expand your window and position and resize buttons automatically.
Chapter 18 GUI user input 097,Enable users to enter text and select options. Chapter 19 Combo boxes and list stores 103, Create combo boxes for user input and associate list stores with them. Chapter 20 Tree views 109,Use the GtkTreeView widget to display information. Chapter 21 Menus 115,Create menu bars with drop down menus. Chapter 22 Dialogs 120,Give users information and ask them questions.
8 AN INTRODUCTION TO C AND GUI PROGRAMMING,AN INTRODUCTION TO C AND GUI PROGRAMMING. Chapter 23 Built in dialogs 123,GTK contains some ready made dialogs. Chapter 24 Customising widgets 131,Change the properties of widgets. Chapter 25 Glade 135,Create window layouts for applications. Chapter 26 C quick reference 144,Cheat sheets and code examples.
Getting started, C is one of the most widely used programming languages. learn how to use it to program the Raspberry Pi,What s so great about C. C is a very versatile and widely used programming language It has been used to write. pretty much everything from low level routines to control the hardware in embedded. microcontrollers to complete operating systems like Linux with graphical user interfaces In. spite of this huge flexibility it is also relatively simple the language only has about 20 or so. keywords but there are huge libraries of additional functions that you can call on when you. need them In the first part of this book we are going to concentrate on learning about the. keywords with a few of the more commonly used library functions the second part of the. book shows how to use the GTK library to make it easy to write graphical interfaces in C. Many of the languages that you may have seen such as Python are what are called. interpreted languages This means that the code you write is run directly each line of code is. read in and interpreted as you run it C is different it s a compiled language This means that. the code you write known as the source code is never run directly The source code is passed. through a program called a compiler which converts it into a machine readable version called. an executable or a binary you then run the resulting executable. This may seem complex but it has a few big advantages First it means that you don t need. to have a copy of C itself on every computer you want to run your program on once compiled. the executable is standalone and self contained Second the compilation process will find. a lot of errors before you even run the program but it won t usually find all of them Most. importantly the compilation process means that the time consuming translation of human. readable code into machine readable instructions has already happened which means that. compiled code generally runs many times faster than interpreted code would. CHOOSE YOUR EDITOR, You can use whatever editor you like to enter code as long as it saves it as plain text The. Geany editor included in Raspbian is a good choice but you can also use Leafpad nano or. any others that you prefer,10 AN INTRODUCTION TO C AND GUI PROGRAMMING. AN INTRODUCTION TO C AND GUI PROGRAMMING,WHITESPACE DOESN T MATTER.
Unlike Python whitespace has no significance in C you can put spaces tabs and new lines. anywhere you like in a C program to make it readable. Hello world your first C program, With all that out of the way which has hopefully made you think that C might be worth. learning let s have a look at the first program everyone writes in any language the one that. prints Hello World on the screen Incidentally the tradition of writing a Hello World program. was first introduced with the original documentation describing C itself Just think no C no. Hello World,include stdio h,void main void,A print statement. printf Hello world n, Hopefully not too frightening Let s look at it line by line. include stdio h, This is known as a hash include As mentioned above the C language has a large library of. functions that can be included and we need to use one of them in this program the formatted. print command printf This is part of the standard input output library or stdio for short. So what this line does is to warn the compiler that the program needs the stdio library to be. included as part of the compile process,void main void.
C is a function based language every program is made up of a number of functions. Each function takes zero or more arguments and returns a single value A function. definition consists of a specification of what the function returns in this case a void a. function name in this case main and a list of arguments enclosed in round brackets again. Every C program has to include a function called main when you run the compiled program. the main function is the first thing that executes. Chapter 1 Getting started 11, The word void is called a type specifier a void is a special type which means no value. required We ll look more at types in the next chapter. So this line defines the main function for this program it states that the main function. takes no arguments and returns no value, The code which makes up the function itself is enclosed between the two curly brackets. that follow the function definition,A print statement. First we have a comment telling us what s going on Comments in C start with the symbol. and end with anything between those two symbols is ignored by the compiler. The code itself is just one line,printf Hello world n. This is a call to the printf print formatted function from the stdio library In this case. it takes a single argument which is a text string enclosed within double quotes As mentioned. above function arguments are enclosed in round brackets. Note that the line ends with a semicolon All statements in C must finish with a semicolon. this tells the compiler that this is the end of a statement One of the most common beginner. mistakes in C is to forget a semicolon somewhere, What about the string itself The Hello World bit is straightforward enough but what.
about that n at the end Remember this function is called print formatted Well the n is a. bit of formatting it s the symbol for a newline character So this line will print the string Hello. World followed by a new line,CHECK YOUR BRACKETS, Unlike whitespace punctuation is very important in C make sure you don t use a curly. bracket where a round one is needed or vice versa,Compiling your program. Let s compile and run this Raspbian includes a C compiler called gcc so there s nothing to. install just start up Raspbian on your Pi and you re ready to go Use your favourite text editor. to create a file called hello c copy the program above into it and save it Then from a terminal. go into the directory where you saved hello c and enter. gcc o myprog hello c,12 AN INTRODUCTION TO C AND GUI PROGRAMMING. AN INTRODUCTION TO C AND GUI PROGRAMMING, This calls the gcc C compiler with the option o myprog which tells it to create an. executable output file called myprog and to use hello c as the input source code. If you entered your C code correctly did you make sure the semicolon was there this. should take a second or so and then return you to the command line There should now be a. file in the current directory called myprog try running it by typing. Et voil You should now have,Hello World,written in the terminal.
5 You interact with both the C compiler and your compiled C programs from the command. line you can either do this in a terminal window in the desktop or by booting your. Raspberry Pi straight to the command line,RUNNING YOUR PROGRAM. You need to tell Linux that the program you want to run is in the current directory so don t. forget the before myprog or it won t know where to look. That s your first C program written compiled and run In the next chapter we ll start using C. for something a bit more useful,Chapter 1 Getting started 13. and arithmetic, Doing some real work in C creating variables and performing. mathematical operations on them, n some languages you can create variables as you go along and put whatever data you. want into them C isn t like that to use a variable in C you need to have created it first. and at the time you create it you have to set what type of value it s going to store By. doing this a block of memory of the correct size can be allocated by the compiler to hold the. variable This process of creating a variable is known as declaration. There are several fundamental data types in C but we ll start by looking at one of the most. commonly used the int type used to store an integer value. include stdio h,void main void,printf The sum of adding d and d is d n a b c.
The top three lines inside the main function here are declarations They tell the compiler. that we would like to use variables called a b and c respectively and that each one is of type. int i e an integer, In the second line we see an example of an initialisation at the same time as a declaration. this stores an initial value of 3 in the variable b Note that the values of a and c at this point are. 14 AN INTRODUCTION TO C AND GUI PROGRAMMING,AN INTRODUCTION TO C AND GUI PROGRAMMING. undefined you might assume that a variable which hasn t had a value stored in it is always 0 but. that isn t the case in C Before reading the value from a variable or using it in a calculation you. must store a value in it reading a variable before initialising it is a common error in C. The next two lines do some actual work with the variables we have declared. This stores a value of 2 in the variable a which will now have this value until it s changed. The reason a is called a variable is that it can vary you can change its value as often as you. like but only to another integer The value of a variable can change but its type is fixed when it. AN INTRODUCTION TO C AND GUI PROGRAMMING 11 Hello world your first C program With all that out of the way which has hopefully made you think that C might be worth learning let s have a look at the first program everyone writes in any language the one that prints Hello World on the screen Incidentally the tradition of

Related Books