
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@1:9bbb606da2d0, 2016-08-26 (annotated)
- Committer:
- Capper
- Date:
- Fri Aug 26 12:24:29 2016 +0000
- Revision:
- 1:9bbb606da2d0
- Parent:
- 0:d7a9c9f2bcc1
Added some blank screen time to avoid OLED image burn
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 | 1:9bbb606da2d0 | 17 | int count2 = 0; |
Capper | 0:d7a9c9f2bcc1 | 18 | extern const uint8_t Main_screen_bmp[ 36864 ]; |
Capper | 0:d7a9c9f2bcc1 | 19 | int argb = 0xff000000; |
Capper | 0:d7a9c9f2bcc1 | 20 | int rgbd = 0x00000000; |
Capper | 0:d7a9c9f2bcc1 | 21 | int xposg = 0; |
Capper | 0:d7a9c9f2bcc1 | 22 | int yposg = 0; |
Capper | 0:d7a9c9f2bcc1 | 23 | int main() { |
Capper | 0:d7a9c9f2bcc1 | 24 | led1 = 1; |
Capper | 0:d7a9c9f2bcc1 | 25 | led2 = 1; |
Capper | 0:d7a9c9f2bcc1 | 26 | led3 = 1; |
Capper | 0:d7a9c9f2bcc1 | 27 | // Configure Accelerometer FXOS8700 |
Capper | 0:d7a9c9f2bcc1 | 28 | accel.accel_config(); |
Capper | 0:d7a9c9f2bcc1 | 29 | BOOSTEN = 1; |
Capper | 0:d7a9c9f2bcc1 | 30 | OLED96x96.open(); |
Capper | 0:d7a9c9f2bcc1 | 31 | OLED96x96.state(Display::DISPLAY_ON); |
Capper | 0:d7a9c9f2bcc1 | 32 | OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR |
Capper | 0:d7a9c9f2bcc1 | 33 | for(int xpos=0;xpos<97;xpos++) |
Capper | 0:d7a9c9f2bcc1 | 34 | { |
Capper | 0:d7a9c9f2bcc1 | 35 | for(int ypos=0;ypos<96;ypos++) |
Capper | 0:d7a9c9f2bcc1 | 36 | { |
Capper | 0:d7a9c9f2bcc1 | 37 | 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 | 38 | OLED96x96.drawPixel(ypos+16,xpos,rgbd); |
Capper | 0:d7a9c9f2bcc1 | 39 | count=count+4; |
Capper | 0:d7a9c9f2bcc1 | 40 | } |
Capper | 0:d7a9c9f2bcc1 | 41 | } |
Capper | 0:d7a9c9f2bcc1 | 42 | OLED96x96.drawCircle(61,47,4,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 43 | OLED96x96.drawCircle(61,47,3,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 44 | OLED96x96.drawCircle(61,47,2,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 45 | while(1) |
Capper | 0:d7a9c9f2bcc1 | 46 | { |
Capper | 0:d7a9c9f2bcc1 | 47 | accel.acquire_accel_data_g(accel_data); |
Capper | 0:d7a9c9f2bcc1 | 48 | xposg=(accel_data[1]*(-40.0)); |
Capper | 0:d7a9c9f2bcc1 | 49 | if (xposg > 27) |
Capper | 0:d7a9c9f2bcc1 | 50 | xposg = 27; |
Capper | 0:d7a9c9f2bcc1 | 51 | if (xposg < -27) |
Capper | 0:d7a9c9f2bcc1 | 52 | xposg = -27; |
Capper | 0:d7a9c9f2bcc1 | 53 | yposg=accel_data[0]*40.0; |
Capper | 0:d7a9c9f2bcc1 | 54 | if (yposg > 27) |
Capper | 0:d7a9c9f2bcc1 | 55 | yposg = 27; |
Capper | 0:d7a9c9f2bcc1 | 56 | if (yposg < -27) |
Capper | 0:d7a9c9f2bcc1 | 57 | yposg = -27; |
Capper | 0:d7a9c9f2bcc1 | 58 | if((xposg == 0) and (yposg == 0)) |
Capper | 0:d7a9c9f2bcc1 | 59 | { |
Capper | 0:d7a9c9f2bcc1 | 60 | led1 = 1; |
Capper | 0:d7a9c9f2bcc1 | 61 | led2 = 0; |
Capper | 0:d7a9c9f2bcc1 | 62 | } |
Capper | 0:d7a9c9f2bcc1 | 63 | OLED96x96.drawCircle(61,47,8,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 64 | OLED96x96.drawLine(61,7,61,87,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 65 | OLED96x96.drawLine(21,47,101,47,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 66 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 67 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 68 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 69 | Thread::wait(20); |
Capper | 0:d7a9c9f2bcc1 | 70 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 71 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 72 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 73 | led1 = 0; |
Capper | 0:d7a9c9f2bcc1 | 74 | led2 = 1; |
Capper | 1:9bbb606da2d0 | 75 | count2++; |
Capper | 1:9bbb606da2d0 | 76 | if (count2 > 100) |
Capper | 1:9bbb606da2d0 | 77 | { //blank display to avoid image burn |
Capper | 1:9bbb606da2d0 | 78 | OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR |
Capper | 1:9bbb606da2d0 | 79 | count2 = 0; |
Capper | 1:9bbb606da2d0 | 80 | } |
Capper | 1:9bbb606da2d0 | 81 | //BOOSTEN = 0; //turn off display after about 1 minute to avoid image burn |
Capper | 0:d7a9c9f2bcc1 | 82 | } |
Capper | 0:d7a9c9f2bcc1 | 83 | |
Capper | 0:d7a9c9f2bcc1 | 84 | } |
Capper | 0:d7a9c9f2bcc1 | 85 |