Bryce Himebaugh
/
lon-lof
Program to demonstrate lighting an led via a command line command
main.cpp@1:48e24393d6d9, 2021-10-06 (annotated)
- Committer:
- bhimebau
- Date:
- Wed Oct 06 13:29:15 2021 +0000
- Revision:
- 1:48e24393d6d9
- Parent:
- 0:f7d9662c6d11
Added Pressure Stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhimebau | 0:f7d9662c6d11 | 1 | /* mbed Microcontroller Library |
bhimebau | 0:f7d9662c6d11 | 2 | * Copyright (c) 2019 ARM Limited |
bhimebau | 0:f7d9662c6d11 | 3 | * SPDX-License-Identifier: Apache-2.0 |
bhimebau | 0:f7d9662c6d11 | 4 | */ |
bhimebau | 0:f7d9662c6d11 | 5 | |
bhimebau | 0:f7d9662c6d11 | 6 | #include "mbed.h" |
bhimebau | 0:f7d9662c6d11 | 7 | #include "platform/mbed_thread.h" |
bhimebau | 0:f7d9662c6d11 | 8 | #include <string.h> |
bhimebau | 1:48e24393d6d9 | 9 | #include "LPS331_I2C.h" |
bhimebau | 0:f7d9662c6d11 | 10 | |
bhimebau | 0:f7d9662c6d11 | 11 | // Blinking rate in milliseconds |
bhimebau | 0:f7d9662c6d11 | 12 | #define BLINKING_RATE_MS 500 |
bhimebau | 0:f7d9662c6d11 | 13 | |
bhimebau | 0:f7d9662c6d11 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
bhimebau | 1:48e24393d6d9 | 15 | LPS331_I2C lps331(PB_7, PB_6, LPS331_I2C_SA0_HIGH); |
bhimebau | 0:f7d9662c6d11 | 16 | |
bhimebau | 0:f7d9662c6d11 | 17 | int main() |
bhimebau | 0:f7d9662c6d11 | 18 | { |
bhimebau | 0:f7d9662c6d11 | 19 | char cmd[80]; |
bhimebau | 0:f7d9662c6d11 | 20 | char ch; |
bhimebau | 0:f7d9662c6d11 | 21 | unsigned char index = 0; |
bhimebau | 0:f7d9662c6d11 | 22 | |
bhimebau | 1:48e24393d6d9 | 23 | if(lps331.isAvailable()) { |
bhimebau | 1:48e24393d6d9 | 24 | pc.printf("LPS331 is available!\r\n"); |
bhimebau | 1:48e24393d6d9 | 25 | } else { |
bhimebau | 1:48e24393d6d9 | 26 | pc.printf("LPS331 is unavailable!\r\n"); |
bhimebau | 1:48e24393d6d9 | 27 | } |
bhimebau | 1:48e24393d6d9 | 28 | |
bhimebau | 0:f7d9662c6d11 | 29 | // Initialise the digital pin LED1 as an output |
bhimebau | 0:f7d9662c6d11 | 30 | DigitalOut led(LED1); |
bhimebau | 0:f7d9662c6d11 | 31 | pc.printf("L432> "); |
bhimebau | 0:f7d9662c6d11 | 32 | while (true) { |
bhimebau | 0:f7d9662c6d11 | 33 | |
bhimebau | 0:f7d9662c6d11 | 34 | ch = pc.getc(); |
bhimebau | 0:f7d9662c6d11 | 35 | index = 0; |
bhimebau | 0:f7d9662c6d11 | 36 | while ((ch != '\n') && (ch != '\r')) { |
bhimebau | 0:f7d9662c6d11 | 37 | pc.putc(ch); |
bhimebau | 0:f7d9662c6d11 | 38 | cmd[index++] = ch; |
bhimebau | 0:f7d9662c6d11 | 39 | ch = pc.getc(); |
bhimebau | 0:f7d9662c6d11 | 40 | } |
bhimebau | 0:f7d9662c6d11 | 41 | cmd[index] = 0; |
bhimebau | 0:f7d9662c6d11 | 42 | if (!strcmp(cmd,"lon")) { |
bhimebau | 0:f7d9662c6d11 | 43 | led = 1; |
bhimebau | 0:f7d9662c6d11 | 44 | } |
bhimebau | 0:f7d9662c6d11 | 45 | else if (!strcmp(cmd,"lof")) { |
bhimebau | 0:f7d9662c6d11 | 46 | led = 0; |
bhimebau | 0:f7d9662c6d11 | 47 | } |
bhimebau | 0:f7d9662c6d11 | 48 | pc.printf("\n\r",cmd); |
bhimebau | 0:f7d9662c6d11 | 49 | pc.printf("L432> "); |
bhimebau | 0:f7d9662c6d11 | 50 | } |
bhimebau | 0:f7d9662c6d11 | 51 | } |
bhimebau | 1:48e24393d6d9 | 52 | |
bhimebau | 1:48e24393d6d9 | 53 | /* |
bhimebau | 1:48e24393d6d9 | 54 | #include "LPS331_I2C.h" |
bhimebau | 1:48e24393d6d9 | 55 | |
bhimebau | 1:48e24393d6d9 | 56 | Serial pc(USBTX, USBRX); |
bhimebau | 1:48e24393d6d9 | 57 | LPS331_I2C lps331(p9, p10, LPS331_I2C_SA0_HIGH); |
bhimebau | 1:48e24393d6d9 | 58 | |
bhimebau | 1:48e24393d6d9 | 59 | |
bhimebau | 1:48e24393d6d9 | 60 | int main() { |
bhimebau | 1:48e24393d6d9 | 61 | pc.printf("LPS331 Test Program.\r\n"); |
bhimebau | 1:48e24393d6d9 | 62 | |
bhimebau | 1:48e24393d6d9 | 63 | if(lps331.isAvailable()) { |
bhimebau | 1:48e24393d6d9 | 64 | pc.printf("LPS331 is available!\r\n"); |
bhimebau | 1:48e24393d6d9 | 65 | } else { |
bhimebau | 1:48e24393d6d9 | 66 | pc.printf("LPS331 is unavailable!\r\n"); |
bhimebau | 1:48e24393d6d9 | 67 | } |
bhimebau | 1:48e24393d6d9 | 68 | |
bhimebau | 1:48e24393d6d9 | 69 | lps331.setResolution(LPS331_I2C_PRESSURE_AVG_512, LPS331_I2C_TEMP_AVG_128); |
bhimebau | 1:48e24393d6d9 | 70 | lps331.setDataRate(LPS331_I2C_DATARATE_7HZ); |
bhimebau | 1:48e24393d6d9 | 71 | lps331.setActive(true); |
bhimebau | 1:48e24393d6d9 | 72 | |
bhimebau | 1:48e24393d6d9 | 73 | pc.printf("LPS331 Register map.\r\n"); |
bhimebau | 1:48e24393d6d9 | 74 | |
bhimebau | 1:48e24393d6d9 | 75 | for(int i = 0; i < 8; i++) { |
bhimebau | 1:48e24393d6d9 | 76 | pc.printf("%02x: ", i); |
bhimebau | 1:48e24393d6d9 | 77 | for(int j = 0; j < 16; j++) { |
bhimebau | 1:48e24393d6d9 | 78 | char value = lps331._read(j | i << 4); |
bhimebau | 1:48e24393d6d9 | 79 | pc.printf("%02x ", value); |
bhimebau | 1:48e24393d6d9 | 80 | } |
bhimebau | 1:48e24393d6d9 | 81 | pc.printf("\r\n"); |
bhimebau | 1:48e24393d6d9 | 82 | } |
bhimebau | 1:48e24393d6d9 | 83 | |
bhimebau | 1:48e24393d6d9 | 84 | pc.printf("LPS331 Register map(multibyte read test).\r\n"); |
bhimebau | 1:48e24393d6d9 | 85 | |
bhimebau | 1:48e24393d6d9 | 86 | for(int i = 0; i < 8; i++) { |
bhimebau | 1:48e24393d6d9 | 87 | char data[16]; |
bhimebau | 1:48e24393d6d9 | 88 | lps331._read_multibyte(i << 4, data, 16); |
bhimebau | 1:48e24393d6d9 | 89 | pc.printf("%02x: ", i); |
bhimebau | 1:48e24393d6d9 | 90 | for(int j = 0; j < 16; j++) { |
bhimebau | 1:48e24393d6d9 | 91 | pc.printf("%02x ", data[j]); |
bhimebau | 1:48e24393d6d9 | 92 | } |
bhimebau | 1:48e24393d6d9 | 93 | pc.printf("\r\n"); |
bhimebau | 1:48e24393d6d9 | 94 | } |
bhimebau | 1:48e24393d6d9 | 95 | |
bhimebau | 1:48e24393d6d9 | 96 | wait(2.0); |
bhimebau | 1:48e24393d6d9 | 97 | |
bhimebau | 1:48e24393d6d9 | 98 | while(true) { |
bhimebau | 1:48e24393d6d9 | 99 | float pres, temp; |
bhimebau | 1:48e24393d6d9 | 100 | |
bhimebau | 1:48e24393d6d9 | 101 | pres = lps331.getPressure(); |
bhimebau | 1:48e24393d6d9 | 102 | temp = lps331.getTemperature(); |
bhimebau | 1:48e24393d6d9 | 103 | |
bhimebau | 1:48e24393d6d9 | 104 | pc.printf("%f,%f\r\n", pres, temp); |
bhimebau | 1:48e24393d6d9 | 105 | |
bhimebau | 1:48e24393d6d9 | 106 | wait(1/7.0); |
bhimebau | 1:48e24393d6d9 | 107 | } |
bhimebau | 1:48e24393d6d9 | 108 | */ |