Make examples friendlier for non GCC compilers and fixed slave_mem_i2c.c to compile on boards without I2C pins (#336)

This commit is contained in:
Graham Sanderson
2023-03-10 15:41:40 -06:00
committed by GitHub
parent 9d3fea1419
commit 60829a134a
39 changed files with 89 additions and 86 deletions

View File

@@ -214,6 +214,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / bmp280_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
// useful information for picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
@@ -248,7 +249,5 @@ int main() {
// poll every 500ms
sleep_ms(500);
}
#endif
return 0;
}

View File

@@ -224,7 +224,5 @@ again:
ht16k33_set_blink(0);
goto again;
return 0;
#endif
}

View File

@@ -163,7 +163,5 @@ int main() {
lcd_clear();
}
}
return 0;
#endif
}

View File

@@ -122,8 +122,7 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@@ -121,7 +121,7 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif

View File

@@ -148,12 +148,15 @@ void mpl3115a2_convert_fifo_batch(uint8_t start, volatile uint8_t buf[], struct
// 3 altitude registers: MSB (8 bits), CSB (8 bits) and LSB (4 bits, starting from MSB)
// first two are integer bits (2's complement) and LSB is fractional bits -> makes 20 bit signed integer
int32_t h = (int32_t) ((uint32_t) buf[start] << 24 | buf[start + 1] << 16 | buf[start + 2] << 8);
int32_t h = (int32_t) buf[start] << 24;
h |= (int32_t) buf[start + 1] << 16;
h |= (int32_t) buf[start + 2] << 8;
data->altitude = ((float)h) / 65536.f;
// 2 temperature registers: MSB (8 bits) and LSB (4 bits, starting from MSB)
// first 8 are integer bits with sign and LSB is fractional bits -> 12 bit signed integer
int16_t t = (int16_t) (((uint16_t) buf[start + 3]) << 8 | buf[start + 4]);
int16_t t = (int16_t) buf[start + 3] << 8;
t |= (int16_t) buf[start + 4];
data->temperature = ((float)t) / 256.f;
}
@@ -162,6 +165,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / mpl3115a2_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
printf("Hello, MPL3115A2. Waiting for something to interrupt me!...\n");
@@ -202,5 +206,4 @@ int main() {
};
#endif
return 0;
}

View File

@@ -82,6 +82,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/mpu6050_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
printf("Hello, MPU6050! Reading raw data from registers...\n");
@@ -111,7 +112,5 @@ int main() {
sleep_ms(100);
}
#endif
return 0;
}

View File

@@ -24,7 +24,7 @@
*/
const int addr = 0x10;
const int max_read = 250;
#define MAX_READ 250
#ifdef i2c_default
@@ -44,24 +44,24 @@ void pa1010d_parse_string(char output[], char protocol[]) {
int p = com_index - output;
// Splits components of output sentence into array
int no_of_fields = 14;
int max_len = 15;
#define NO_OF_FIELDS 14
#define MAX_LEN 15
int n = 0;
int m = 0;
char gps_data[no_of_fields][max_len];
char gps_data[NO_OF_FIELDS][MAX_LEN];
memset(gps_data, 0, sizeof(gps_data));
bool complete = false;
while (output[p] != '$' && n < max_len && complete == false) {
while (output[p] != '$' && n < MAX_LEN && complete == false) {
if (output[p] == ',' || output[p] == '*') {
n += 1;
m = 0;
} else {
gps_data[n][m] = output[p];
// Checks if sentence is complete
if (m < no_of_fields) {
if (m < NO_OF_FIELDS) {
m++;
} else {
complete = true;
@@ -92,15 +92,15 @@ void pa1010d_parse_string(char output[], char protocol[]) {
}
void pa1010d_read_raw(char numcommand[]) {
uint8_t buffer[max_read];
uint8_t buffer[MAX_READ];
int i = 0;
bool complete = false;
i2c_read_blocking(i2c_default, addr, buffer, max_read, false);
i2c_read_blocking(i2c_default, addr, buffer, MAX_READ, false);
// Convert bytes to characters
while (i < max_read && complete == false) {
while (i < MAX_READ && complete == false) {
numcommand[i] = buffer[i];
// Stop converting at end of message
if (buffer[i] == 10 && buffer[i + 1] == 10) {
@@ -119,7 +119,7 @@ int main() {
puts("Default I2C pins were not defined");
#else
char numcommand[max_read];
char numcommand[MAX_READ];
// Decide which protocols you would like to retrieve data from
char init_command[] = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n";
@@ -140,7 +140,7 @@ int main() {
while (1) {
// Clear array
memset(numcommand, 0, max_read);
memset(numcommand, 0, MAX_READ);
// Read and re-format
pa1010d_read_raw(numcommand);
pa1010d_parse_string(numcommand, "GNRMC");
@@ -149,8 +149,7 @@ int main() {
sleep_ms(1000);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@@ -156,9 +156,8 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@@ -14,6 +14,7 @@
static const uint I2C_SLAVE_ADDRESS = 0x17;
static const uint I2C_BAUDRATE = 100000; // 100 kHz
#ifdef i2c_default
// For this example, we run both the master and slave from the same board.
// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).
static const uint I2C_SLAVE_SDA_PIN = PICO_DEFAULT_I2C_SDA_PIN; // 4
@@ -124,11 +125,18 @@ static void run_master() {
sleep_ms(2000);
}
}
#endif
int main() {
stdio_init_all();
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / slave_mem_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
puts("\nI2C slave example");
setup_slave();
run_master();
#endif
}