Monday, October 19, 2015

On Functions


Functions in C

Up to this point, we have described how to use functions, but not how to code them.  It turns out that in C, it’s relatively straightforward to code your own function.  Every function begins with a statement that looks very much like a function prototype.  However, while a function prototype tells the compiler what arguments the function takes and what type of value it returns, the function definition is always followed by the body of the function – the series of variable definitions and statements that do the work of the function.

The following C code implements the function average_ints(), which averages three values of type int and returns their average as a double.

/*Function to average 3 integers */

double average_ints(int i, int j, int k)
 {
double average;
average = (i+j+k)/3.0;
return (average);
}

A few key ideas are illustrated in this example.  Note that the first line of the function defines the type of the value the function returns, in this case, a double.  Like a function prototype, it also defines the types of the arguments.  However, in this case the arguments are given variable names.  These names can then be used in the body of the function.  If you think about what happens when the function average_ints() is invoked, the copies of the arguments can be thought of as being assigned to the variables i, j, and k.   The variables used in this function like i, j, k and average are internal variables.  They are internal variables since they are valid only within the function and are not global.

One of the key features of functions is the use of the return statement.  The return statement allows you to pass the result of a function back to the calling function:  In this case, the main() function. 

/*Another version of average_ints() */

double average_ints(int i, int j, int k)
{
             return ( (i+j+k)/3.0 );
}

So, any C function can be invoked or called by another.  The invoking function may pass information tot he invoked function and the invoked function may return information to its invoker.


A function’s header consists of:
·      Data type returned, or the keyword void if the function does not return a value
·      Function’s name
·      In parentheses, a list of parameters and their types separated by commas, or the key word void if the function has no parameters

Specifying the data type is optional:  If omitted, it defaults to int.  However, it is good practice to always specify the data type.

The special key word void is used to indicate that a function doesn’t return a value or that it doesn’t have any arguments.  For example, the function prototype

            void no_result(double);

declares that no_result() does not return a value.  Similarly, the function prototype

            int no_arguments(void);

indicates that the function returns an integer value but does not have any arguments.

Saturday, October 3, 2015

VNC with the Pi

Step 1: get packages
You’ll need TigerVNC, NetworkManager, network-manager-applet and you’ll need to run a system update and reinstall polkit. It sounds like a lot, but it can all be done in just a few commands:
Reply y when prompted.
pacman –Syu
pacman –S systemd polkit
Reboot (your pi will likely sit there and do nothing for 30 seconds to a minute before it does anything. Reinstalling polkit should fix this once it boots back up)
pacman –S tigervnc networkmanager network-manager-applet

Step 2: configure VNC
As user ME…
vncserver
Enter a password (don’t forget it)
Enter a view only password if you want
vncserver –kill :1
cd ~/.vnc
gvim xstartup

comment out or delete all the if statement lines, starting with "if [ $OS = ‘Linux’ ]” and ending with the “fi" before "[ -r $Home/.Xresources ]"
delete everything after  "xsetroot - solid grey”, then add:

vncconfig -iconic & 
xrdb -merge ~/.Xresources 
startxfce4 &
Save and close

cd ..
chmod 700 ~/.vnc

Step3: configure the system service.
As Root…
cd /usr/lib/systemd/system
gvim vncserver.service
Here we just need to change 3 lines. NOTE: The geometry flag is just the resolution of the monitor that VNC creates, and can be set to whatever you want (1920x1080?).
change "User=" to 
User=me
change: “execstart" and “execstop" to 
ExecStart=/usr/bin/vncserver %i -geometry 1024x768
ExecStop=/usr/bin/vncserver -kill %i -geometry 1024x768
SAVE AS vncserver@:1.service then close

Step4: disable old network service and enable network manager and VNC to run when the Pi starts
As Root…
systemctl disable systemd.networkd
systemctl enable NetworkManager
systemctl enable vncserver@:1.service
reboot

Step5: Wireless and Static IP
See videos. Ignore talking….
The “install network manager and get on wifi” is only relevant now after 2:30

Step6: Connecting via VNC: 
For Macs, they can use the built in screensharing app (it’s not in the applications folder, the easiest way to find it is to search for it in spotlight [command + spacebar on most Macs]). If you don’t want to use that, you can always download another app like ChickenOfTheVNC
For Windows, you will have to download a vnc client app light Tightvnc, RealVNC, or UVNC.

To connect, enter the IP that you chose, then a :, then 5900+the number you chose for the VNC service. Using the instructions above: that would be192.168.0.100:5901. You should be prompted for a the password you created the first time you ran vncserver.

Wednesday, September 30, 2015

VNC working on my office iMac

VNC up and running for Raspberry Pi on my work iMac. Thanks Cody, our IT guru!