-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSD1306.js
94 lines (85 loc) · 2.56 KB
/
SSD1306.js
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
+(function(factory) {
factory(window);
}(function(scope) {
'use strict';
var self;
var proto;
var _textSize = 2;
var _cursorX = 0;
var _cursorY = 0;
var sendLength = 32;
function SSD1306(board) {
this._board = board;
self = this;
board.send([0xF0, 0x04, 0x01, 0x0, 0xF7]);
board.send([0xF0, 0x04, 0x01, 0x02, _cursorX, _cursorY, 0xF7]);
board.send([0xF0, 0x04, 0x01, 0x03, _textSize, 0xF7]);
board.send([0xF0, 0x04, 0x01, 0x01, 0xF7]);
}
SSD1306.prototype = proto = Object.create(Object.prototype, {
constructor: {
value: SSD1306
},
textSize: {
get: function() {
return _textSize;
},
set: function(val) {
board.send([0xF0, 0x04, 0x01, 0x03, val, 0xF7]);
_textSize = val;
}
}
});
proto.clear = function() {
board.send([0xF0, 0x04, 0x01, 0x01, 0xF7]);
}
proto.drawImage = function(num) {
board.send([0xF0, 0x04, 0x01, 0x05, num, 0xF7]);
}
proto.render = function() {
board.send([0xF0, 0x04, 0x01, 0x06, 0xF7]);
}
proto.save = function(data) {
for (var i = 0; i < data.length; i = i + sendLength) {
var chunk = data.substring(i, i + sendLength);
saveChunk(i / 2, chunk);
}
}
function saveChunk(startPos, data) {
var CMD = [0xf0, 0x04, 0x01, 0x0A];
var raw = [];
raw = raw.concat(CMD);
var n = '0000' + startPos.toString(16);
n = n.substring(n.length - 4);
for (var i = 0; i < 4; i++) {
raw.push(n.charCodeAt(i));
}
raw.push(0xf7);
// send Data //
CMD = [0xf0, 0x04, 0x01, 0x0B];
raw = raw.concat(CMD);
for (i = 0; i < data.length; i++) {
raw.push(data.charCodeAt(i));
}
raw.push(0xf7);
board.send(raw);
}
proto.print = function(cursorX, cursorY, str) {
var len = arguments.length;
if (len == 3) {
_cursorX = cursorX;
_cursorY = cursorY;
board.send([0xF0, 0x04, 0x01, 0x02, cursorX, cursorY, 0xF7]);
} else {
str = cursorX;
board.send([0xF0, 0x04, 0x01, 0x02, _cursorX, _cursorY, 0xF7]);
}
var strCMD = [0xF0, 0x04, 0x01, 0x04];
for (var i = 0; i < str.length; i++) {
strCMD.push(str.charCodeAt(i));
}
strCMD.push(0xF7);
board.send(strCMD);
}
scope.SSD1306 = SSD1306;
}));