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.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 8:d27efcac2dd7
- Parent:
- 7:6b27977d1800
- Child:
- 9:55473409c585
--- a/main.cpp Fri Mar 31 23:40:20 2017 +0000
+++ b/main.cpp Sat Apr 01 20:19:57 2017 +0000
@@ -1,5 +1,10 @@
#include "mbed.h"
+//******************************************************************
+// All variables defined below
+//******************************************************************
+
+//communication
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
Serial keyOut(p13, p14);
@@ -12,13 +17,10 @@
char s;
int i;
-
//previous point values
int prevX = 1023;
int prevY = 1023;
-
-
//point variables
int point1x = 0;
int point1y = 0;
@@ -29,22 +31,24 @@
int point4x = 0;
int point4y = 0;
-
-
//sensitivity
//Level 5: p0 = 0x96, p1 = 0xFE, p2 = 0xFE, p3 = 0x05
+//highest sensitivity to more accurately detect points
int sen0 = 0x96;
int sen1 = 0xFE;
int sen2 = 0xFE;
int sen3 = 0x00;
-//matrices of x and y coordinates
+//matrices of x and y coordinates from the first camera
int onex[4];
int oney[4];
+//******************************************************************
+// All methods defined below
+//******************************************************************
-
+//writes two bytes to the camera
void write2bytes(char data1, char data2){
char out[2];
out[0] = data1;
@@ -53,33 +57,35 @@
wait(0.01);
}
-void mouseCommand(uint8_t buttons, uint8_t x, uint8_t y) {
- keyOut.putc(0xFD);
- keyOut.putc(0x00);
- keyOut.putc(0x03);
- keyOut.putc(buttons);
- keyOut.putc(x);
- keyOut.putc(y);
- keyOut.putc(0x00);
- keyOut.putc(0x00);
- keyOut.putc(0x00);
-}
+//TODO: get rid of these b/c they don't work. instead use printc
+//
+//void mouseCommand(uint8_t buttons, uint8_t x, uint8_t y) {
+// keyOut.putc(0xFD);
+// keyOut.putc(0x00);
+// keyOut.putc(0x03);
+// keyOut.putc(buttons);
+// keyOut.putc(x);
+// keyOut.putc(y);
+// keyOut.putc(0x00);
+// keyOut.putc(0x00);
+// keyOut.putc(0x00);
+//}
+//
+//void keyCommand(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2 = 0, uint8_t keycode3 = 0,
+// uint8_t keycode4 = 0, uint8_t keycode5 = 0, uint8_t keycode6 = 0) {
+// keyOut.putc(0xFD); // our command
+// keyOut.putc(modifiers); // modifier!
+// keyOut.putc(0x00); // 0x00
+// keyOut.putc(keycode1); // key code #1
+// keyOut.putc(keycode2); // key code #2
+// keyOut.putc(keycode3); // key code #3
+// keyOut.putc(keycode4); // key code #4
+// keyOut.putc(keycode5); // key code #5
+// keyOut.putc(keycode6); // key code #6
+//}
-void keyCommand(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2 = 0, uint8_t keycode3 = 0,
- uint8_t keycode4 = 0, uint8_t keycode5 = 0, uint8_t keycode6 = 0) {
- keyOut.putc(0xFD); // our command
- keyOut.putc(modifiers); // modifier!
- keyOut.putc(0x00); // 0x00
- keyOut.putc(keycode1); // key code #1
- keyOut.putc(keycode2); // key code #2
- keyOut.putc(keycode3); // key code #3
- keyOut.putc(keycode4); // key code #4
- keyOut.putc(keycode5); // key code #5
- keyOut.putc(keycode6); // key code #6
-}
-
+// Initialize WiiMote Camera
void initCamera(void){
- // Initialize WiiMote Camera
write2bytes(0x30, 0x01);
write2bytes(0x00, 0x02);
write2bytes(0x00, 0x00);
@@ -94,6 +100,9 @@
}
+//get data from camera one
+//populates onex and oney with values depending on the measured points
+//NOTE: 1023 means nothing was detected
void readCameraData(void){
//request data from camera
@@ -104,16 +113,7 @@
//get data from camera
camera1.read(slaveAddress, data_buf, 16);
-
-// //print content of data buffer
-// for(int i = 0; i < 16; i++){
-// pc.printf(" data buf %d --> %c <-- \t as int --> %d <--\n", i, data_buf[i], (float) data_buf[i]);
-//
-// }
-
-
-
-
+
//POINT 1
//get data
point1x = data_buf[1];
@@ -123,6 +123,11 @@
onex[0] = point1x + ((s & 0x30) << 4);
oney[0] = point1y + ((s & 0xC0) << 2);
+
+ //>>>>>>>>>>>>>>>>>Begin unfinished code for moving averages
+
+ //look at delta btwn prev val and current
+ //TODO: moving average
if(prevX != 1023 || prevY != 1023){
if(onex[0] - prevX > 5){
keyOut.putc(0x41);
@@ -137,6 +142,8 @@
prevY = oney[0];
+ //<<<<<<<<<<<<<<<<End unfinished code for moving averages
+
//POINT 2
//get data
point2x = data_buf[4];
@@ -166,6 +173,8 @@
}
+//print to serial monitor the coordinates of the points stored in
+//the passed x and y arrays
void printCamData(int xcor[4], int ycor[4]){
for(int i = 0; i<4; i++){
int x = xcor[i];
@@ -175,12 +184,7 @@
pc.printf(" %d,", x);
//y coordinate
- pc.printf(" %d\t", y);
-
- //print A if x is less than 800
- // if(x < 800 ){
-// keyOut.putc(0x41);
-// }
+ pc.printf(" %d\t", y);
}
//new line and delay
@@ -191,7 +195,7 @@
-
+//entrance to the code
int main() {
myled = 0;
//slaveAddress = IRsensorAddress >> 1;
@@ -201,14 +205,13 @@
//update baud rate
pc.baud(115200);
- //click the left button and drag the mouse down 50 units
- //mouseCommand(0x1, 0, -50);
-// keyCommand(0, 4);
-
-
-
+ //loop to search for new info using the camera
while(1) {
+
+ //wait
wait(0.04);
+
+ //toggle test LED
myled = 1 - myled;
//get the camera data
@@ -217,7 +220,7 @@
//print points
printCamData(onex, oney);
- //testing infinite print
+ //uncomment below to test infinite print
//keyOut.putc(0x41);

