-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnootor.cpp
281 lines (242 loc) · 6.68 KB
/
snootor.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Snootlab Max 7313 Motor shield library
// Based on Adafruit Motor shield library
// https://github.com/adafruit/Adafruit-Motor-Shield-library
// copyleft Snootlab, 2011
// this code is public domain, enjoy!
/**
*
* generic snootor methods, each snootor-driven motor uses them,
* especially for 'pseudo-multitasking'
*
**/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define SNOOT_WIREWRITE Wire.write
#else
#include <avr/io.h>
#include "WProgram.h"
#define SNOOT_WIREWRITE Wire.send
#endif
#include <Wire.h>
#include <snootor.h>
Snootor::Snootor(){
motor_state=0;
maxstate=0;
/**
* keep track of "directions"
**/
_regvalue=0;
}
void Snootor::enableMax(void) {
if(maxstate==0){
i2c( 0x6, 0x00); // input and output config.
i2c( 0x2, 0x00); // input and output config.
i2c( 0xe, 0x0f); // config bit
i2c( 0xf, 0x0c); // blink 0 on
/*
i2c( 0xe, 0xff); // 0f Internal oscilator disabled. All output are static WITHOUT PWM
i2c( 0xf, 0x10); // blink 0 on
i2c( 0x6, 0x00); // input and output config.
i2c( 0x7, 0x00); // ...
i2c( 0x2, 0xFF); // global intensity reg.
i2c(0x3, 0xff);
i2c(0xe, 0xff); // config bit
*/
/*
i2c(0x10,0x00); // zero out pwms on register 0x10
i2c(0x11,0x00); // zero out pwms on register 0x11
i2c(0x12,0x00); // zero out pwms on register 0x12
i2c(0x13,0x00); // zero out pwms on register 0x13
*/
i2c(0x14,0x00); // zero out pwms on register 0x14
i2c(0x15,0x00); // zero out pwms on register 0x15
i2c(0x16,0x00); // zero out pwms on register 0x16
i2c(0x17,0x00); // zero out pwms on register 0x17
maxstate=1;
}
#ifdef MOTOR_DEBUG
Serial.println("MAX7313 I2C INIT DONE !");
#endif
}
/**
*
* wrapper around delay()
*
* MOTOR_MICRO_DELAY -> 'tick' of pseudo-scheduler
* check for each motor if it's loaded, (MOTOR_MASK vs motor_state)
* execute basic step for loaded motors
*
**/
void Snootor::delay(uint32_t msecs){
uint32_t i;
for(i=0;i<msecs*1000;i+=MOTOR_MICRO_DELAY){
delayMicroseconds(MOTOR_MICRO_DELAY);
if(motor_state & MOTOR_MASK_1) i+=motors[0]->next();
if(motor_state & MOTOR_MASK_2) i+=motors[1]-> next();
if(motor_state & MOTOR_MASK_3) i+=motors[2]-> next();
if(motor_state & MOTOR_MASK_4) i+=motors[3]-> next();
}
}
void Snootor::stop(){
if(motor_state & MOTOR_MASK_1) {
#ifdef MOTOR_DEBUG
Serial.println("Stop Motor nr 1");
#endif
motors[0]-> stop();
}
if(motor_state & MOTOR_MASK_2) {
#ifdef MOTOR_DEBUG
Serial.println("Stop Motor nr 2");
#endif
motors[1]-> stop();
}
if(motor_state & MOTOR_MASK_3) {
#ifdef MOTOR_DEBUG
Serial.println("Stop Motor nr 3");
#endif
motors[2]-> stop();
}
if(motor_state & MOTOR_MASK_4) {
#ifdef MOTOR_DEBUG
Serial.println("Stop Motor nr 4");
#endif
motors[3]-> stop();
}
}
void Snootor::add(SnootorMotor*m){
enableMax();
uint8_t i=0;
while((motor_state & 1<<i)&& i<3)
i++;
motors[i]=m;
motor_state |= 1<<i;
#ifdef MOTOR_DEBUG
Serial.println("snootor::add!");
m->dump();
Serial.print("motor state : ");
Serial.println(motor_state,DEC);
Serial.print("motor state & MOTOR_MASK_1 : ");
Serial.println(motor_state & MOTOR_MASK_1,DEC);
Serial.print("motor 1 : ");
Serial.println((long)motors,HEX);
Serial.print("motor state & MOTOR_MASK_2 : ");
Serial.println(motor_state & MOTOR_MASK_2,DEC);
Serial.print("motor 2 : ");
Serial.println((long)(motors+1),HEX);
Serial.print("motor state & MOTOR_MASK_3 : ");
Serial.println(motor_state & MOTOR_MASK_3,DEC);
Serial.print("motor 3 : ");
Serial.println((long)(motors+2),HEX);
Serial.print("motor state & MOTOR_MASK_4 : ");
Serial.println(motor_state & MOTOR_MASK_4,DEC);
Serial.print("motor 4 : ");
Serial.println((long)(motors+3),HEX);
#endif
}
/**
* single bit i2c communication
**/
void Snootor::i2c(uint8_t reg,uint8_t val){
Wire.beginTransmission(MAX_ADRESS);
SNOOT_WIREWRITE( reg);
SNOOT_WIREWRITE( val);
Wire.endTransmission();
}
/**
* two bit i2c communication
**/
void Snootor::i2c2(uint8_t reg,uint8_t val,uint8_t reg2,uint8_t val2){
Wire.beginTransmission(MAX_ADRESS);
SNOOT_WIREWRITE( reg);
SNOOT_WIREWRITE( val);
if(reg+1 != reg2){
Wire.endTransmission();
Wire.beginTransmission(MAX_ADRESS);
SNOOT_WIREWRITE( reg2);
}
SNOOT_WIREWRITE( val2);
Wire.endTransmission();
}
/**
* sending 4bit pwm by "number"
* "value" is reversed
* pwm_state keeps track of pwm values
* lnumber : 0-7, corresponds to pwm number 1-8
*
* number 1/2 -> registry 0x14
* number 3/4 -> registry 0x15
* number 5/6 -> registry 0x16
* number 7/8 -> registry 0x17
* 0xFF = off
* 0xF0 = first pwm activated
* 0x0F = second pwm activated
* 0x0 = no pwm activated
*
**/
void Snootor::sendPWM(uint8_t number,uint8_t value){
int reg_number=0x14 + (number-1)/2;
int even_number=2*((number-1)/2);
value&=0xf;
pwm_state[number-1]=0xf-value;
#ifdef MOTOR_DEBUG
Serial.print("sendpwm on led (w/o offset):");
Serial.print(number,DEC);
Serial.print(" - EN");
Serial.print(even_number,DEC);
Serial.print(" - value (4bit) : 0x");
Serial.print(value,HEX);
Serial.print(" - value inv (4bit) : 0x");
Serial.print(0xf-value,HEX);
Serial.print(" - reg : 0x");
Serial.print(reg_number,HEX);
Serial.print("- state (4bit) : 0x");
Serial.print(pwm_state[number-1],HEX);
Serial.print("- state (8bit) : 0x");
Serial.println(pwm_state[even_number]+(pwm_state[even_number+1]<<4),HEX);
#endif
i2c(reg_number,pwm_state[even_number]+(pwm_state[even_number+1]<<4));
}
/**
* debug function
*
**/
#ifdef MOTOR_DEBUG
void Snootor::dump(){
if(motor_state & MOTOR_MASK_1) {
Serial.println("Motor nr 1");
motors[0]-> dump();
}
if(motor_state & MOTOR_MASK_2) {
Serial.println("Motor nr 2");
motors[1]-> dump();
}
if(motor_state & MOTOR_MASK_3) {
Serial.println("Motor nr 3");
motors[2]-> dump();
}
if(motor_state & MOTOR_MASK_4) {
Serial.println("Motor nr 4");
motors[3]-> dump();
}
for(int i=0;i<4;i++){
Serial.print("REG 0x");
Serial.print(0x14+i,HEX);
Serial.print(" - NR ");
Serial.print(2*i+1,DEC);
Serial.print(" - VAL 0x");
Serial.print(pwm_state[2*i] ,HEX);
Serial.print(" - NR ");
Serial.print(2*i+2,DEC);
Serial.print(" - VAL 0x");
Serial.print(pwm_state[2*i+1] ,HEX);
Serial.print(" - state 0x");
Serial.println(pwm_state[2*i]+(pwm_state[2*i+1]<<4),HEX);
}
}
#endif
/**
*
* Declaration of global object
*
**/
Snootor SC;