From 80af416d5bb25278fbd145fd540b11e7783f3f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 28 Dec 2017 12:01:48 +0100 Subject: [PATCH] Changed i2c lock mask method --- code/espurna/i2c.ino | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index 77a83e63..ccbf27bb 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -8,8 +8,7 @@ Copyright (C) 2017 by Xose PĂ©rez #if I2C_SUPPORT -#include -std::vector _i2c_locked; +unsigned int _i2c_locked[16] = {0}; #if I2C_USE_BRZO #include "brzo_i2c.h" @@ -127,20 +126,20 @@ bool i2cCheck(unsigned char address) { } bool i2cGetLock(unsigned char address) { - for (unsigned char i=0; i<_i2c_locked.size(); i++) { - if (_i2c_locked[i] == address) return false; - } - _i2c_locked.push_back(address); + unsigned char index = address / 8; + unsigned char mask = 1 << (address % 8); + if (_i2c_locked[index] & mask) return false; + _i2c_locked[index] = _i2c_locked[index] | mask; DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address); return true; } bool i2cReleaseLock(unsigned char address) { - for (unsigned char i=0; i<_i2c_locked.size(); i++) { - if (_i2c_locked[i] == address) { - _i2c_locked.erase(_i2c_locked.begin() + i); - return true; - } + unsigned char index = address / 8; + unsigned char mask = 1 << (address % 8); + if (_i2c_locked[index] & mask) { + _i2c_locked[index] = _i2c_locked[index] & ~mask; + return true; } return false; }