AMTS/Mieke/SW/ODD/main.c

92 lines
2.9 KiB
C
Raw Normal View History

2018-04-03 15:32:24 +00:00
/*
Demo program for the 2017/18 open door day at HTL Hollabrunn
Copyright (C) 2018 Andreas Mieke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-03-18 22:43:59 +00:00
#include "display.h"
#include "bma.h"
#include "eeprom.h"
#include "bluetooth.h"
#define BUFFER_SIZE 256
int main()
{
2018-04-03 15:18:28 +00:00
// Variables needed
2018-03-18 22:43:59 +00:00
disp_state_t current_state = DISP_STATE_NONE;
uint8_t X, Y, Z, running = 0;
2018-04-03 15:18:28 +00:00
uint8_t bX[BUFFER_SIZE] = {128}, bY[BUFFER_SIZE] = {128}, bZ[BUFFER_SIZE] = {128}; // 128 is the 'zero' line in the display, so the array is initzialized to 128
2018-03-18 22:43:59 +00:00
uint16_t buffer_pos = 0;
2018-04-03 15:18:28 +00:00
// Init everything we need
2018-03-18 22:43:59 +00:00
disp_init();
systick_init();
bma_init();
eeprom_init();
bluetooth_init();
2018-04-03 15:18:28 +00:00
// Enable display
2018-03-18 22:43:59 +00:00
disp_enable();
// Main loop
for (;;) {
2018-04-03 15:18:28 +00:00
// Check if button is pressed
2018-03-18 22:43:59 +00:00
current_state = disp_get_last_state();
if(current_state == DISP_STATE_START) {
2018-04-03 15:18:28 +00:00
// If start button set running to true
2018-03-18 22:43:59 +00:00
running = 1;
} else if(current_state == DISP_STATE_PAUSE) {
2018-04-03 15:18:28 +00:00
// On pause set running to false
2018-03-18 22:43:59 +00:00
running = 0;
}
2018-04-03 15:18:28 +00:00
// Only read and send data if display is actually running
2018-03-18 22:43:59 +00:00
if(running == 1) {
2018-04-03 15:18:28 +00:00
// Get accelerations
2018-03-18 22:43:59 +00:00
bma_get_acc(&X, &Y, &Z);
2018-04-03 15:18:28 +00:00
// Put them into the buffer (for save/recall)
2018-03-18 22:43:59 +00:00
bX[buffer_pos] = X;
bY[buffer_pos] = Y;
bZ[buffer_pos] = Z;
2018-04-03 15:18:28 +00:00
// Send data to display and bluetooth
2018-03-18 22:43:59 +00:00
disp_send_gyro_data(X, Y, Z);
bluetooth_send_gyro_data(X, Y, Z);
2018-04-03 15:18:28 +00:00
// Increment buffer position
2018-03-18 22:43:59 +00:00
buffer_pos++;
if(buffer_pos == BUFFER_SIZE) {
2018-04-03 15:18:28 +00:00
// If buffer size is reached, return to 0 (ringbuffer)
2018-03-18 22:43:59 +00:00
buffer_pos = 0;
}
}
if(current_state == DISP_STATE_SAVE) {
2018-04-03 15:18:28 +00:00
// On save, send the three arrays to the EEPROM
2018-03-18 22:43:59 +00:00
eeprom_write(0x0000, bX, BUFFER_SIZE);
eeprom_write(0x0400, bY, BUFFER_SIZE);
eeprom_write(0x0800, bZ, BUFFER_SIZE);
}
if(current_state == DISP_STATE_RECALL) {
2018-04-03 15:18:28 +00:00
// On recall read the three arrays from EEPROM
2018-03-18 22:43:59 +00:00
eeprom_read(0x0000, bX, BUFFER_SIZE);
eeprom_read(0x0400, bY, BUFFER_SIZE);
eeprom_read(0x0800, bZ, BUFFER_SIZE);
2018-04-03 15:18:28 +00:00
// Also update display and bluetooth completely with the new data in the buffer
2018-03-18 22:43:59 +00:00
for(uint16_t i = 0; i < BUFFER_SIZE; i++) {
disp_send_gyro_data(bX[i], bY[i], bZ[i]);
bluetooth_send_gyro_data(bX[i], bY[i], bZ[i]);
}
}
}
}