akkera 102
/
apuplay
SPC music playback tools for real snes apu
Diff: gpio.cpp
- Revision:
- 0:5bd52e196edb
- Child:
- 2:62e6e22f8be2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpio.cpp Mon Jan 09 13:54:39 2017 +0000 @@ -0,0 +1,192 @@ +/** + * @file gpio.cpp + * @brief GPIO クラスの動作の定義を行います + */ + +#include "stdafx.h" +#include "gpio.h" +// #include <wiringPi.h> +#include "mbed.h" + +DigitalInOut d[] = { + p5, // A7 + p6, // A6 + p7, // A0 + p8, // A1 + p9, // WR + p10, // RD + + p11, // D0 + p12, + p13, + p14, + p15, + p16, + p17, + p18, // D7 + + p19, // ICL +}; + + +/*! 唯一のインスタンスです */ +CGpio CGpio::sm_instance; + +/** + * 初期化 + */ +void CGpio::Initialize() +{ +// ::wiringPiSetup(); +} + +/** + * I/Oの方向を設定する + * @param[in] pin ピン番号 + * @param[in] write 方向 + */ +void CGpio::PinDir(int pin, bool write) +{ +// ::pinMode(pin, (write ? OUTPUT : INPUT)); + if(write) + { + d[pin].output(); + } + else + { + d[pin].input(); + } +} + +/** + * Output + * @param[in] pin ピン番号 + * @param[in] high ON/OFF + */ +void CGpio::SetPin(int pin, bool high) +{ +// ::digitalWrite(pin, (high ? HIGH : LOW)); + d[pin] = high ? 1 : 0; +} + +/** + * Input + * @param[in] pin ピン番号 + * @return ON/OFF + */ +bool CGpio::GetPin(int pin) +{ +// return (::digitalRead(pin) != LOW); + d[pin].input(); + return d[pin] != 0; +} + +/** + * コンストラクタ + */ +CGpio::CGpio() + : m_dir(kUninitialize) +{ +} + +/** + * バイト書き込み + * @param[in] c データ + */ +void CGpio::WriteByte(unsigned char c) +{ +// ByteMode(kWrite); +// ::digitalWriteByte(c); + int i; + + for(i=6; i<6+8; i++) + { + d[i].output(); + d[i] = c & 0x1; + c >>= 1; + } +} + +/** + * バイト読み込み + * @return データ + */ +unsigned char CGpio::ReadByte() +{ +// ByteMode(kRead); +// return SwapBits(::digitalReadByte()); + + unsigned char ret = 0; + int i; + + for(i=6; i<6+8; i++) + { + d[i].input(); + ret <<= 1; + ret |= d[i]; + } + + return SwapBits(ret); +} + +/** + * バイト モード + * @param[in] dir I/O + */ + /* +void CGpio::ByteMode(ByteDir dir) +{ + if (m_dir != dir) + { + m_dir = dir; + const int mode = (dir != kRead) ? OUTPUT : INPUT; + for (int i = 0; i < 8; i++) + { + ::pinMode(i, mode); + } + } +} +*/ + +/** + * ビット スワップ + * @param[in] c データ + * @return データ + */ +unsigned char CGpio::SwapBits(unsigned char c) +{ + unsigned char s = 0; + if (c & 0x01) + { + s |= 0x80; + } + if (c & 0x02) + { + s |= 0x40; + } + if (c & 0x04) + { + s |= 0x20; + } + if (c & 0x08) + { + s |= 0x10; + } + if (c & 0x10) + { + s |= 0x08; + } + if (c & 0x20) + { + s |= 0x04; + } + if (c & 0x40) + { + s |= 0x02; + } + if (c & 0x80) + { + s |= 0x01; + } + return s; +}