Test with EzSbc2 LPC1347 board and Adafruit 0,96" display, connected to i2c
Dependencies: Adafruit_GFX I2CDevLib MPU9150 SmallGUI USBDevice WeeESP8266 mbed nRF24L01P
main.cpp
00001 #include "mbed.h" 00002 #include "Adafruit_SSD1306.h" 00003 #include "MPU9150.h" 00004 #include "USBSerial.h" 00005 #include "Controls.h" 00006 #include "nRF24L01P.h" 00007 00008 00009 00010 // #define USE_USBSERIAL 00011 // #define USE_NRF 00012 00013 // modified für EzSBC2 00014 00015 DigitalOut ledRed(P1_16); 00016 DigitalOut ledGreen(P1_15); 00017 00018 float sum = 0; 00019 uint32_t sumCount = 0; 00020 char buffer[14]; 00021 00022 Timer t; 00023 #ifdef USE_USBSERIAL 00024 USBSerial pc; 00025 #endif 00026 00027 00028 // an I2C sub-class that provides a constructed default 00029 class I2C2 : public I2C 00030 { 00031 public: 00032 I2C2(PinName sda, PinName scl) : I2C(sda, scl) { 00033 frequency(400000); 00034 start(); 00035 }; 00036 }; 00037 00038 #ifdef USE_NRF 00039 nRF24L01P my_nrf24l01p(P0_9, P0_8, P1_29, P0_11, P0_7, P0_17); // mosi, miso, sck, csn, ce, irq 00040 #endif 00041 00042 I2C2 gI2C(P0_5, P0_4); 00043 Adafruit_SSD1306_I2c gOled2(gI2C, NC, 0x78, 64, 128); 00044 00045 I2Cdev i2Cdev; 00046 MPU9150 mpu(i2Cdev); 00047 00048 #define TRANSFER_SIZE 4 00049 00050 int main() 00051 { 00052 uint16_t x=0; 00053 00054 uint16_t txData[2]; 00055 00056 00057 #ifdef USE_NRF 00058 my_nrf24l01p.powerUp(); 00059 00060 // Display the (default) setup of the nRF24L01+ chip 00061 //pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); 00062 //pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); 00063 //pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); 00064 //pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); 00065 //pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); 00066 00067 //pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); 00068 00069 my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); 00070 00071 //my_nrf24l01p.setReceiveMode(); 00072 my_nrf24l01p.setTransmitMode(); 00073 my_nrf24l01p.enable(); 00074 #endif 00075 00076 //while(1) 00077 { 00078 ledRed = 1; 00079 ledGreen = 0; 00080 wait(0.5); 00081 00082 ledRed = 0; 00083 ledGreen = 1; 00084 wait(0.5); 00085 } 00086 00087 int16_t y = 12; 00088 int16_t ySize = 4; 00089 int16_t yGap = 2; 00090 00091 float scaleMin = SHRT_MIN * 0.5f; // fullscale = -1g 00092 float scaleMax = SHRT_MAX * 0.5f; // fullscale = +1g 00093 00094 Canvas canvasBG(gOled2); 00095 barGraph bgAx(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00096 canvasBG.addControl(bgAx); 00097 bgAx.setScale(scaleMin, scaleMax); 00098 y = y + ySize + yGap; 00099 00100 barGraph bgAy(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00101 canvasBG.addControl(bgAy); 00102 bgAy.setScale(scaleMin, scaleMax); 00103 y = y + ySize + yGap; 00104 00105 barGraph bgAz(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00106 canvasBG.addControl(bgAz); 00107 bgAz.setScale(scaleMin, scaleMax); 00108 y = y + ySize + yGap; 00109 00110 barGraph bgGx(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00111 canvasBG.addControl(bgGx); 00112 bgGx.setScale(scaleMin, scaleMax); 00113 y = y + ySize + yGap; 00114 00115 barGraph bgGy(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00116 canvasBG.addControl(bgGy); 00117 bgGy.setScale(scaleMin, scaleMax); 00118 y = y + ySize + yGap; 00119 00120 barGraph bgGz(barGraph::bgHorizontal, 0, y, 127, y+ySize); 00121 canvasBG.addControl(bgGz); 00122 bgGz.setScale(scaleMin, scaleMax); 00123 00124 gOled2.clearDisplay(); 00125 gOled2.display(); 00126 00127 //gOled2.setTextCursor(1, 0); 00128 gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height()); 00129 00130 #ifdef USE_USBSERIAL 00131 pc.printf("MPU6050 test\n\n"); 00132 pc.printf("MPU6050 initialize \n"); 00133 #endif 00134 00135 int16_t ax, ay, az; 00136 int16_t gx, gy, gz; 00137 00138 mpu.initialize(); 00139 00140 #ifdef USE_USBSERIAL 00141 pc.printf("MPU6050 testConnection \n"); 00142 #endif 00143 00144 bool mpu6050TestResult = mpu.testConnection(); 00145 00146 #ifdef USE_USBSERIAL 00147 if(mpu6050TestResult) { 00148 pc.printf("MPU6050 test passed \n"); 00149 } else { 00150 pc.printf("MPU6050 test failed \n"); 00151 } 00152 #endif 00153 00154 char buffer[32]; 00155 while(1) { 00156 ledRed = !ledRed; 00157 gOled2.clearDisplay(); 00158 00159 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 00160 //writing current accelerometer and gyro position 00161 //pc.printf("%d;%d;%d;%d;%d;%d\n",ax,ay,az,gx,gy,gz); 00162 00163 if (0) { 00164 sprintf(buffer, "%i %i %i", ax, ay, az); 00165 } 00166 00167 #ifdef USE_NRF 00168 // Send the transmitbuffer via the nRF24L01+ 00169 txData[0] = ax; 00170 txData[1] = ay; 00171 my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)txData, TRANSFER_SIZE ); 00172 #endif 00173 00174 00175 gOled2.setTextCursor(0, 0); 00176 //gOled2.printf("wifi ok: %u\r", wifiOk); 00177 00178 bgAx.setValue(ax); 00179 bgAy.setValue(ay); 00180 bgAz.setValue(az); 00181 00182 bgGx.setValue(gx); 00183 bgGy.setValue(gy); 00184 bgGz.setValue(gz); 00185 00186 canvasBG.draw(); 00187 00188 gOled2.display(); 00189 00190 x++; 00191 //wait(0.01); 00192 } 00193 }
Generated on Wed Jul 20 2022 22:53:44 by 1.7.2