Texting via XBee
Overview
For our ECE 4180 Final Project we attempted to replicate the popular text messaging software, iMessage. Our design allows for two users to type a message using the ABC text input style on the MPR121 Capacitive Touch Pad. The text is displayed on the user's uLCD screen and when the push button is pressed, the message is sent and displayed on the other user's uLCD screen. As new messages are received, they are displayed below the old messages
- by Daniel Begasse and Keith Ng
Pictures and Videos
Components
- uLCD http://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd
- MPR121 Capacitive Touch Pad http://developer.mbed.org/users/4180_1/notebook/mpr121-i2c-capacitive-touch-sensor/
- XBee S1 http://developer.mbed.org/cookbook/XBee
- Push Button http://developer.mbed.org/users/4180_1/notebook/pushbuttons/
uLCD PinOut
MBED | uLCD |
p27 | TX |
p28 | RX |
p30 | RES |
VU | +5V |
GND | GND |
XBee PinOut
MBED | XBee |
3.3V | p1 |
p14 | p2 |
p13 | p3 |
p11 | p5 |
GND | GND |
MPR121 PinOut
MBED | MPR121 |
3.3V | VCC |
IRQ | p26 |
SCL | p10 |
SDA | p9 |
GND | GND |
Code
main.cpp
#include "mbed.h" #include "uLCD_4DGL.h" #include <stdio.h> #include <stdlib.h> #include <mpr121.h> #include <iostream> #include <string> using namespace std; //Set up the interrupt pin InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 I2C i2c(p9, p10); // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); //create a global lcd project uLCD_4DGL uLCD(p28, p27, p30); // Setup the Serial to the PC for debugging Serial pc(USBTX, USBRX); //Set up XBee Serial xbee(p13, p14); DigitalOut rst1(p11); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); DigitalIn send(p21); volatile bool tripped = false; volatile bool validkey;//This represents if the key pressed was a valid key volatile int oldpressed;//This is the previous key that was pressed volatile int value;//This is the value coming in from the register volatile int pressed;//This is the current key being pressed volatile int pressedcount = 0;//This is how many times the current key has been repeatedly pressed int cursor_row = 13; int cursor_col = 0; int read_row = 0; int read_col = 0; string message_send = "- "; string message_receive = " "; int char_count = 0;//This is the number of times the space button has been pressed for the current message unsigned size; int received_count = 0; bool sent = false; char key_1[1] = {'1'}; char key_2[4] = {'a','b','c','2'}; char key_3[4] = {'d','e','f','3'}; char key_4[4] = {'g','h','i','4'}; char key_5[4] = {'j','k','l','5'}; char key_6[4] = {'m','n','o','6'}; char key_7[5] = {'p','q','r','s','7'}; char key_8[4] = {'t','u','v','8'}; char key_9[5] = {'w','x','y','z','9'}; char key_10[1] = {' '}; void fallInterrupt() { validkey = false;//Each key pressed is invalid until proven to be valid value = mpr121.read(0x00); value += mpr121.read(0x01)<<8; //Take the value from the register and use it to assign a key # switch(value){ case (0x1): pressed = 10; validkey = true; break; case (0x10): pressed = 11; validkey = true; break; case (0x100): pressed = 12; validkey = true; break; case (0x2): pressed = 7; validkey = true; break; case (0x20): pressed = 8; validkey = true; break; case (0x200): pressed = 9; validkey = true; break; case (0x4): pressed = 4; validkey = true; break; case (0x40): pressed = 5; validkey = true; break; case (0x400): pressed = 6; validkey = true; break; case (0x8): pressed = 1; validkey = true; break; case (0x80): pressed = 2; validkey = true; break; case (0x800): pressed = 3; validkey = true; break; default: validkey = false; break; } //If the two values are different, assign pressed to oldpressed if ((pressed != oldpressed) && (validkey == true) ){ oldpressed = pressed; pressedcount = 0; char_count++;//move the cursor over if there is a new key } else if ((pressed == oldpressed) && (value != 0)) { pressedcount++; } } void checkCharacter(){ if (pressed == 1){ if (pressedcount > 0){ pressedcount = 0; } message_send[char_count] = '1'; } if (pressed == 2){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_2[pressedcount]; } if (pressed == 3){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_3[pressedcount]; } if (pressed == 4){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_4[pressedcount]; } if (pressed == 5){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_5[pressedcount]; } if (pressed == 6){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_6[pressedcount]; } if (pressed == 7){ if (pressedcount > 4){ pressedcount = 0; } message_send[char_count] = key_7[pressedcount]; } if (pressed == 8){ if (pressedcount > 3){ pressedcount = 0; } message_send[char_count] = key_8[pressedcount]; } if (pressed == 9){ if (pressedcount > 4){ pressedcount = 0; } message_send[char_count] = key_9[pressedcount]; } if (pressed == 10) { message_send[char_count] = ' '; } if (pressed == 11) { if (pressedcount > 0){ pressedcount = 0; } message_send[char_count] = '0'; } } void printMessage(){ uLCD.locate(cursor_col, cursor_row); //This will print the current message being typed towards the bottom of the lcd screen for ( unsigned i = 0; i <= char_count; i++ ){ uLCD.printf("%c", message_send[i]); } //If the message was just sent, if ( sent == true ){ uLCD.locate(0, cursor_row); for ( unsigned i = 1; i <= char_count+1; i++ ){ message_send[i] = ' ';//Overwrite all of the spots in the string with a space uLCD.printf("%c", message_send[i]); } char_count = 0; } } void write(){ char chartosend; for ( unsigned i = 0; i <= char_count; i++ ){ //Go through each char of message and send the char's 1 by 1 through the xbee chartosend = message_send[i]; xbee.printf("%c", chartosend); wait(0.01); led2 = 1; wait(0.01); led2 = 0; } } void read(){ char fromXBee; //led3 = 1; //wait(0.2); if(xbee.readable()) { uLCD.locate(received_count, read_row); led4 = 1; wait(0.2); fromXBee = xbee.getc(); //Get individual characters being sent by other xbee message_receive[received_count] = fromXBee; uLCD.printf("%c", message_receive[received_count]); received_count++; read(); led4 = 0; //wait(0.2); } //led3 = 0; //wait(0.2); } int main(){ rst1 = 0; wait_ms(1); rst1 = 1; wait_ms(1); pc.baud(9600); send.mode(PullUp); interrupt.mode(PullUp); wait(.001); interrupt.fall(&fallInterrupt); while(1){ checkCharacter(); printMessage(); sent = false; if ( send == 0 ){ led2 = 1; write(); sent = true; pressed = 0; validkey = false; } read(); if (sent == true){ read_row++; received_count = 0; } } }
Import programxbee_imessage
ECE 4180 Final Project
Please log in to post comments.