Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:e8a1398d13f1, 2019-02-06 (annotated)
- Committer:
- JackSkia
- Date:
- Wed Feb 06 10:32:17 2019 +0000
- Revision:
- 0:e8a1398d13f1
Smart device for vibration detect
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JackSkia | 0:e8a1398d13f1 | 1 | #include "mbed.h" |
JackSkia | 0:e8a1398d13f1 | 2 | #include "SimpleSpirit1.h" |
JackSkia | 0:e8a1398d13f1 | 3 | |
JackSkia | 0:e8a1398d13f1 | 4 | // Rf variables |
JackSkia | 0:e8a1398d13f1 | 5 | |
JackSkia | 0:e8a1398d13f1 | 6 | static uint8_t txDataXYZ[6]; |
JackSkia | 0:e8a1398d13f1 | 7 | static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(PC_12,PC_11, PC_10, PE_5, PB_5, PB_15); /* mosi,miso,sclk,irq,cs,sdn*/ |
JackSkia | 0:e8a1398d13f1 | 8 | static volatile bool tx_done_flag = false; |
JackSkia | 0:e8a1398d13f1 | 9 | static volatile bool send_data_flag = false; |
JackSkia | 0:e8a1398d13f1 | 10 | static InterruptIn event(PA_2); |
JackSkia | 0:e8a1398d13f1 | 11 | |
JackSkia | 0:e8a1398d13f1 | 12 | // Sensor variables |
JackSkia | 0:e8a1398d13f1 | 13 | |
JackSkia | 0:e8a1398d13f1 | 14 | SPI device(D11, D12, D13); // mosi, miso, sclk,ssel |
JackSkia | 0:e8a1398d13f1 | 15 | Serial pc(SERIAL_TX, SERIAL_RX); |
JackSkia | 0:e8a1398d13f1 | 16 | DigitalOut cs (PB_2); |
JackSkia | 0:e8a1398d13f1 | 17 | |
JackSkia | 0:e8a1398d13f1 | 18 | static bool flag=0; |
JackSkia | 0:e8a1398d13f1 | 19 | static uint16_t new_data=0; |
JackSkia | 0:e8a1398d13f1 | 20 | static uint16_t in_threshold=0x1e0f; // inactivity threshold level |
JackSkia | 0:e8a1398d13f1 | 21 | static uint16_t dur_threshold=0x1f0f; |
JackSkia | 0:e8a1398d13f1 | 22 | static uint16_t ctrl_1=0x203f; |
JackSkia | 0:e8a1398d13f1 | 23 | static uint16_t ctrl_2=0x2100; //HP filter disable |
JackSkia | 0:e8a1398d13f1 | 24 | static uint16_t ctrl_3=0x2208; |
JackSkia | 0:e8a1398d13f1 | 25 | static uint16_t ctrl_4=0x2304; |
JackSkia | 0:e8a1398d13f1 | 26 | static uint16_t ctrl_5=0x2400; |
JackSkia | 0:e8a1398d13f1 | 27 | //static unsigned char add_ctrl_6=0x2500; // INT2 |
JackSkia | 0:e8a1398d13f1 | 28 | static uint16_t ctrl_7=0x2604; //Interrupt lached |
JackSkia | 0:e8a1398d13f1 | 29 | static uint16_t status=0x2700; |
JackSkia | 0:e8a1398d13f1 | 30 | static uint16_t out_x_l=0x2800; |
JackSkia | 0:e8a1398d13f1 | 31 | static uint16_t out_x_h=0x2900; |
JackSkia | 0:e8a1398d13f1 | 32 | static uint16_t out_y_l=0x2a00; |
JackSkia | 0:e8a1398d13f1 | 33 | static uint16_t out_y_h=0x2b00; |
JackSkia | 0:e8a1398d13f1 | 34 | static uint16_t out_z_l=0x2c00; |
JackSkia | 0:e8a1398d13f1 | 35 | static uint16_t out_z_h=0x2d00; |
JackSkia | 0:e8a1398d13f1 | 36 | static uint16_t fifo_ctrl=0x2e40; //FIFO configuration |
JackSkia | 0:e8a1398d13f1 | 37 | //static uint16_t fifo_src=0x2f00; //FIFO status register |
JackSkia | 0:e8a1398d13f1 | 38 | static uint16_t ig_conf_1=0x300a; //interrupt 1 configuration |
JackSkia | 0:e8a1398d13f1 | 39 | static uint16_t ig_src_1=0x3100; //Interrupt Status Register |
JackSkia | 0:e8a1398d13f1 | 40 | static uint16_t threshold_x=0x3220; |
JackSkia | 0:e8a1398d13f1 | 41 | static uint16_t threshold_y=0x3320; |
JackSkia | 0:e8a1398d13f1 | 42 | static uint16_t threshold_z=0x3420; //threshold level |
JackSkia | 0:e8a1398d13f1 | 43 | static uint16_t ig_dur_1=0x3504; //interrupt duration |
JackSkia | 0:e8a1398d13f1 | 44 | //static unsigned char ig_conf_2=0x36; |
JackSkia | 0:e8a1398d13f1 | 45 | |
JackSkia | 0:e8a1398d13f1 | 46 | static uint32_t coda=0; |
JackSkia | 0:e8a1398d13f1 | 47 | |
JackSkia | 0:e8a1398d13f1 | 48 | static InterruptIn event1(PA_2); |
JackSkia | 0:e8a1398d13f1 | 49 | |
JackSkia | 0:e8a1398d13f1 | 50 | // Rf function |
JackSkia | 0:e8a1398d13f1 | 51 | |
JackSkia | 0:e8a1398d13f1 | 52 | static void send_data(void) //Funzione per mandare dati |
JackSkia | 0:e8a1398d13f1 | 53 | { |
JackSkia | 0:e8a1398d13f1 | 54 | while(myspirit.is_receiving()); |
JackSkia | 0:e8a1398d13f1 | 55 | |
JackSkia | 0:e8a1398d13f1 | 56 | size_t curr_len = strlen((const char*)txDataXYZ) + 1; |
JackSkia | 0:e8a1398d13f1 | 57 | myspirit.send(txDataXYZ, 6); |
JackSkia | 0:e8a1398d13f1 | 58 | } |
JackSkia | 0:e8a1398d13f1 | 59 | |
JackSkia | 0:e8a1398d13f1 | 60 | // Sensor function |
JackSkia | 0:e8a1398d13f1 | 61 | |
JackSkia | 0:e8a1398d13f1 | 62 | static void toggle_on() { //Inizializzazione evento |
JackSkia | 0:e8a1398d13f1 | 63 | printf("Event Start\n"); |
JackSkia | 0:e8a1398d13f1 | 64 | flag=1; |
JackSkia | 0:e8a1398d13f1 | 65 | coda=0; |
JackSkia | 0:e8a1398d13f1 | 66 | } |
JackSkia | 0:e8a1398d13f1 | 67 | |
JackSkia | 0:e8a1398d13f1 | 68 | static void paket_send(uint16_t a){ //Funzione utilizzata per scrivere i dati nei registri del sensore |
JackSkia | 0:e8a1398d13f1 | 69 | cs=0; |
JackSkia | 0:e8a1398d13f1 | 70 | device.write(a); |
JackSkia | 0:e8a1398d13f1 | 71 | cs=1; |
JackSkia | 0:e8a1398d13f1 | 72 | } |
JackSkia | 0:e8a1398d13f1 | 73 | |
JackSkia | 0:e8a1398d13f1 | 74 | static unsigned char read_data(uint16_t a){ //Funzione utilizzata per leggere i dati nei registri del sensore |
JackSkia | 0:e8a1398d13f1 | 75 | unsigned char temp_data; |
JackSkia | 0:e8a1398d13f1 | 76 | uint16_t temp_add; |
JackSkia | 0:e8a1398d13f1 | 77 | temp_add=0x8000+a; |
JackSkia | 0:e8a1398d13f1 | 78 | cs=0; |
JackSkia | 0:e8a1398d13f1 | 79 | temp_data=device.write(temp_add); |
JackSkia | 0:e8a1398d13f1 | 80 | cs=1; |
JackSkia | 0:e8a1398d13f1 | 81 | return temp_data; |
JackSkia | 0:e8a1398d13f1 | 82 | } |
JackSkia | 0:e8a1398d13f1 | 83 | |
JackSkia | 0:e8a1398d13f1 | 84 | // Main |
JackSkia | 0:e8a1398d13f1 | 85 | |
JackSkia | 0:e8a1398d13f1 | 86 | int main() |
JackSkia | 0:e8a1398d13f1 | 87 | { |
JackSkia | 0:e8a1398d13f1 | 88 | event1.rise(&toggle_on); |
JackSkia | 0:e8a1398d13f1 | 89 | device.format(16,3); |
JackSkia | 0:e8a1398d13f1 | 90 | |
JackSkia | 0:e8a1398d13f1 | 91 | cs=1; |
JackSkia | 0:e8a1398d13f1 | 92 | paket_send(in_threshold); |
JackSkia | 0:e8a1398d13f1 | 93 | paket_send(dur_threshold); |
JackSkia | 0:e8a1398d13f1 | 94 | paket_send(ctrl_1); |
JackSkia | 0:e8a1398d13f1 | 95 | paket_send(ctrl_2); |
JackSkia | 0:e8a1398d13f1 | 96 | paket_send(ctrl_3); |
JackSkia | 0:e8a1398d13f1 | 97 | paket_send(ctrl_4); |
JackSkia | 0:e8a1398d13f1 | 98 | paket_send(ctrl_5); |
JackSkia | 0:e8a1398d13f1 | 99 | paket_send(ctrl_7); |
JackSkia | 0:e8a1398d13f1 | 100 | paket_send(threshold_x); |
JackSkia | 0:e8a1398d13f1 | 101 | paket_send(threshold_y); |
JackSkia | 0:e8a1398d13f1 | 102 | paket_send(threshold_z); |
JackSkia | 0:e8a1398d13f1 | 103 | paket_send(fifo_ctrl); |
JackSkia | 0:e8a1398d13f1 | 104 | paket_send(ig_dur_1); |
JackSkia | 0:e8a1398d13f1 | 105 | paket_send(ig_conf_1); |
JackSkia | 0:e8a1398d13f1 | 106 | |
JackSkia | 0:e8a1398d13f1 | 107 | while(1) |
JackSkia | 0:e8a1398d13f1 | 108 | { //sleep(); |
JackSkia | 0:e8a1398d13f1 | 109 | hal_deepsleep(); |
JackSkia | 0:e8a1398d13f1 | 110 | //printf("Stand-by\n"); |
JackSkia | 0:e8a1398d13f1 | 111 | //__WFI(); // Funzione Wait For an Interrupt |
JackSkia | 0:e8a1398d13f1 | 112 | myspirit.on(); // Accendo antenna |
JackSkia | 0:e8a1398d13f1 | 113 | while(flag){ |
JackSkia | 0:e8a1398d13f1 | 114 | //myspirit.on(); // Lettura Interrupt |
JackSkia | 0:e8a1398d13f1 | 115 | new_data=read_data(status); // Lettura stato |
JackSkia | 0:e8a1398d13f1 | 116 | new_data=new_data&0x08; // Lettura bit new data |
JackSkia | 0:e8a1398d13f1 | 117 | if(new_data==0x08){ |
JackSkia | 0:e8a1398d13f1 | 118 | txDataXYZ[0]=read_data(out_x_h); |
JackSkia | 0:e8a1398d13f1 | 119 | txDataXYZ[3]=read_data(out_x_l); |
JackSkia | 0:e8a1398d13f1 | 120 | txDataXYZ[1]=read_data(out_y_h); |
JackSkia | 0:e8a1398d13f1 | 121 | txDataXYZ[4]=read_data(out_y_l); |
JackSkia | 0:e8a1398d13f1 | 122 | txDataXYZ[2]=read_data(out_z_h); |
JackSkia | 0:e8a1398d13f1 | 123 | txDataXYZ[5]=read_data(out_z_l); |
JackSkia | 0:e8a1398d13f1 | 124 | //send_data_flag = false; |
JackSkia | 0:e8a1398d13f1 | 125 | send_data(); // Invio Dati |
JackSkia | 0:e8a1398d13f1 | 126 | coda++; |
JackSkia | 0:e8a1398d13f1 | 127 | if (coda>500){ // Condizione uscita invio dati |
JackSkia | 0:e8a1398d13f1 | 128 | read_data(ig_src_1); |
JackSkia | 0:e8a1398d13f1 | 129 | flag=false; |
JackSkia | 0:e8a1398d13f1 | 130 | printf("Event Done\n"); |
JackSkia | 0:e8a1398d13f1 | 131 | } |
JackSkia | 0:e8a1398d13f1 | 132 | } |
JackSkia | 0:e8a1398d13f1 | 133 | } |
JackSkia | 0:e8a1398d13f1 | 134 | myspirit.off(); // Spegnimento antenna |
JackSkia | 0:e8a1398d13f1 | 135 | } |
JackSkia | 0:e8a1398d13f1 | 136 | } |