forked from lzmartinico/HackTheBurgh2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
135 lines (120 loc) · 3.45 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* WiFi Example
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <stdlib.h>
#include "mbed.h"
#include "BLEDevice.h"
#include "TCPSocket.h"
#include "C12832.h"
#include "MMA7660.h"
#define WIFI_ESP8266 1
#define WIFI_IDW0XX1 2
#if 1
#include "OdinWiFiInterface.h"
OdinWiFiInterface wifi;
#elif TARGET_REALTEK_RTL8195AM
#include "RTWInterface.h"
RTWInterface wifi;
#else // External WiFi modules
#if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
#include "ESP8266Interface.h"
ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
#elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
#include "SpwfSAInterface.h"
SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
#endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
#endif
Serial pc(USBTX, USBRX); // tx, rx
C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
MMA7660 accel(PF_0, PF_1);
InterruptIn button(PF_2);
int screen_width = 20; //lcd.columns();
int oldInd = screen_width*1.5;
int stateX = 0;
int stateY = 0;
char message[66] = "";
float oldX, oldY, oldZ;
void lcd_print(const char* message)
{
lcd.cls();
lcd.locate(0, 3);
lcd.printf(message);
}
void updateEgg(int x, int y) {
// Override hte
pc.printf("updateEgg called with %d and %d", x, y);
int xOffset = 0;
if (stateX > -0.5 && stateX <= 0.5) {
xOffset = screen_width;
} else if (stateX > 0.5 && stateX <= 1) {
xOffset = 2*screen_width;
} else {
lcd_print("Failure");
return;
}
pc.printf("xOffset is %d", xOffset);
message[oldInd] = '_';
message[oldInd+1] = '_';
message[xOffset+screen_width/2+stateY] = '(';
message[xOffset+screen_width/2+stateY+1] = ')';
oldInd = xOffset+screen_width/2+stateY;
}
void read_accel() {
if (!accel.testConnection()) {
lcd_print("Error");
} else {
float x = accel.x();
float y = accel.y();
float z = accel.z();
char val[32];
sprintf(val, "x=%.2f y=%.2f z=%.2f\r\n", x, y, z);
float thresh = 0.05;
if (oldX && abs(oldX-x >= thresh)) {
if (x - oldX > 0) stateX -= 0.1;
else stateX += 0.1;
}
if (oldY && abs(oldY-y >= thresh)) {
if (y - oldY > 0) stateY -= 0.1;
else stateY += 0.1;
}
updateEgg(stateX, stateY);
//lcd_print(val);
pc.printf(val);
oldX = x;
oldY = y;
oldZ = z;
}
}
void start_accellerometer() {
accel.setActive(true);
while (true) {
read_accel();
wait_ms(1000);
}
}
int main()
{
int count = 0;
std::string half(screen_width+screen_width/2-1, '_');
const char *half_c = half.c_str();
std::strcat(message, half_c);
std::strcat(message, "()");
std::strcat(message, half_c);
lcd.cls();
lcd.locate(0, 2);
lcd.printf(const_cast<const char*>(message));
start_accellerometer();
}