LCD Projection of the Values of the compass.
Dependencies: BSP_DISCO_L476VG COMPASS_DISCO_L476VG LCD_DISCO_L476VG mbed
Fork of DISCO_L476VG_Compass by
main.cpp@4:b5850ba75275, 2018-04-26 (annotated)
- Committer:
- SpoiledAsparagus
- Date:
- Thu Apr 26 20:43:06 2018 +0000
- Revision:
- 4:b5850ba75275
- Parent:
- 0:211cb2effe6e
Compass LCD visuals.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:211cb2effe6e | 1 | #include "mbed.h" |
bcostm | 0:211cb2effe6e | 2 | #include "COMPASS_DISCO_L476VG.h" |
SpoiledAsparagus | 4:b5850ba75275 | 3 | #include "LCD_DISCO_L476VG.h" |
SpoiledAsparagus | 4:b5850ba75275 | 4 | #define USE_PRINTF (1) |
bcostm | 0:211cb2effe6e | 5 | |
SpoiledAsparagus | 4:b5850ba75275 | 6 | uint32_t temp = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 7 | uint8_t stemp[7] = {0}; |
SpoiledAsparagus | 4:b5850ba75275 | 8 | |
SpoiledAsparagus | 4:b5850ba75275 | 9 | LCD_DISCO_L476VG lcd; |
bcostm | 0:211cb2effe6e | 10 | COMPASS_DISCO_L476VG compass; |
SpoiledAsparagus | 4:b5850ba75275 | 11 | // LEDs |
SpoiledAsparagus | 4:b5850ba75275 | 12 | DigitalOut led_green(LED1); |
SpoiledAsparagus | 4:b5850ba75275 | 13 | DigitalOut led_red(LED2); |
bcostm | 0:211cb2effe6e | 14 | |
bcostm | 0:211cb2effe6e | 15 | DigitalOut led1(LED1); |
bcostm | 0:211cb2effe6e | 16 | |
SpoiledAsparagus | 4:b5850ba75275 | 17 | // Joystick button |
SpoiledAsparagus | 4:b5850ba75275 | 18 | InterruptIn center(JOYSTICK_CENTER); |
SpoiledAsparagus | 4:b5850ba75275 | 19 | InterruptIn left(JOYSTICK_LEFT); |
SpoiledAsparagus | 4:b5850ba75275 | 20 | InterruptIn right(JOYSTICK_RIGHT); |
SpoiledAsparagus | 4:b5850ba75275 | 21 | InterruptIn up(JOYSTICK_UP); |
SpoiledAsparagus | 4:b5850ba75275 | 22 | InterruptIn down(JOYSTICK_DOWN); |
SpoiledAsparagus | 4:b5850ba75275 | 23 | |
SpoiledAsparagus | 4:b5850ba75275 | 24 | int16_t MagBuffer_Global[3]; |
SpoiledAsparagus | 4:b5850ba75275 | 25 | int16_t AccBuffer_Global[3]; |
SpoiledAsparagus | 4:b5850ba75275 | 26 | |
SpoiledAsparagus | 4:b5850ba75275 | 27 | void center_released() { |
SpoiledAsparagus | 4:b5850ba75275 | 28 | led_green = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 29 | lcd.Clear(); |
SpoiledAsparagus | 4:b5850ba75275 | 30 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 31 | printf("center button released\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 32 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 33 | } |
SpoiledAsparagus | 4:b5850ba75275 | 34 | |
SpoiledAsparagus | 4:b5850ba75275 | 35 | |
SpoiledAsparagus | 4:b5850ba75275 | 36 | void center_pressed() { |
SpoiledAsparagus | 4:b5850ba75275 | 37 | sprintf((char *)stemp, "X = %d\n", MagBuffer_Global[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 38 | lcd.DisplayString(stemp); |
SpoiledAsparagus | 4:b5850ba75275 | 39 | //lcd.DisplayString((uint8_t *)"CENTER"); |
SpoiledAsparagus | 4:b5850ba75275 | 40 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 41 | printf("center button pressed\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 42 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 43 | BSP_COMPASS_DeInit(); |
SpoiledAsparagus | 4:b5850ba75275 | 44 | BSP_COMPASS_Init(); |
SpoiledAsparagus | 4:b5850ba75275 | 45 | } |
SpoiledAsparagus | 4:b5850ba75275 | 46 | |
SpoiledAsparagus | 4:b5850ba75275 | 47 | void left_released() { |
SpoiledAsparagus | 4:b5850ba75275 | 48 | led_green = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 49 | lcd.Clear(); |
SpoiledAsparagus | 4:b5850ba75275 | 50 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 51 | printf("left button released\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 52 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 53 | } |
SpoiledAsparagus | 4:b5850ba75275 | 54 | |
SpoiledAsparagus | 4:b5850ba75275 | 55 | void left_pressed() { |
SpoiledAsparagus | 4:b5850ba75275 | 56 | sprintf((char *)stemp, "Y = %d\n", MagBuffer_Global[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 57 | lcd.DisplayString(stemp); |
SpoiledAsparagus | 4:b5850ba75275 | 58 | //lcd.DisplayString((uint8_t *)"LEFT"); |
SpoiledAsparagus | 4:b5850ba75275 | 59 | |
SpoiledAsparagus | 4:b5850ba75275 | 60 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 61 | printf("left button pressed\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 62 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 63 | } |
SpoiledAsparagus | 4:b5850ba75275 | 64 | |
SpoiledAsparagus | 4:b5850ba75275 | 65 | void right_released() { |
SpoiledAsparagus | 4:b5850ba75275 | 66 | led_green = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 67 | lcd.Clear(); |
SpoiledAsparagus | 4:b5850ba75275 | 68 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 69 | printf("right button released\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 70 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 71 | } |
SpoiledAsparagus | 4:b5850ba75275 | 72 | |
SpoiledAsparagus | 4:b5850ba75275 | 73 | void right_pressed() { |
SpoiledAsparagus | 4:b5850ba75275 | 74 | sprintf((char *)stemp, "Z = %d\n", MagBuffer_Global[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 75 | lcd.DisplayString(stemp); |
SpoiledAsparagus | 4:b5850ba75275 | 76 | //lcd.DisplayString((uint8_t *)"RIGHT"); |
SpoiledAsparagus | 4:b5850ba75275 | 77 | led_green = 1; |
SpoiledAsparagus | 4:b5850ba75275 | 78 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 79 | printf("right button pressed\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 80 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 81 | } |
SpoiledAsparagus | 4:b5850ba75275 | 82 | |
SpoiledAsparagus | 4:b5850ba75275 | 83 | void up_released() { |
SpoiledAsparagus | 4:b5850ba75275 | 84 | led_green = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 85 | lcd.Clear(); |
SpoiledAsparagus | 4:b5850ba75275 | 86 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 87 | printf("up button released\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 88 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 89 | } |
SpoiledAsparagus | 4:b5850ba75275 | 90 | |
SpoiledAsparagus | 4:b5850ba75275 | 91 | void up_pressed() { |
SpoiledAsparagus | 4:b5850ba75275 | 92 | sprintf((char *)stemp, "X = %d\n", AccBuffer_Global[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 93 | lcd.DisplayString(stemp); |
SpoiledAsparagus | 4:b5850ba75275 | 94 | //lcd.DisplayString((uint8_t *)"UP"); |
SpoiledAsparagus | 4:b5850ba75275 | 95 | led_green = 1; |
SpoiledAsparagus | 4:b5850ba75275 | 96 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 97 | printf("up button pressed\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 98 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 99 | } |
SpoiledAsparagus | 4:b5850ba75275 | 100 | |
SpoiledAsparagus | 4:b5850ba75275 | 101 | void down_released() { |
SpoiledAsparagus | 4:b5850ba75275 | 102 | led_green = 0; |
SpoiledAsparagus | 4:b5850ba75275 | 103 | lcd.Clear(); |
SpoiledAsparagus | 4:b5850ba75275 | 104 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 105 | printf("down button released\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 106 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 107 | } |
SpoiledAsparagus | 4:b5850ba75275 | 108 | |
SpoiledAsparagus | 4:b5850ba75275 | 109 | void down_pressed() { |
SpoiledAsparagus | 4:b5850ba75275 | 110 | sprintf((char *)stemp, "Z = %d\n", AccBuffer_Global[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 111 | lcd.DisplayString(stemp); |
SpoiledAsparagus | 4:b5850ba75275 | 112 | //lcd.DisplayString((uint8_t *)"DOWN"); |
SpoiledAsparagus | 4:b5850ba75275 | 113 | led_green = 1; |
SpoiledAsparagus | 4:b5850ba75275 | 114 | #if USE_PRINTF == 1 |
SpoiledAsparagus | 4:b5850ba75275 | 115 | printf("down button pressed\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 116 | #endif |
SpoiledAsparagus | 4:b5850ba75275 | 117 | } |
SpoiledAsparagus | 4:b5850ba75275 | 118 | |
bcostm | 0:211cb2effe6e | 119 | int main() |
bcostm | 0:211cb2effe6e | 120 | { |
bcostm | 0:211cb2effe6e | 121 | int16_t MagBuffer[3]; |
bcostm | 0:211cb2effe6e | 122 | int16_t AccBuffer[3]; |
SpoiledAsparagus | 4:b5850ba75275 | 123 | |
bcostm | 0:211cb2effe6e | 124 | |
bcostm | 0:211cb2effe6e | 125 | printf("Compass started\n"); |
SpoiledAsparagus | 4:b5850ba75275 | 126 | lcd.DisplayString((uint8_t *)"HELLO"); |
SpoiledAsparagus | 4:b5850ba75275 | 127 | // Both rise and fall edges generate an interrupt |
SpoiledAsparagus | 4:b5850ba75275 | 128 | center.fall(¢er_released); |
SpoiledAsparagus | 4:b5850ba75275 | 129 | center.rise(¢er_pressed); |
SpoiledAsparagus | 4:b5850ba75275 | 130 | left.fall(&left_released); |
SpoiledAsparagus | 4:b5850ba75275 | 131 | left.rise(&left_pressed); |
SpoiledAsparagus | 4:b5850ba75275 | 132 | right.fall(&right_released); |
SpoiledAsparagus | 4:b5850ba75275 | 133 | right.rise(&right_pressed); |
SpoiledAsparagus | 4:b5850ba75275 | 134 | up.fall(&up_released); |
SpoiledAsparagus | 4:b5850ba75275 | 135 | up.rise(&up_pressed); |
SpoiledAsparagus | 4:b5850ba75275 | 136 | down.fall(&down_released); |
SpoiledAsparagus | 4:b5850ba75275 | 137 | down.rise(&down_pressed); |
SpoiledAsparagus | 4:b5850ba75275 | 138 | |
SpoiledAsparagus | 4:b5850ba75275 | 139 | // Add pull-down on these pins |
SpoiledAsparagus | 4:b5850ba75275 | 140 | // Warning: must be done AFTER edges setting |
SpoiledAsparagus | 4:b5850ba75275 | 141 | left.mode(PullDown); |
SpoiledAsparagus | 4:b5850ba75275 | 142 | right.mode(PullDown); |
SpoiledAsparagus | 4:b5850ba75275 | 143 | up.mode(PullDown); |
SpoiledAsparagus | 4:b5850ba75275 | 144 | down.mode(PullDown); |
bcostm | 0:211cb2effe6e | 145 | |
bcostm | 0:211cb2effe6e | 146 | while(1) { |
bcostm | 0:211cb2effe6e | 147 | |
bcostm | 0:211cb2effe6e | 148 | // Read acceleremoter and magnetometer values |
bcostm | 0:211cb2effe6e | 149 | compass.AccGetXYZ(AccBuffer); |
bcostm | 0:211cb2effe6e | 150 | compass.MagGetXYZ(MagBuffer); |
SpoiledAsparagus | 4:b5850ba75275 | 151 | MagBuffer_Global[0]= MagBuffer[0]; |
SpoiledAsparagus | 4:b5850ba75275 | 152 | AccBuffer_Global[0]= AccBuffer[0]; |
bcostm | 0:211cb2effe6e | 153 | // Display values |
SpoiledAsparagus | 4:b5850ba75275 | 154 | printf("Acc X = %d\n\r", AccBuffer[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 155 | //AccBuffer[1]=0; |
SpoiledAsparagus | 4:b5850ba75275 | 156 | //AccBuffer[2]=0; |
SpoiledAsparagus | 4:b5850ba75275 | 157 | printf("Acc Y = %d\n\r", AccBuffer[1]); |
SpoiledAsparagus | 4:b5850ba75275 | 158 | printf("Acc Z = %d\n\r", AccBuffer[2]); |
SpoiledAsparagus | 4:b5850ba75275 | 159 | //MagBuffer[1]=0; |
SpoiledAsparagus | 4:b5850ba75275 | 160 | //MagBuffer[2]=0; |
SpoiledAsparagus | 4:b5850ba75275 | 161 | printf("Mag X = %d\n\r ", MagBuffer[0]); |
SpoiledAsparagus | 4:b5850ba75275 | 162 | printf("Mag Y = %d ", MagBuffer[1]); |
SpoiledAsparagus | 4:b5850ba75275 | 163 | printf("Mag Z = %d\n\r", MagBuffer[2]); |
bcostm | 0:211cb2effe6e | 164 | printf("\033[6A"); // Moves cursor up x lines (x value is between [ and A) |
bcostm | 0:211cb2effe6e | 165 | |
bcostm | 0:211cb2effe6e | 166 | led1 = !led1; |
SpoiledAsparagus | 4:b5850ba75275 | 167 | wait(2); |
bcostm | 0:211cb2effe6e | 168 | } |
bcostm | 0:211cb2effe6e | 169 | } |
SpoiledAsparagus | 4:b5850ba75275 | 170 | |
SpoiledAsparagus | 4:b5850ba75275 | 171 |