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.
accelerometer.h
00001 #pragma once 00002 #ifndef ACCELEROMETER_H 00003 #define ACCELEROMETER_H 00004 00005 #include <processes.h> 00006 #include <Mutexes.h> 00007 #include <channels.h> 00008 #define X_axis p28 00009 #define Y_axis p27 00010 00011 //DigitalOut led3(LED3); //these LEDs are used to indicate when an interrupt occurs. 00012 //DigitalOut led4(LED4); 00013 00014 00015 class accelerometer { 00016 public: 00017 accelerometer() 00018 :X_int(X_axis), 00019 Y_int(Y_axis) 00020 //X_input(X_axis), 00021 //Y_input(Y_axis) 00022 { 00023 //led3 = led4 = 0; 00024 X_time = Y_time = 0; 00025 //enable_flag = false; 00026 enable_X_rising_flag = enable_X_falling_flag = enable_Y_rising_flag = enable_Y_falling_flag = false; 00027 X_int.mode(PullDown); 00028 Y_int.mode(PullDown); 00029 //Y_input.mode(PullDown); 00030 //X_input.mode(PullDown); 00031 X_timer.reset(); 00032 Y_timer.reset(); 00033 set_interrupts(); 00034 enable_X_rising_flag = enable_Y_rising_flag = true; 00035 } 00036 00037 00038 void set_interrupts() 00039 { 00040 X_int.fall(this, &accelerometer::set_X_time); 00041 X_int.rise(this, &accelerometer::start_X_timing); 00042 Y_int.fall(this, &accelerometer::set_Y_time); 00043 Y_int.rise(this, &accelerometer::start_Y_timing); 00044 } 00045 00046 void start_X_timing() 00047 { 00048 OS::TISRW ISRW; 00049 if(enable_X_rising_flag == true) 00050 { 00051 X_timer.start(); 00052 enable_X_falling_flag = true; 00053 //printf("In start_x_time().\n"); 00054 00055 } 00056 00057 00058 } 00059 void start_Y_timing() 00060 { 00061 OS::TISRW ISRW; 00062 if(enable_Y_rising_flag == true) 00063 { 00064 Y_timer.start(); 00065 enable_Y_falling_flag = true; 00066 00067 } 00068 00069 00070 } 00071 00072 void set_X_time() { 00073 if(enable_X_falling_flag == true) 00074 { 00075 OS::TISRW ISRW; 00076 X_timer.stop(); 00077 X_time = X_timer.read_us(); 00078 X_timer.reset(); 00079 // led3 = !led3; 00080 //printf("In set_x_time().\n"); 00081 00082 00083 } 00084 } 00085 00086 00087 void set_Y_time() { 00088 if(enable_Y_falling_flag == true) 00089 { 00090 OS::TISRW ISRW; 00091 Y_timer.stop(); 00092 Y_time = Y_timer.read_us(); 00093 Y_timer.reset(); 00094 // led4 = !led4; 00095 00096 00097 } 00098 } 00099 00100 float get_X_time() 00101 { 00102 return X_time; 00103 } 00104 00105 float get_Y_time() 00106 { 00107 return Y_time; 00108 } 00109 00110 00111 private: 00112 Timer X_timer; 00113 Timer Y_timer; 00114 float X_time; //time in us 00115 float Y_time; 00116 InterruptIn X_int; 00117 InterruptIn Y_int; 00118 //DigitalIn X_input; 00119 //DigitalIn Y_input; 00120 bool enable_X_rising_flag; 00121 bool enable_X_falling_flag; 00122 bool enable_Y_rising_flag; 00123 bool enable_Y_falling_flag; 00124 }; 00125 00126 00127 00128 00129 00130 00131 00132 00133 extern BusOut leds; 00134 00135 char ACC_Return_chars[100] = ""; 00136 00137 00138 //typedef OS::process<OS::pr2, 1400> Accelo_proc; 00139 template<> OS_PROCESS void Accelo_proc::Exec() //Output stream handling process 00140 { 00141 00142 accelerometer ACC; 00143 00144 byte M = 0; 00145 for(;;) 00146 { 00147 if(ACCELEROMETER_MESSAGE.wait(70)) //length of time to wait also determines how rapidly to return accelerometer data 00148 { leds = 0x2; 00149 M = ACCELEROMETER_MESSAGE; //read the message that was destined for this process 00150 ACCELEROMETER_MESSAGE.reset(); 00151 00152 if(M == 0) //then return x and y data once 00153 { 00154 sprintf(ACC_Return_chars, "ACC_X:%f\nACC_Y:%f\n", ACC.get_X_time(), ACC.get_Y_time()); // ready return string 00155 //if(!Ser_out_Mutex.IsLocked()) 00156 { 00157 //Ser_out_Mutex.Lock(); 00158 //if(TX_Channel.get_free_size() > strlen(Return_chars)+1) 00159 TX_channel.write(ACC_Return_chars, strlen(ACC_Return_chars)+1); //output the data! 00160 //Ser_out_Mutex.Unlock(); 00161 } 00162 00163 00164 00165 } 00166 } 00167 if(M == 1) //ticker 00168 { 00169 00170 { 00171 //TCritSect cs; 00172 sprintf(ACC_Return_chars, "ACC_X:%f\nACC_Y:%f\n", ACC.get_X_time(), ACC.get_Y_time()); // ready return string //"ACC_X:%f\nACC_Y:%f\n" 00173 } 00174 //if(!Ser_out_Mutex.IsLocked()) 00175 { 00176 //Ser_out_Mutex.Lock(); 00177 //if(TX_Channel.get_free_size() > strlen(Return_chars)+1) 00178 TX_channel.write(ACC_Return_chars, strlen(ACC_Return_chars)+1); //output the data! 00179 //Ser_out_Mutex.Unlock(); 00180 } 00181 //TX_flag.Signal(); 00182 00183 } 00184 00185 leds = 0x2; 00186 00187 00188 } 00189 } 00190 00191 00192 #endif 00193 00194 00195 00196 00197
Generated on Wed Jul 13 2022 05:20:16 by
1.7.2