Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:4555c427d7a8
- Child:
- 1:0a93e9e88ad3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Oct 07 20:28:50 2015 +0000
@@ -0,0 +1,246 @@
+#include "mbed.h"
+#include "LM75B.h"
+#include "MMA7455.h"
+#include "EthernetInterface.h"
+
+#define F 0
+#define C 1
+
+// thresholds for detecting a rotation of 90 deg or more
+#define X_TH 60
+#define Y_TH 60
+#define Z_TH 0
+
+/*
+**************************
+ Display Functions
+**************************
+*/
+void displayBanner();
+void newline();
+
+/*
+**************************
+ Utility Functions
+**************************
+*/
+float tempConverter(float inTemp, bool convDir);
+void accFeed();
+void sampleAcc(int period, int32_t data[3]);
+
+/*
+**************************
+ Global Data
+**************************
+*/
+const char banner[37] = "Serial Comm Established with LPC4088";
+
+// holds last temperature reading
+float currTemp = 0;
+
+// x, y, z
+// holds current position
+int32_t accPos[3] = {};
+// holds up to 20 samples of data
+int32_t accData[20][20][20] = {};
+// holds calibration offsets
+int32_t accCal[3] = {};
+
+/*
+**************************
+ Objects
+**************************
+*/
+// UART connection to PC
+Serial terminal(USBTX, USBRX);
+// accelerometer on I2C bus
+MMA7455 acc(P0_27, P0_28);
+// temperature sensor on I2C bus
+LM75B temp(P0_27, P0_28, LM75B::ADDRESS_1, 120);
+// ethernet connection to log sensor data on a server
+EthernetInterface eth;
+
+int main() {
+ /*
+ **************************
+ Initializations
+ **************************
+ */
+ SerialInit();
+ EthernetInit();
+ TempInit();
+ AccInit();
+ /*
+ **************************
+ Main Execution
+ **************************
+ */
+ while(1) {
+ currTemp = (float)temp;
+ terminal.printf("Current temperature (F): %.1f", tempConverter(currTemp, F));
+ newline();
+
+ acc.read(accPos[0], accPos[1], accPos[2]);
+ while(accPos[2] < Z_TH) {
+ // output audible beep
+ //alarm = 1;
+ terminal.printf("BEEEEEP!");
+ newline();
+ acc.read(accPos[0], accPos[1], accPos[2]);
+ }
+ wait(1);
+ //alarm = 0;
+ }
+}
+
+/*
+**************************
+ Function Definitions
+**************************
+*/
+void SerialInit()
+{
+ // initialize connection to PC
+ terminal.baud(19200);
+ displayBanner();
+ newline();
+}
+
+void EthernetInit()
+{
+ // initialize ethernet connection
+ terminal.printf("Establishing ethernet connection...");
+ newline();
+ // use DHCP - static IP
+ eth.init("192.168.1.4","255.255.255.0","192.168.1.1");
+ eth.connect();
+ terminal.printf(" IP Address: %s", eth.getIPAddress()); newline();
+ TCPSocketConnection socket;
+ socket.connect("192.168.1.3", 1001);
+ if(socket.is_connected()) {
+ terminal.printf(" Connected!");
+ // write to server
+ char http_cmd[] = "This is a test";
+ socket.send_all(http_cmd, sizeof(http_cmd)-1);
+ }
+ else
+ terminal.printf(" Unable to connect.");
+ newline(); newline();
+}
+
+void TempInit()
+{
+ terminal.printf("Temperature sensor...");
+ // verify connection with temp sensor
+ if(temp.open()) {
+ terminal.printf("initialized.");
+ newline();
+ }
+ else {
+ terminal.printf("unavailable.");
+ newline();
+ }
+}
+
+void AccInit()
+{
+ // configure accelerometer for 2G range
+ acc.setMode(MMA7455::ModeMeasurement);
+ acc.setRange(MMA7455::Range_2g);
+ terminal.printf("Calibrating accelerometer...");
+
+ // if we can successfully calibrate the accelerometer...
+ if(acc.calibrate()) {
+ newline();
+ acc.getCalibrationOffsets(accCal[0], accCal[1], accCal[2]);
+ terminal.printf(" Offsets are (xyz): (%d) (%d) (%d)", accCal[0], accCal[1], accCal[2]);
+ newline(); newline();
+ }
+ else {
+ terminal.printf("failed.");
+ newline(); newline();
+ }
+}
+
+void newline()
+{
+ // newline = carriage return + line feed
+ terminal.putc('\n');
+ terminal.putc('\r');
+}
+
+/*
+ Displays the following header:
+************************************************
+Serial Comm Established with LPC4088
+************************************************
+*/
+void displayBanner()
+{
+ int i = 0;
+ for(int j = 0; j < 48; j++)
+ terminal.putc('*');
+ newline();
+
+ while(i != 36) {
+ char c = banner[i];
+ terminal.putc(c);
+ i++;
+ }
+ newline();
+
+ for(int j = 0; j < 48; j++)
+ terminal.putc('*');
+}
+
+// converts a temperature reading from C > F or F > C
+// convDir = 0: F > C
+// convDir = 1: C > F
+float tempConverter(float inTemp, bool convDir)
+{
+ const float CToF = (9.0/5.0);
+ const float FToC = (5.0/9.0);
+ const int offset = 32;
+ if(convDir == 0){ //Convert C to F
+ return (inTemp * CToF) + offset;
+ }
+ else{ //Convert F to C
+ return (inTemp - offset) * FToC;
+ }
+}
+
+// prints the current positional data from the accelerometer
+void accFeed()
+{
+ // returns false if the mode is set to standby or unable to convert
+ if(acc.read(accPos[0], accPos[1], accPos[2])) {
+ terminal.printf("x: %d y: %d z: %d", accPos[0], accPos[1], accPos[2]);
+ newline();
+ }
+ else {
+ terminal.printf("Unable to access MMA7455");
+ newline();
+ }
+}
+
+/*
+ Samples the accelerometer in 1/4s intervals for the length of "period" (max 5).
+ The results are placed in the 3D array. Additionally, the array passed in
+ will hold the last reading.
+*/
+void sampleAcc(int period, int32_t data[3])
+{
+ for(int i = 0; i < period*4; i++) {
+ //load temps
+ acc.read(data[0], data[1], data[2]);
+ accData[i][0][0] = data[0]; // x
+ accData[0][i][0] = data[1]; // y
+ accData[0][0][i] = data[2]; // z
+ wait(.25);
+ }
+ // if we didn't fill the whole array, we'll clear it to avoid confusion later
+ if(period < 5) {
+ for(int i = period*4; i < 20; i++)
+ accData[i][i][i] = 0;
+ }
+}
\ No newline at end of file