Help with writting code in the Imp's platform

Hi I am new to using the imp and all. I see that the code resemble C/C++ so I was wondering if I need to add libraries like for example: #include <stdaio.h> or #include <spi.h> such like this, and also how do you initialize a variable in the code because i have written like: int x,y,z; or char values [10]; or char DATAX0=0X32; or something like that, does it works how do I know it works? what I am trying to do is get readings of data form an accelerometer (ADXL345) (datasheet) trough either SPI or I2C with the imp, I already have seen it with arduino but I would like to use the imp for this.

Squirrel is dynamically typed, so you don’t need to declare a variable’s type. It’s enough simply to write:

value = 10; value = true; value = 42.000001;
or
value = "Squirrel";

When a variable is local to a function, not global, it’s conventional to state this:

local value = "This string is only available in this function";

Global variables are different: they a stored in one of Squirrel’s tables, which are arrays of key:value pairs. So globals are assigned as table elements, called ‘slots’ in Squirrel terminology and assigned this way:

value <- "This is a string"; value <- 42; value <- false;
or
value <- 99.9999;

thanks for the explanation. So if my code looks like this:
#include “SPI.h”
#include “Wire.h”
#include "ADXL345.h"
hardware.spi257.configure ();
#define ADXL345_DEVICE (0x53)
int (CS = 10);
char (POWER_CTL = 0x2D); //Power Control Register
char (DATA_FORMAT = 0x31);
char (DATAX0 = 0x32); //X-Axis Data 0
char (DATAX1 = 0x33); //X-Axis Data 1
char (DATAY0 = 0x34); //Y-Axis Data 0
char (DATAY1 = 0x35); //Y-Axis Data 1
char (DATAZ0 = 0x36); //Z-Axis Data 0
char (DATAZ1 = 0x37); //Z-Axis Data 1
char (values [10]);
int (x,y,z);
Which lines will be unnecessary or can be taken out?

Pretty much all of it. The #include lines pull in Arduino libraries - these are not compatible with Squirrel and the imp API.

You can used #define, but the Squirrel syntax is:

#define ADXL345_DEVICE 0x53

It’s hard to see which items are constants and which are variables, but broadly I would write your code as:

`// Global variables
CS <- 10;
values <- [0,0,0,0,0,0,0,0,0,0];
x <- 0;
y <- 0;
z <- 0;
DATA_FORMAT <- 0x31;
DATAX0 <- 0x32; //X-Axis Data 0
DATAX1 <- 0x33; //X-Axis Data 1
DATAY0 <- 0x34; //Y-Axis Data 0
DATAY1 <- 0x35; //Y-Axis Data 1
DATAZ0 <- 0x36; //Z-Axis Data 0
DATAZ1 <- 0x37; //Z-Axis Data 1

//Constants
const ADXL345_DEVICE = 0x53;
const POWER_CTL = 0x2D; // Power control register

//Program Start

hardware.spi257.configure(CLOCK_IDLE_LOW, 400);`

The exact parameters inside the brackets in the last line will depend on your device. Compare the datasheet details to the function’s description

Wow thanks for taking your time in helping me, since I am new to using the Imp I was kinda confused on how the code worked.

Maybe this question is not supposed to be in this section but I wonder if you can like display data on the log or somewhere on the computer using the imp, like when I sample data I would like to see it, is it possible?

I use UART for this. Here is the test code I write to send light-level data from the Imp directly to a connected computer. This could easily be adapted to send log data to the computer.

Linux and Mac OS X can do this in the Terminal; Windows needs puTTY.

You will also need a USB-to-serial adaptor cable, <a http://uk.farnell.com/ftdi/ttl-232r-rpi/cable-debug-ttl-232-usb-rpi/dp/2147356?CMP=e-8976-00001047=“such as this one”>such as this one. You can get drivers from the FTDI website.

`// Imp UART example code
// This code uses UART to communicate with a host computer
// It requires a USB-to-serial adaptor cable such as this one:
// http://uk.farnell.com/ftdi/ttl-232r-rpi/cable-debug-ttl-232-usb-rpi/dp/2147356?CMP=e-8976-00001047
// Note: yellow = TX; orange = RX ; black = GND


// Alias one of the Imp's UARTs

computer <- hardware.uart57;

// Create a global variable to hold text sent back from the connected computer

input_string <- "";

// Define functions

function loop()
{
	// Gets integer value from Imp's light sensor,
	// converts it to a string and relays it via UART

	local current_light = hardware.lightlevel();
	computer.write("Current light level is: " + current_light + "\\r\
");
	
	// Sleep Imp for a second
	
	imp.wakeup(1, loop);
}


function readback()
{
    // Function triggered by receipt of a byte from the connected computer
    // Adds the input byte as an alphanumeric character to a buffer string
    // which is displayed in the log when the remote user hits Enter
    
    local byte = computer.read();
    
    // Ignore initial input
    
    if (byte == -1) return;
    
    if (byte == 13)
    {
        // Carriage return received? Output the string and clear it for the next input
    
        server.log("Sent string: " + input_string);
        input_string = "";
    }
    else
    {
        // Add the input character to the buffer
        
        input_string = input_string + chr(byte);
    }
}


function chr(ascii_value)
{
    // Convert passed integer value Ascii code into a character string
    
    if (ascii_value < 32) return "";
    
    return format("%c", ascii_value);
}



// Start of program

// Configure UART. Note the use of the final, optional parameter: a callback function
// triggered by a byte of data arriving from remote connection

computer.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS, readback);
loop();`

You can output to the IDE on the computer

server.log(“some message to you”);

sorry - seems easy so maybe I have misunderstood something?

You can also send messages to the agent and have the agent send them in response to an http GET from a web page.

You would not need a physical connection to see data on a PC

So easy, mjkuwp94, that I assumed LeonKuro had already rejected it for some reason, ie. his imp is offline, perhaps when his WiFi is down. But, yes,

server.log("some message to you");

is the stock way to do this.

Thanks for the response I think mjkuwp94’s idea is good since I only need to read the data of an accelerometer all the time I just need to output the data values to be seen so I guess server.log(“some message to you”) might do the trick, also smittytone your idea is good but unfortunately I need to communicate to the accelerometer using either SPI or I2C so UART might not be an option here but I will take it in account.

if you use the imp002 module you can easily have both UART and spi.

Depending on the accelerometer there might be a way to configure the wiring so that you can switch between spi and i2c in software without changing hardware. that is in addition to a uart.

however… the log command should work well for you and save the hassle of connecting more things.

LeonKuro, the UART option was offered because of a misunderstanding over what you were asking for. Using server.log() is a much better option for day-to-day debugging, but UART communications is a fun thing to try out.

Well thank you I might as well try using UART after I am done, just for the fun of it.