I have been working to create a GUI for a custom made formula car for a club I am in. I have the GUI built and compiled in a GTK program that is written in C along with functions that are intended to get the sensor data. We are using a CAN network and we have purchased and implemented the PI CAN 2 shield and are successfully capturing CAN frames with all needed data. The part that I am unsure about is how to get that data from the SPI connection that the shield uses to communicate with the pi and into my C program to be used. Below is my code written in C and an image of the CAN frames the PI is receiving. I think my problem is trying to use the scanf function to parse through the frames but I just don't really know.
Thanks in advance
Code: Select all
#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "CAN.h"
#define FREQUENCY 500
static void destroy (GtkWidget *widget, gpointer data);
static gboolean update_text (GtkWidget *label);
long findRPM() {
char device[5];
char filter[10];
char zeros[4];
char random[3];
char rpm[3];
char TPS[3];
char fuelOpen[3];
char igAngle[3];
short done = 0;
while(done == 0){
scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, rpm, TPS, fuelOpen, igAngle);
if(strcmp(filter, "0CFFF048")){
long tpsNumber = strtol(TPS, NULL, 16);
return tpsNumber;
printf("%s",TPS);
done = 1;
}
}
}
long findCoolantTemp() {
char device[5];
char filter[10];
char zeros[4];
char random[3];
char batt[3];
char air[3];
char coolant[3];
char type[2];
short done = 0;
while(done == 0){
scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, batt, air, coolant, type);
if(strcmp(filter, "0CFFF548")){
long cool = strtol(coolant, NULL, 16);
return cool;
printf("%s",cool);
done = 1;
}
}
}
long findGear() {
char device[5];
char filter[10];
char zeros[4];
char random[3];
char batt[3];
char air[3];
char coolant[3];
char type[2];
short done = 0;
while(done == 0){
scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, batt, air, coolant, type);
if(strcmp(filter, "0CFFF548")){
long cool = strtol(coolant, NULL, 16);
return cool;
printf("%s",cool);
done = 1;
}
}
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Cyclone Racing");
gtk_window_set_default_size (GTK_WINDOW (window), 800, 480);
g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
label = gtk_label_new ("Lets Go Racing!");
gtk_container_add (GTK_CONTAINER (window), label);
g_timeout_add (1000/FREQUENCY, (GSourceFunc)update_text, label);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
static gboolean
update_text (GtkWidget *label)
{
gchar *text;
static int i = 0;
/* A prettier solution would proably be to remove the timeout when
* the window is closed */
if (GTK_IS_LABEL(label))
{
text = g_strdup_printf ("%d %s\n%d %s\n%s", findRPM(),"RPM",findCoolantTemp(),"F","Gear");
gtk_label_set_text (GTK_LABEL(label), text);
g_free (text);
return TRUE;
}
/* Returning FALSE removes the timeout from the glib main loop */
else
return FALSE;
}
static void
destroy (GtkWidget *widget, gpointer data)
{
gtk_main_quit [img][/img]();
}