aboutsummaryrefslogtreecommitdiff
path: root/test_fw/include/i2c_scan.h
blob: 1fc81c1d16884d245638c1cc9b733f1fa176b307 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

#include <Arduino.h>
#include <Wire.h>

namespace ocularium {
    inline void scan(TwoWire& wire, bool post_delay = true)
    {
        for(auto address = 1; address < 127; address++)
        {
            wire.beginTransmission(address);
            const uint8_t error = wire.endTransmission();

            if (error == 0)
            {
                Serial.print("I2C device found at address 0x");
                if (address<16)
                    Serial.print("0");
                Serial.print(address,HEX);
                Serial.println();
            }
            else if (error==4)
            {
                Serial.print("Unknown error at address 0x");
                if (address<16)
                    Serial.print("0");
                Serial.println(address,HEX);
            }
        }

        if (post_delay)
        {
            // Let the bus settle before doing anything else.
            delay(10);
        }
    }
}