![](/media/cache/group/hexiweare_logo_main_black.jpg.50x50_q85.jpg)
HexiWear Bubble level program
Dependencies: FXOS8700 NeatGUI
This program uses the accelerometer on the Hexiwear device to simulate a bubble level tool. When the X and Y accelerometer values are both zero, the HexiWear LED changes from red to green. Enjoy,
main.cpp@0:d7a9c9f2bcc1, 2016-08-18 (annotated)
- Committer:
- Capper
- Date:
- Thu Aug 18 20:48:07 2016 +0000
- Revision:
- 0:d7a9c9f2bcc1
- Child:
- 1:9bbb606da2d0
HexiWear Bubble level using FXOS8700 accelerometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Capper | 0:d7a9c9f2bcc1 | 1 | #include "mbed.h" |
Capper | 0:d7a9c9f2bcc1 | 2 | #include <SSD1351_SPI.h> |
Capper | 0:d7a9c9f2bcc1 | 3 | #include <HexiBubble.h> |
Capper | 0:d7a9c9f2bcc1 | 4 | #include "FXOS8700.h" |
Capper | 0:d7a9c9f2bcc1 | 5 | |
Capper | 0:d7a9c9f2bcc1 | 6 | //SSD1351_SPI ( mosi, miso, sclk, cs, dc ) ; |
Capper | 0:d7a9c9f2bcc1 | 7 | SSD1351_SPI OLED96x96(PTB22,PTB23,PTB21,PTB20,PTD15); |
Capper | 0:d7a9c9f2bcc1 | 8 | // Pin connections for Hexiwear |
Capper | 0:d7a9c9f2bcc1 | 9 | FXOS8700 accel(PTC11, PTC10); |
Capper | 0:d7a9c9f2bcc1 | 10 | // Storage for the data from the sensor |
Capper | 0:d7a9c9f2bcc1 | 11 | float accel_data[3]; float accel_rms=0.0; |
Capper | 0:d7a9c9f2bcc1 | 12 | DigitalOut led1(LED1); |
Capper | 0:d7a9c9f2bcc1 | 13 | DigitalOut led2(LED2); |
Capper | 0:d7a9c9f2bcc1 | 14 | DigitalOut led3(LED3); |
Capper | 0:d7a9c9f2bcc1 | 15 | DigitalOut BOOSTEN(PTC13); //oled power enable |
Capper | 0:d7a9c9f2bcc1 | 16 | int count = 0; |
Capper | 0:d7a9c9f2bcc1 | 17 | extern const uint8_t Main_screen_bmp[ 36864 ]; |
Capper | 0:d7a9c9f2bcc1 | 18 | int argb = 0xff000000; |
Capper | 0:d7a9c9f2bcc1 | 19 | int rgbd = 0x00000000; |
Capper | 0:d7a9c9f2bcc1 | 20 | int xposg = 0; |
Capper | 0:d7a9c9f2bcc1 | 21 | int yposg = 0; |
Capper | 0:d7a9c9f2bcc1 | 22 | int main() { |
Capper | 0:d7a9c9f2bcc1 | 23 | led1 = 1; |
Capper | 0:d7a9c9f2bcc1 | 24 | led2 = 1; |
Capper | 0:d7a9c9f2bcc1 | 25 | led3 = 1; |
Capper | 0:d7a9c9f2bcc1 | 26 | // Configure Accelerometer FXOS8700 |
Capper | 0:d7a9c9f2bcc1 | 27 | accel.accel_config(); |
Capper | 0:d7a9c9f2bcc1 | 28 | BOOSTEN = 1; |
Capper | 0:d7a9c9f2bcc1 | 29 | OLED96x96.open(); |
Capper | 0:d7a9c9f2bcc1 | 30 | OLED96x96.state(Display::DISPLAY_ON); |
Capper | 0:d7a9c9f2bcc1 | 31 | OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR |
Capper | 0:d7a9c9f2bcc1 | 32 | for(int xpos=0;xpos<97;xpos++) |
Capper | 0:d7a9c9f2bcc1 | 33 | { |
Capper | 0:d7a9c9f2bcc1 | 34 | for(int ypos=0;ypos<96;ypos++) |
Capper | 0:d7a9c9f2bcc1 | 35 | { |
Capper | 0:d7a9c9f2bcc1 | 36 | rgbd = ((Main_screen_bmp[count+3]<<24)|(Main_screen_bmp[count]<<16)|(Main_screen_bmp[count+1]<<8)|(Main_screen_bmp[count+2])); |
Capper | 0:d7a9c9f2bcc1 | 37 | OLED96x96.drawPixel(ypos+16,xpos,rgbd); |
Capper | 0:d7a9c9f2bcc1 | 38 | count=count+4; |
Capper | 0:d7a9c9f2bcc1 | 39 | } |
Capper | 0:d7a9c9f2bcc1 | 40 | } |
Capper | 0:d7a9c9f2bcc1 | 41 | OLED96x96.drawCircle(61,47,4,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 42 | OLED96x96.drawCircle(61,47,3,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 43 | OLED96x96.drawCircle(61,47,2,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 44 | while(1) |
Capper | 0:d7a9c9f2bcc1 | 45 | { |
Capper | 0:d7a9c9f2bcc1 | 46 | accel.acquire_accel_data_g(accel_data); |
Capper | 0:d7a9c9f2bcc1 | 47 | xposg=(accel_data[1]*(-40.0)); |
Capper | 0:d7a9c9f2bcc1 | 48 | if (xposg > 27) |
Capper | 0:d7a9c9f2bcc1 | 49 | xposg = 27; |
Capper | 0:d7a9c9f2bcc1 | 50 | if (xposg < -27) |
Capper | 0:d7a9c9f2bcc1 | 51 | xposg = -27; |
Capper | 0:d7a9c9f2bcc1 | 52 | yposg=accel_data[0]*40.0; |
Capper | 0:d7a9c9f2bcc1 | 53 | if (yposg > 27) |
Capper | 0:d7a9c9f2bcc1 | 54 | yposg = 27; |
Capper | 0:d7a9c9f2bcc1 | 55 | if (yposg < -27) |
Capper | 0:d7a9c9f2bcc1 | 56 | yposg = -27; |
Capper | 0:d7a9c9f2bcc1 | 57 | if((xposg == 0) and (yposg == 0)) |
Capper | 0:d7a9c9f2bcc1 | 58 | { |
Capper | 0:d7a9c9f2bcc1 | 59 | led1 = 1; |
Capper | 0:d7a9c9f2bcc1 | 60 | led2 = 0; |
Capper | 0:d7a9c9f2bcc1 | 61 | } |
Capper | 0:d7a9c9f2bcc1 | 62 | OLED96x96.drawCircle(61,47,8,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 63 | OLED96x96.drawLine(61,7,61,87,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 64 | OLED96x96.drawLine(21,47,101,47,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 65 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 66 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 67 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 68 | Thread::wait(20); |
Capper | 0:d7a9c9f2bcc1 | 69 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 70 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 71 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 72 | led1 = 0; |
Capper | 0:d7a9c9f2bcc1 | 73 | led2 = 1; |
Capper | 0:d7a9c9f2bcc1 | 74 | } |
Capper | 0:d7a9c9f2bcc1 | 75 | |
Capper | 0:d7a9c9f2bcc1 | 76 | } |
Capper | 0:d7a9c9f2bcc1 | 77 |