-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "ADXL330.h" | ||
#include <WProgram.h> | ||
|
||
ADXL330::ADXL330(Aiko::Device::MCP320x& adc) { | ||
adc_ = &adc; | ||
isSetUp_ = false; | ||
} | ||
|
||
int ADXL330::readX() { if (!isSetUp_) setup(); return adc_->readChannel(0) - 2048; } | ||
int ADXL330::readY() { if (!isSetUp_) setup(); return adc_->readChannel(1) - 2048; } | ||
int ADXL330::readZ() { if (!isSetUp_) setup(); return adc_->readChannel(2) - 2048; } | ||
|
||
void ADXL330::setup() { | ||
// Make sure the accelerometer self-test pin stays low. | ||
pinMode(3, OUTPUT); | ||
digitalWrite(3, LOW); | ||
isSetUp_ = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <AikoDeviceMCP320x.h> | ||
|
||
class ADXL330 { | ||
public: | ||
ADXL330(Aiko::Device::MCP320x& adc); | ||
int readX(); | ||
int readY(); | ||
int readZ(); | ||
|
||
private: | ||
void setup(); | ||
|
||
bool isSetUp_; | ||
Aiko::Device::MCP320x *adc_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include <AikoDeviceMCP320x.h> | ||
#include <AikoEvents.h> | ||
#include <Wire.h> | ||
#include <OneWire.h> | ||
#include "ADXL330.h" | ||
#include "DS1307.h" | ||
#include "DS18B20.h" | ||
#include "MPX4101A.h" | ||
|
||
using namespace Aiko; | ||
|
||
Device::MCP320x adc5v(47); | ||
Device::MCP320x adc3v(48); | ||
ADXL330 accelerometer(adc3v); | ||
MPX4101A mapSensor(adc5v); | ||
DS1307 clock; | ||
OneWire oneWire(2); | ||
DS18B20 temperatureSensor(oneWire); | ||
|
||
void reportNodeName() { | ||
Serial.println("(nodeName= artemis_1)"); | ||
} | ||
|
||
void readClock() { | ||
clock.read(); | ||
|
||
Serial.print("(clock= "); | ||
if (clock.hour_ < 10) Serial.print('0'); | ||
Serial.print((int)clock.hour_); | ||
if (clock.minute_ < 10) Serial.print('0'); | ||
Serial.print((int)clock.minute_); | ||
if (clock.second_ < 10) Serial.print('0'); | ||
Serial.print((int)clock.second_); | ||
Serial.println(")"); | ||
} | ||
|
||
void readAccelerometer() { | ||
int x = accelerometer.readX(); | ||
int y = accelerometer.readY(); | ||
int z = accelerometer.readZ(); | ||
|
||
Serial.print("(acceleromter_x= "); | ||
Serial.print(x); | ||
Serial.println(")"); | ||
|
||
Serial.print("(acceleromter_y= "); | ||
Serial.print(y); | ||
Serial.println(")"); | ||
|
||
Serial.print("(acceleromter_z= "); | ||
Serial.print(z); | ||
Serial.println(")"); | ||
} | ||
|
||
void readMapSensor() { | ||
Serial.print("(barometer= "); | ||
Serial.print(mapSensor.readPressure()); | ||
Serial.println(")"); | ||
} | ||
|
||
void readLDRs() { | ||
Serial.print("(light_a= "); | ||
Serial.print(analogRead(0)); | ||
Serial.println(")"); | ||
Serial.print("(light_b= "); | ||
Serial.print(analogRead(1)); | ||
Serial.println(")"); | ||
} | ||
|
||
void readVoltage() { | ||
float voltage = (float)analogRead(7) * (5.0 / 1024.0); | ||
|
||
Serial.print("(voltage= "); | ||
Serial.print(voltage); | ||
Serial.println(")"); | ||
} | ||
|
||
void readTemperature() { | ||
temperatureSensor.read(); | ||
|
||
Serial.print("(temperature= "); | ||
Serial.print(temperatureSensor.temperature()); | ||
Serial.println(")"); | ||
|
||
temperatureSensor.startConversion(); | ||
} | ||
|
||
void setup() { | ||
clock.second_ = 0; | ||
clock.minute_ = 1; | ||
clock.hour_ = 2; | ||
clock.dayOfWeek_ = 3; | ||
clock.dayOfMonth_ = 4; | ||
clock.month_ = 5; | ||
clock.year_ = 6; | ||
clock.set(); | ||
|
||
Serial.begin(9600); | ||
Events.addHandler(reportNodeName, 1000); | ||
Events.addHandler(readClock, 1000); | ||
Events.addHandler(readAccelerometer, 100); | ||
Events.addHandler(readMapSensor, 100); | ||
Events.addHandler(readLDRs, 100); | ||
Events.addHandler(readVoltage, 100); | ||
Events.addHandler(readTemperature, 1000); | ||
} | ||
|
||
void loop() { | ||
Events.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <WProgram.h> | ||
#include "DS1307.h" | ||
#include <Wire.h> | ||
|
||
#define DS1307_I2C_ADDRESS 0x68 | ||
|
||
DS1307::DS1307() { | ||
isSetUp_ = false; | ||
} | ||
|
||
unsigned char DS1307::bcdToDecimal(unsigned char x) { | ||
return ((x >> 4) * 10) + (x & 0xF); | ||
} | ||
|
||
unsigned char DS1307::decimalToBCD(unsigned char x) { | ||
return ((x / 10) << 4) | (x % 10); | ||
} | ||
|
||
void DS1307::read() { | ||
if (!isSetUp_) setup(); | ||
|
||
Wire.beginTransmission(DS1307_I2C_ADDRESS); | ||
Wire.send(0); | ||
Wire.endTransmission(); | ||
|
||
Wire.requestFrom(DS1307_I2C_ADDRESS, 7); | ||
|
||
// A few of these need masks because certain bits are control bits | ||
second_ = bcdToDecimal(Wire.receive() & 0x7f); | ||
minute_ = bcdToDecimal(Wire.receive()); | ||
hour_ = bcdToDecimal(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm | ||
dayOfWeek_ = bcdToDecimal(Wire.receive()); | ||
dayOfMonth_ = bcdToDecimal(Wire.receive()); | ||
month_ = bcdToDecimal(Wire.receive()); | ||
year_ = bcdToDecimal(Wire.receive()); | ||
} | ||
|
||
void DS1307::set() { | ||
if (!isSetUp_) setup(); | ||
|
||
Wire.beginTransmission(DS1307_I2C_ADDRESS); | ||
Wire.send(0); | ||
Wire.send(decimalToBCD(second_)); // 0 to bit 7 starts the clock | ||
Wire.send(decimalToBCD(minute_)); | ||
Wire.send(decimalToBCD(hour_)); // If you want 12 hour am/pm you need to set | ||
// bit 6 (also need to change readDateDs1307) | ||
Wire.send(decimalToBCD(dayOfWeek_)); | ||
Wire.send(decimalToBCD(dayOfMonth_)); | ||
Wire.send(decimalToBCD(month_)); | ||
Wire.send(decimalToBCD(year_)); | ||
Wire.send(0x10); | ||
Wire.endTransmission(); | ||
} | ||
|
||
void DS1307::setup() { | ||
Wire.begin(); | ||
isSetUp_ = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class DS1307 { | ||
public: | ||
DS1307(); | ||
void read(); | ||
void set(); | ||
|
||
unsigned char second_, minute_, hour_, dayOfWeek_, dayOfMonth_, month_, year_; | ||
|
||
private: | ||
bool isSetUp_; | ||
|
||
unsigned char bcdToDecimal(unsigned char x); | ||
unsigned char decimalToBCD(unsigned char x); | ||
void setup(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "DS18B20.h" | ||
|
||
#define ONE_WIRE_DEVICE_18B20 0x28 | ||
|
||
#define ONE_WIRE_COMMAND_READ_SCRATCHPAD 0xBE | ||
#define ONE_WIRE_COMMAND_START_CONVERSION 0x44 | ||
#define ONE_WIRE_COMMAND_MATCH_ROM 0x55 | ||
#define ONE_WIRE_COMMAND_SKIP_ROM 0xCC | ||
|
||
DS18B20::DS18B20(OneWire &oneWire) { | ||
oneWire_ = &oneWire; | ||
isSetUp_ = false; | ||
} | ||
|
||
void DS18B20::setup() { | ||
deviceFound_ = false; | ||
oneWire_->reset_search(); | ||
while (!deviceFound_ && oneWire_->search(address_)) { | ||
// FIXME: CRC check the address | ||
if (address_[0] == ONE_WIRE_DEVICE_18B20) deviceFound_ = true; | ||
} | ||
|
||
isSetUp_ = true; | ||
} | ||
|
||
void DS18B20::startConversion() { | ||
if (!isSetUp_) setup(); | ||
oneWire_->reset(); // time: 1 millisecond | ||
oneWire_->select(address_); // time: 5 milliseconds | ||
oneWire_->write(ONE_WIRE_COMMAND_START_CONVERSION, 1); // time: 1 millisecond | ||
} | ||
|
||
void DS18B20::read() { | ||
if (!isSetUp_) setup(); | ||
oneWire_->reset(); // time: 1 millisecond | ||
oneWire_->select(address_); // time: 5 milliseconds | ||
oneWire_->write(ONE_WIRE_COMMAND_READ_SCRATCHPAD); // time: 1 millisecond | ||
|
||
for (int i = 0; i < 9; i++) data_[i] = oneWire_->read(); | ||
} | ||
|
||
float DS18B20::temperature() { | ||
int sign = data_[1] & 0xF0 ? -1 : 1; | ||
int value = ((int)(data_[1] & 0x07) << 8) | data_[0]; | ||
return ((float)(value * sign)) / 16.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <OneWire.h> | ||
|
||
class DS18B20 { | ||
public: | ||
DS18B20(OneWire &oneWire); | ||
void startConversion(); | ||
void read(); | ||
float temperature(); | ||
|
||
private: | ||
OneWire *oneWire_; | ||
bool isSetUp_; | ||
bool deviceFound_; | ||
unsigned char address_[8]; | ||
unsigned char data_[9]; | ||
|
||
void setup(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "MPX4101A.h" | ||
|
||
MPX4101A::MPX4101A(Aiko::Device::MCP320x &adc) { | ||
adc_ = &adc; | ||
} | ||
|
||
int MPX4101A::readPressure() { | ||
adc_->readChannel(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <AikoDeviceMCP320x.h> | ||
|
||
class MPX4101A { | ||
public: | ||
MPX4101A(Aiko::Device::MCP320x &adc); | ||
int readPressure(); | ||
|
||
private: | ||
Aiko::Device::MCP320x *adc_; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.