Language - C SBC Linux GCC: Difference between revisions

From Phidgets Support
(Redirected page to Language - C)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NoTitle}}
#REDIRECT [[Language - C]]
{{Language_-_C_Dev_Environment_Table}}
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - C'''
 
'''SBC Linux with GCC'''</font>
 
Welcome to using Phidgets with C! By using C, you will have access to the complete Phidget22 API, including events.
 
GCC is a compiler system for originally written for GNU, and is the standard compiler on unix-like operating systems like Linux. It allows compilation of C programs from the command line.
|{{TOC limit|2}}
|}
==Getting Started With the Phidget SBC==
Welcome to using the Phidget SBC. If you haven't already, check out the [[SBC3003 User Guide#Getting Started | user guide]] in order to set up the following:
* Networking
* Administrator password
 
 
If you are ready to go, the first step will be deciding how you will use the SBC:
*Use a more powerful external computer to develop your code, and then simply copy the files to the SBC.
*Use the SBC like any other Linux computer, simply connect a monitor and a keyboard and begin your development.
 
 
This guide will cover development using an external machine. For development using the SBC itself, check the [[Language_-_C_Linux_GCC|GCC (Linux)]] page.
 
To begin, this video will help you get started:
<center>{{#ev:youtube|8vv8kx4743c}}</center>
 
 
===Developing with an External Computer===
{{SBC_Developing_with_an_External_Computer}}
 
===Installing Packages for Development===
At this point you have connected to the SBC through one or more these three options:
*SBC Web Interface
*SSH
*Directly via monitor and keyboard
 
Now that you are connected, you may want to start developing on/for the SBC. Before you do this, you need to install some packages.
 
The simplest way to install C support on the SBC is via the install buttons on located on the SBC Web Interface (System->Packages). Check ''Include full Debian Package Repository'' before installing.
 
 
[[File:phidgetsbc_installpackages.PNG|link=|alt=|center]]
 
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
 
 
==Use Our Examples==
'''''For simplicity, if you have not used Phidgets before, we recommend trying them out directly on an external development machine like a desktop computer. For C development, check the [[Language_-_C_Windows_GCC|GCC (Windows)]], [[Language_-_C_macOS_GCC|GCC (MacOS)]], or [[Language_-_C_Linux_GCC|GCC (Linux)]] page, depending on your operating system.'''''
 
One of the best ways to start programming with Phidgets is to use our example code as a guide.
 
Select an example that will work with your Phidget:
*{{SampleCode|C|C Examples}}
 
When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to compile and run them.
 
Follow these steps to get our examples running on an SBC:
 
1. Download the example files onto the development machine.
 
2. Using the SBC Web Interface, create a new project:
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
3. Transfer all the example files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP].
 
4. Use [[#SSH|SSH]] to access the SBC terminal
 
5. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
6. Compile the example by entering the following command in the terminal:
 
<syntaxhighlight lang='bash'>
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -I../Common -lphidget22
</syntaxhighlight>
 
7. You can now run the program with the command:
 
<syntaxhighlight lang='bash'>
./example
</syntaxhighlight>
 
Success! The example is running on your SBC. If you aren't interested in developing directly on the Phidget SBC, jump ahead to [[#Running a program automatically|running a program automatically]].
 
{{Edit_the_Examples}}
 
==Editing the Examples==
{{Language_-_C_Editing_The_Examples}}
 
==Write Code==
{{Language_-_C_Write_Code}}
 
==Setting up a New Project==
When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget C library.
 
When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to compile and run them.
 
To include the Phidget C library, add the following line to your code:
<syntaxhighlight lang='C'>
#include <phidget22.h>
</syntaxhighlight>
 
Once your code is written, follow these steps to get your program running on the SBC:
 
1. Using the SBC Web Interface, create a new project:
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
2. Transfer all the project files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP].
 
3. Use [[#SSH|SSH]] to access the SBC terminal
 
4. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
5. Compile the example by entering the following command in the terminal, replacing "example" with the name of your C file:
 
<syntaxhighlight lang='bash'>
gcc example.c -o example -lphidget22
</syntaxhighlight>
 
6. You can now run the program with the command:
 
<syntaxhighlight lang='bash'>
./example
</syntaxhighlight>
 
Success! The program is running on your SBC. Next, you may want to learn about [[#Running a program automatically|running a program automatically]].
 
==Running a Program Automatically==
After testing your program, you will likely want it to run on boot, or on a schedule, without your input.
 
{{SBC_Running_A_Program_Automatically}}
 
{{Language_-_C_Further_Reading}}

Latest revision as of 16:35, 15 March 2021

Redirect to: