setup_galileo.cpp 3.06 KB
Newer Older
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Implements specific functionnalities of a "Galileo board" scientific object

#include "handler.hpp"

//
// Galileo service handlers
//

#define PRESS_BUTTON 34

#define SENSOR_1 27
#define SENSOR_2 25
#define SENSOR_3 12
#define SENSOR_4 13

#define MAGNET_1 26

// Implements the on/off button mechamism

VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
20
unsigned int handle_galileo_off_last_time = 0;
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
21
22
23
24

void handle_galileo_button(bool on_else_off)
{
  verbose(on_else_off ? "handle_galileo_on_off(1)" : "handle_galileo_on_off(0)");
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
25
  if(!on_else_off) {
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
26
    handle_galileo_off_last_time = millis();
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
27
  }
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
28
29
  digitalWrite(MAGNET_1, on_else_off);
}
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
30
void handle_galileo_on()
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
31
{
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
32
33
34
35
36
37
  handle_galileo_button(true);
}
void IRAM_ATTR handle_galileo_run()
{
  handle_galileo_button(false);
  setInterval(handle_galileo_on, 1000, 1);
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
38
39
40
41
42
43
}
void handle_galileo_start()
{
  // Configures the magnet output
  {
    pinMode(MAGNET_1, OUTPUT);
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
44
    handle_galileo_on();
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
45
46
47
48
49
  }

  // Registers the on/off button mechamism
  {
    pinMode(PRESS_BUTTON, INPUT);
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
50
51
    attachInterrupt(digitalPinToInterrupt(PRESS_BUTTON), handle_galileo_run, RISING);
    handle_galileo_off_last_time = 0;
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
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
  }

  // Registers sensor input timing measure
  {
    gpio_digital_timing_start(SENSOR_1, RISING);
    gpio_digital_timing_start(SENSOR_2, RISING);
    gpio_digital_timing_start(SENSOR_3, RISING);
    gpio_digital_timing_start(SENSOR_4, RISING);
  }
}
void handle_galileo_stop()
{
  detachInterrupt(digitalPinToInterrupt(PRESS_BUTTON));

  gpio_digital_timing_stop(SENSOR_1);
  gpio_digital_timing_stop(SENSOR_2);
  gpio_digital_timing_stop(SENSOR_3);
  gpio_digital_timing_stop(SENSOR_4);

  digitalWrite(MAGNET_1, 0);
}
void hande_galileo_get()
{
  // Converts in seconds and skip negative values
  double d[5] = {
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
77
78
79
80
81
    0.001 * handle_galileo_off_last_time,
    0.001 * ((int) gpio_digital_timing_get(SENSOR_1) - (int) handle_galileo_off_last_time),
    0.001 * ((int) gpio_digital_timing_get(SENSOR_2) - (int) handle_galileo_off_last_time),
    0.001 * ((int) gpio_digital_timing_get(SENSOR_3) - (int) handle_galileo_off_last_time),
    0.001 * ((int) gpio_digital_timing_get(SENSOR_4) - (int) handle_galileo_off_last_time)
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
82
83
84
85
86
  };
  for(unsigned int i = 0; i < 5; i++) {
    d[i] = MAX(0, d[i]);
  }
  //
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
87
  answer(true, "{\"route\": \"galileo\", \"time\": %.3f, \"start_time\": %.3f, \"detected_times\": [%.3f, %.3f, %.3f, %.3f], \"detected_time_differences\": [%.3f, %.3f, %.3f], \"detected_time_difference_variations\": [%.5f, %.5f]}", fmillis(), d[0], d[1], d[2], d[3], d[4], d[2] - d[1], d[3] - d[2], d[4] - d[3],
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
88
         2 * d[2] - d[1] - d[3], 2 * d[3] - d[2] - d[4]);
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
}
//
// Service configuration
//

void handle_galileo()
{
  String action = server->arg("action");
  if(action == "start") {
    handle_galileo_start();
  } else if(action == "run") {
    handle_galileo_run();
  } else if(action == "stop") {
    handle_galileo_stop();
  }
  hande_galileo_get();
}
void handle_galileo_page()
{
#include "setup_galileo_page.hpp"
  server->send(200, "text/html", setup_galileo_page);
}
void setup_galileo_service()
{
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
113
  server->on("/", HTTP_GET, handle_galileo_page);
VIEVILLE Thierry's avatar
VIEVILLE Thierry committed
114
115
  server->on("/galileo", HTTP_POST, handle_galileo);
}