Sun Aug 10, 2014 12:34 pm
// Gamebuino "NFC" Demo - by Mark Feldman (Myndale)
// http://gamebuino.com/forum/viewtopic.php?f=12&t=995
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
// how fast to transmit, this demo uses 10 bits per byte (1 start, 8 data, 1 stop)
#define BAUD_RATE 200
// how sensitive the ambient light sensor should be
#define TRIGGER 50
#define DELAY (1000/BAUD_RATE)
#define MENULENGTH 2
const char strTransmit[] PROGMEM = "Transmit";
const char strReceive[] PROGMEM = "Receive";
const char* const menu[MENULENGTH] PROGMEM = {
strTransmit,
strReceive,
};
#define MAX_MESSAGE_LENGTH 20
char message[MAX_MESSAGE_LENGTH];
int message_pos;
// use to keep track of ambient light, this value is used to do basic automatic gain
// control so that the demo can work in multiple ambient light conditions
int ambient;
void setup(){
gb.begin();
gb.titleScreen(F("\"NFC\" Demo"));
}
void loop(){
switch(gb.menu(menu, MENULENGTH))
{
case -1: //nothing selected
gb.titleScreen(F("\"NFC\" Demo"));
break;
case 0:
transmit();
break;
case 1:
receive();
break;
}
}
void transmit() {
strcpy(message, "Hello Gamebuino!");
gb.keyboard(message, sizeof(message)-1);
gb.display.clear();
gb.display.println("Transmitting message:");
gb.display.println();
gb.display.println(message);
gb.backlight.automatic = false;
gb.display.update();
pinMode(AMBIENTLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, HIGH);
ambient = analogRead(AMBIENTLIGHT_PIN);
gb.sound.playOK();
wait(3000, false);
digitalWrite(BACKLIGHT_PIN, LOW);
while (true)
{
if (wait(0, true))
return;
if (analogRead(AMBIENTLIGHT_PIN) >= (ambient-TRIGGER))
continue;
if (wait(1500, true))
return;
send(message);
send('\n');
if (wait(1500, true))
return;
}
}
boolean wait(int milliseconds, boolean watch)
{
long target = millis() + milliseconds;
do
{
gb.buttons.update();
gb.sound.updatePattern();
if (watch && any_button_pressed())
{
gb.sound.playCancel();
return true;
}
} while (millis() < target);
return false;
}
void sendBit(int bit)
{
digitalWrite(BACKLIGHT_PIN, bit ? HIGH : LOW);
delay(DELAY);
}
void send(char c)
{
Serial.print(c);
sendBit(1); // start bit
for (int i=0; i<8; i++, c>>=1)
sendBit(c&1);
sendBit(0); // stop bit
}
void send(const char * str)
{
while (*str)
send(*str++);
}
void receive() {
gb.display.clear();
gb.display.println("Receiving...");
gb.backlight.automatic = false;
gb.display.update();
pinMode(AMBIENTLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, LOW);
ambient = analogRead(AMBIENTLIGHT_PIN);
gb.sound.playOK();
wait(1000, false);
int low = analogRead(AMBIENTLIGHT_PIN);
int high = low + TRIGGER;
int middle = (low + high) >> 1;
message_pos = 0;
while (true)
{
// wait for start of character
while (true)
{
if (wait(0, true))
return;
int current = analogRead(AMBIENTLIGHT_PIN);
if (current >= middle)
{
high = current;
break;
}
if (current < middle)
{
low = current;
high = low + TRIGGER;
middle = (low + high) >> 1;
}
}
// read in the character
delay(2*DELAY/3);
high = analogRead(AMBIENTLIGHT_PIN);
middle = (low + high) >> 1;
// stream the bytes in
char c = 0;
for (int i=0; i<8; i++)
{
delay(DELAY);
int val = analogRead(AMBIENTLIGHT_PIN);
if (val > middle)
{
c |= (1 << i);
high = val;
}
else
{
low = val;
}
middle = (low + high) >> 1;
}
delay(DELAY);
// test for end of message
if (c == '\n')
{
message[message_pos++] = 0;
display_message();
return;
}
else if (message_pos < MAX_MESSAGE_LENGTH)
message[message_pos++] = c;
}
}
void display_message()
{
gb.sound.playOK();
while (true)
{
gb.update();
gb.display.clear();
gb.display.println("Received message:");
gb.display.println();
gb.display.println(message);
if (any_button_pressed())
{
gb.sound.playCancel();
return;
}
}
}
boolean any_button_pressed()
{
return gb.buttons.pressed(BTN_A) ||
gb.buttons.pressed(BTN_B) ||
gb.buttons.pressed(BTN_C);
}
Sun Aug 10, 2014 12:43 pm
Sun Aug 10, 2014 12:57 pm
Sun Aug 10, 2014 1:02 pm
Sun Aug 10, 2014 2:56 pm
Sun Aug 10, 2014 5:08 pm
Sun Aug 17, 2014 1:43 am
Mon Sep 08, 2014 7:10 pm