aboutsummaryrefslogtreecommitdiff
path: root/src/core/Wire.h
blob: 7b42f94d74fcbd1c7ef00b05ddf53d9dbee4e183 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef WIRE_H_
#define WIRE_H_

/**************************************************************************//**
 * @file     Wire.h
 * @brief    Arduino-like library for MARY
 * @version	V0.50
 * @date	4. April 2012.
 * @author	@@lynxeyed_atsu
 * @note	Copyright (c) 2011 Lynx-EyED's Klavier and Craft-works. <BR>
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions: <BR>
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.<BR>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
#include "LPC11xx.h"
#include <libmary.h>

#ifdef __cplusplus
	extern "C" {
#endif

#define BUFFER_LENGTH 32

/*! @enum	WIRE_ACK_STATE
	@brief	I2Cでバイトデータを送った際にスレーブからの値を返す
*/
typedef enum {
	ACK = 0,	/*!< ACK(入力バイトは承認された) */
	NAK,		/*!< ACK(入力バイトは承認されなかった) */
	BUFF_OV, 		/*!< バッファオーバーフロー */
	ADDR_NAK,		/*!< スレーブへアドレスを送信しNACKを受信した */
	DATA_NAK,		/*!< スレーブへデータを送信しNACKを受信した */
	AND_MORE		/*!< その他のエラー */
} WIRE_ACK_STATE;

class TwoWire{

private:
	int sda,scl;

    static uint8_t rxBuffer[];
    static uint8_t rxBufferIndex;
    static uint8_t rxBufferLength;

    static uint8_t txAddress;
    static uint8_t txBuffer[];
    static uint8_t txBufferIndex;
    static uint8_t txBufferLength;

    static uint8_t transmitting;
    static void (*user_onRequest)(void);
    static void (*user_onReceive)(int);
    static void onRequestService(void);
    static void onReceiveService(uint8_t*, int);

public:
	TwoWire();
	void begin();
	void begin(uint8_t);
	void begin(int);
	void begin(int,int);
	void beginTransmission(uint8_t);
	void beginTransmission(int);
	uint8_t endTransmission(void);
	uint8_t requestFrom(uint8_t, uint8_t);
	uint8_t requestFrom(int, int);
	void write(int8_t);
	void send(uint8_t*, uint8_t);
	void send(int8_t *);
	void send(int);
	uint8_t available(void);
	uint8_t receive(void);
	void onReceive( void (*)(int) );
	void onRequest( void (*)(void) );
};

extern TwoWire Wire;

#ifdef __cplusplus
  }
#endif

#endif /* WIRE_H_ */