Team 5
From BenningtonWiki
we are meeting on thursday 1:30, and saturday 3. <source lang=c>
- define POWERAPIN 0
- define POWERBPIN 1
- define LIGHTWPIN 2
- define LIGHTXPIN 3
- define LIGHTYPIN 4
- define LIGHTZPIN 5
- define RESETPIN 2
- define BARFPIN 3
- define LEDPIN 4
- include <sd_raw.h>
- include <string.h>
- include <stdio.h>
- include <stdarg.h>
static long int log_count;
void rsprintf(const char *format, ...); void sd_write(uint32_t offset, void *buffer, uint16_t length); void sd_read(uint32_t offset, void *buffer, uint16_t length);
void setup(){
pinMode(RESETPIN, INPUT); pinMode(BARFPIN, INPUT); pinMode(LEDPIN, OUTPUT); sd_raw_init(); sd_check_format();
}
void loop(){
static char on; static long int next; long int now;
if (digitalRead(BARFPIN) > 0) {
// Serial.println("barf");
sd_print_log();
while (digitalRead(BARFPIN) > 0) ;
}
if (digitalRead(RESETPIN) > 0) {
sd_format();
while (digitalRead(RESETPIN) > 0) ;
}
now = millis();
if (now >= next) {
readSensors(now);
digitalWrite(LEDPIN, on);
on = !on;
next = (now + 1000);
}
}
void readSensors(unsigned long now){
int powera; int powerb; int lightw; int lightx; int lighty; int lightz;
// rsprintf("In readSensors()\n");
powera = analogRead(POWERAPIN); powerb = analogRead(POWERBPIN); lightw = analogRead(LIGHTWPIN); lightx = analogRead(LIGHTXPIN); lighty = analogRead(LIGHTYPIN); lightz = analogRead(LIGHTZPIN); sd_log((now/1000), powera, powerb, lightw, lightx, lighty, lightz);
}
struct memory{
long int timestamp; int powera, powerb, lightw, lightx, lighty, lightz;
};
void sd_format(){
unsigned long hello = 42; sd_write(0, &hello, sizeof(hello)); log_count = 0; sd_write(4, &log_count, sizeof(log_count));
}
void sd_check_format(){
int id;
sd_read(0, &id, sizeof(id));
if (id != 42){
sd_format();
}
sd_read(4, &log_count, sizeof(log_count));
}
void sd_log(long int time, int a, int b, int w, int x, int y, int z){
struct memory mem;
mem.timestamp = time;
mem.powera = a;
mem.powerb = b;
mem.lightw = w;
mem.lightx = x;
mem.lighty = y;
mem.lightz = z;
sd_read(4, &log_count, sizeof(log_count));
sd_write(((log_count * sizeof(mem)) + 8 ), &mem, sizeof(mem));
rsprintf("%ld: %ld, %d, %d, %d, %d, %d, %d\n", log_count, mem.timestamp, mem.powera, mem.powerb, mem.lightw, mem.lightx, mem.lighty, mem.lightz);
++log_count;
sd_write(4, &log_count, sizeof(log_count));
}
void rsprintf(const char *format, ...) {
static unsigned char inited = 0;
char msg[100];
va_list ap;
if (!inited) {
Serial.begin(9600);
inited = 1;
}
va_start(ap, format);
vsnprintf(msg, sizeof(msg)-1, format, ap);
Serial.print(msg);
va_end(ap);
}
void sd_print_log() { struct memory mem; sd_read(4, &log_count, sizeof(log_count)); for (long i=0; i < log_count; ++i) { sd_read(i*sizeof(mem)+8, &mem, sizeof(mem)); rsprintf("%ld: %ld, %d, %d, %d, %d, %d, %d\n", i, mem.timestamp, mem.powera, mem.powerb, mem.lightw, mem.lightx, mem.lighty, mem.lightz); }
return;
}
void sd_write(uint32_t offset, void *buffer, uint16_t length) {
sd_raw_write(offset, (uint8_t *) buffer, length); sd_raw_sync();
}
void sd_read(uint32_t offset, void *buffer, uint16_t length) {
sd_raw_read(offset, (uint8_t *) buffer, length);
} </source>






