<commit_before>/**
 * @file Cosa/TWI/Driver/PCF8574.cpp
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2013, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * This file is part of the Arduino Che Cosa project.
 */

#include "Cosa/Board.hh"
#if !defined(__ARDUINO_TINY__)
#include "Cosa/TWI/Driver/PCF8574.hh"

uint8_t 
PCF8574::read()
{
  if (!twi.begin()) return (0);
  uint8_t res;
  twi.read(m_addr, &res, sizeof(res));
  twi.end();
  return (res & m_ddr);
}

bool
PCF8574::write(uint8_t pin, uint8_t value)
{
  uint8_t mask = _BV(pin & PIN_MASK) & ~m_ddr;
  if (value)
    m_port |= mask;
  else
    m_port &= ~mask;
  if (!twi.begin()) return (false);
  int res = twi.write(m_addr, &m_port, sizeof(m_port));
  twi.end();
  return (res == sizeof(m_port));
}

bool
PCF8574::write(uint8_t value)
{
  if (!twi.begin()) return (false);
  m_port = value & ~m_ddr;
  int res = twi.write(m_addr, &m_port, sizeof(m_port));
  twi.end();
  return (res == sizeof(m_port));
}

bool
PCF8574::write(void* buf, size_t size)
{
  if (!twi.begin()) return (false);
  uint8_t* bp = (uint8_t*) bp;
  size_t n = size;
  while (n--) *bp++ &= ~m_ddr;
  int res = twi.write(m_addr, buf, size);
  twi.end();
  return (res == size);
}

#endif<commit_msg>Adding support for TWI master on ATtiny.<commit_after>/**
 * @file Cosa/TWI/Driver/PCF8574.cpp
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2013, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * This file is part of the Arduino Che Cosa project.
 */

#include "Cosa/TWI/Driver/PCF8574.hh"

uint8_t 
PCF8574::read()
{
  if (!twi.begin()) return (0);
  uint8_t res;
  twi.read(m_addr, &res, sizeof(res));
  twi.end();
  return (res & m_ddr);
}

bool
PCF8574::write(uint8_t pin, uint8_t value)
{
  uint8_t mask = _BV(pin & PIN_MASK) & ~m_ddr;
  if (value)
    m_port |= mask;
  else
    m_port &= ~mask;
  if (!twi.begin()) return (false);
  int res = twi.write(m_addr, &m_port, sizeof(m_port));
  twi.end();
  return (res == sizeof(m_port));
}

bool
PCF8574::write(uint8_t value)
{
  if (!twi.begin()) return (false);
  m_port = value & ~m_ddr;
  int res = twi.write(m_addr, &m_port, sizeof(m_port));
  twi.end();
  return (res == sizeof(m_port));
}

bool
PCF8574::write(void* buf, size_t size)
{
  if (!twi.begin()) return (false);
  uint8_t* bp = (uint8_t*) bp;
  size_t n = size;
  while (n--) *bp++ &= ~m_ddr;
  int res = twi.write(m_addr, buf, size);
  twi.end();
  return (res == size);
}
{'fraction_non_alphanumeric': 0.13241772479917077, 'fraction_numerical': 0.028504793988079813, 'mean_word_length': 3.707317073170732, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6785195', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1397, 'n_words': 474}