blob: a5a5e284a4fe1f7c20df41c208058e9c8c7f6516 [file] [log] [blame]
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// smogcheck.c: demonstrate use of lib_smogcheck.c
#include <stdint.h>
#include <stdlib.h>
#include "lib/lib_smogcheck.h"
void ExitOnError(int val) {
if (val < 0) {
exit(EXIT_FAILURE);
}
}
int main() {
int fd = -1;
int ret = -1;
const int kPcaSlaveAddress = 0x27;
const int kI2cBusAddress = 0x2;
const int kInaSlaveAddress = 0x40;
const uint8_t kLedRegister = 0x3;
const uint8_t kVoltageRegister = 0x2;
const uint8_t kTurnOnLedOne = 0xfe;
fd = GetDeviceFile(kI2cBusAddress);
ExitOnError(fd);
// Enable LED 1
ret = SetSlaveAddress(fd, kPcaSlaveAddress);
ExitOnError(ret);
ret = WriteByte(fd, kLedRegister, kTurnOnLedOne);
ExitOnError(ret);
ret = ReadByte(fd, kLedRegister);
ExitOnError(ret);
// Read Backup Power voltage
ret = SetSlaveAddress(fd, kInaSlaveAddress);
ExitOnError(ret);
ret = ReadWord(fd, kVoltageRegister);
ExitOnError(ret);
return EXIT_SUCCESS;
}