-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeColorSensor.h
588 lines (551 loc) · 13.4 KB
/
MeColorSensor.h
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class Mecolor
* \brief Driver for MeColorSensor module.
* @file MeColorSensor.h
* @author MakeBlock
* @version V1.0.3
* @date 2017/01/17
* @brief Header for MeColorSensor.cpp module.
*
* \par Copyright
* This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* This file is a drive for MeColorSensor module, It supports MeColorSensor V1.0 device provided
* by MakeBlock.
*
* \par Method List:
*
* 1. void MeColorSensor::SensorInit(void)
* 2. uint8_t MeColorSensor::ReportId(void)
* 3. void MeColorSensor::ColorDataRead(void)
* 4. void MeColorSensor::TurnOnLight(void)
* 5. void MeColorSensor::TurnOffLight(void)
* 6. uint16_t MeColorSensor::ReturnRedData(void)
* 7. uint16_t MeColorSensor::ReturnGreenData(void)
* 8. uint16_t MeColorSensor::ReturnBlueData(void)
* 9. uint16_t MeColorSensor::ReturnColorData(void)
* 10. uint8_t MeColorSensor::ColorIdentify(void)
* 11. long MeColorSensor::ReturnColorCode(void)
* 12. uint16_t MeColorSensor::calculateColorTemperature(void)
* 13. uint16_t MeColorSensor::calculateLux(void)
* 14. int8_t MeColorSensor::writeReg(int16_t reg, uint8_t data)
* 15. int8_t MeColorSensor::readData(uint8_t start, uint8_t *buffer, uint8_t size)
* 16. int8_t MeColorSensor::writeData(uint8_t start, const uint8_t *pData, uint8_t size)
* 17. uint8_t MeColorSensor::Returnresult(void);
* 18. uint8_t MeColorSensor::ReturnGrayscale(void);
* 19. uint16_t MeColorSensor::ReturnColorhue(void);
* 20. uint8_t MeColorSensor::MAX(uint8_t r,uint8_t g,uint8_t b);
* 21. uint8_t MeColorSensor::MIN(uint8_t r,uint8_t g,uint8_t b);
* 22. void MeColorSensor::TurnOffmodule(void);
* 23. void MeColorSensor::TurnOnmodule(void);
* 24. uint8_t MeColorSensor::ColorDataReadOnebyOne();
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* zzipeng 2017/01/17 1.0.0 complete the driver code.
* zzipeng 2017/04/03 1.0.1 only detect six colors.
* zzipeng 2017/04/10 1.0.2 only detect seven colors and add methods named MeColorSensor::TurnOffmodule(void),MeColorSensor::TurnOnmodule.
* zzipeng 2017/04/20 1.0.3 add methods MeColorSensor::ColorDataReadOnebyOne();
* Lanweiting 2017/06/23 1.0.4 Canonical the code format.
* </pre>
*
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _MeColorSensor_H_
#define _MeColorSensor_H_
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <Arduino.h>
#include "MeConfig.h"
#include "Wire.h"
#ifdef ME_PORT_DEFINED
#include "MePort.h"
#endif // ME_PORT_DEFINED
/* Exported macro ------------------------------------------------------------*/
#define I2C_ERROR (-1)
#define COLORSENSOR_DEFAULT_ADDRESS (0x38)//0011 1000
/* Register List define------------------------------------------------------------*/
#define SYSTEM_CONTROL (0X40)
#define MODE_CONTROL1 (0X41)
#define MODE_CONTROL2 (0X42)
#define MODE_CONTROL3 (0X44)
#define RED_DATA_LSBs (0X50)
#define RED_DATA_MSBs (0X51)
#define GREEN_DATA_LSBs (0X52)
#define GREEN_DATA_MSBs (0X53)
#define BLUE_DATA_LSBs (0X54)
#define BLUE_DATA_MSBs (0X55)
#define CLEAR_DATA_LSBs (0X56)
#define CLEAR_DATA_MSBs (0X57)
#define DINT_DATA_LSBs (0X58)
#define DINT_DATA_MSBs (0X59)
#define INTERRUPT (0X60)
#define PERSISTENCE (0X61)
#define TH_LSBs (0X62)
#define TH_MSBs (0X63)
#define TL_LSBs (0X64)
#define TL_MSBs (0X65)
#define MANUFACTURER_ID (0X92)
#define CHIP_ID (0XE0)
#define SW_RESET (1 << 7)
#define INT_RESET (1 << 6)
/* REG_MODECONTROL1(0x41) */
#define MEASURE_160MS (0x00)
#define MEASURE_320MS (0x01)
#define MEASURE_640MS (0x02)
#define MEASURE_1280MS (0x03)
#define MEASURE_2560MS (0x04)
#define MEASUREMENT_MAX (0x05)
/* Color result list*/
typedef enum
{
WHITE = 0,
PINKE = 1,
RED = 2,
ORANGE= 3,
YELLOW= 4,
GREEN = 5,
CYAN = 6,
BLUE = 7,
PURPLE= 8,
BLACK = 9,
GOLD = 10,
}COLORTYPES;
/*
* Class: MeColorSensor
* \par Description
* Declaration of Class MeColorSensor
*/
#ifndef ME_PORT_DEFINED
class MeColorSensor
#else // !ME_PORT_DEFINED
class MeColorSensor : public MePort
#endif // !ME_PORT_DEFINED
{
public:
#ifdef ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the MeColorSensor to arduino port,
* no pins are used or initialized here
*/
MeColorSensor(void);
/**
* Alternate Constructor which can call your own function to map the MeColorSensor to arduino port.
* \param[in]
* port - RJ25 port number
*/
MeColorSensor(uint8_t port);
/**
* Alternate Constructor which can call your own function to map the MeColorSensor to arduino port
* and change the i2c device address
* \param[in]
* port - RJ25 port number
* \param[in]
* address - the i2c address you want to set
*/
MeColorSensor(uint8_t port, uint8_t address);
#else // ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the _AD0 and _INT to arduino port,
* \param[in]
* _AD0 - arduino gpio number
* \param[in]
* _INT - arduino gpio number
*/
MeColorSensor(uint8_t _AD0, uint8_t _INT);
/**
* Alternate Constructor which can call your own function to map the _AD0 and _INT to arduino port
* and change the i2c device address.
* \param[in]
* _AD0 - arduino gpio number
* \param[in]
* _INT - arduino gpio number
* \param[in]
* address - the i2c address you want to set
*/
MeColorSensor(uint8_t _AD0, uint8_t _INT, uint8_t address);
#endif /* ME_PORT_DEFINED */
/**
* \par Function
* SensorInit
* \par Description
* Initialize the MeColorSensor.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* You can check the bh1745 datasheet for the registor address.
*/
void SensorInit(void);
/**
* \par Function
* ReportId
* \par Description
* Report the MeColorSensor Module ID.
* \param[in]
* None
* \par Output
* None
* \return
* default ID 0xE0
* \par Others
* None
*/
uint8_t ReportId(void);
/**
* \par Function
* ColorDataRead
* \par Description
* read the MeColorSensor module color data.
* \param[in]
* None
* \par Output
* color RGB value
* \return
* None
* \par Others
* None
*/
void ColorDataRead(void);
/**
* \par Function
* ColorDataReadOnebyOne
* \par Description
* one by on to read the MeColorSensor module color data.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint8_t ColorDataReadOnebyOne(void);
/**
* \par Function
* ReturnColorCode
* \par Description
* Return the MeColorSensor Color Code.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
long ReturnColorCode(void);
/**
* \par Function
* ColorIdentify
* \par Description
* Identify Color for the Module.when you want to use two modules, you can use this methods to get data.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint8_t ColorIdentify(void);
/**
* \par Function
* Returnresult
* \par Description
* Identify Color for the Module.when you use just one module, you can use this methods to get data.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint8_t Returnresult(void);
/**
* \par Function
* ReturnGrayscale
* \par Description
* Return Color Grayscale.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* Gray = R*0.299 + G*0.587 + B*0.114
* Gray = (r*38 + g*75 + b*15)>>7;
*/
uint8_t ReturnGrayscale(void);
/**
* \par Function
* ReturnColorhue
* \par Description
* Return Color hue.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint16_t ReturnColorhue(void);
/**
* \par Function
* ReturnRedData
* \par Description
* Return Color Red value.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint16_t ReturnRedData(void);
/**
* \par Function
* ReturnGreenData
* \par Description
* Return Color Green value.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint16_t ReturnGreenData(void);
/**
* \par Function
* ReturnBlueData
* \par Description
* Return Color Blue value.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint16_t ReturnBlueData(void);
/**
* \par Function
* ReturnColorData
* \par Description
* Return Color data value.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
uint16_t ReturnColorData(void);
/**
* \par Function
* TurnOnLight
* \par Description
* Turn On the MeColorSensor module Light.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void TurnOnLight(void);
/**
* \par Function
* TurnOffLight
* \par Description
* Turn Off the MeColorSensor module Light.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void TurnOffLight(void);
/**
* \par Function
* TurnOffmodule
* \par Description
* Turn Off the MeColorSensor module.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void TurnOffmodule(void);
/**
* \par Function
* TurnOnmodule
* \par Description
* Turn On the MeColorSensor module.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void TurnOnmodule(void);
/**
* \par Function
* writeReg
* \par Description
* write the MeColorSensor module register.
* \param[in]
* None
* \par Output
* None
* \return
* Return the error code.
* the definition of the value of variable return_value:
* 0:success
* 1:BUFFER_LENGTH is shorter than size
* 2:address send, nack received
* 3:data send, nack received
* 4:other twi error
* refer to the arduino official library twi.c
* \par Others
* To set the register for initializing.
*/
int8_t writeReg(int16_t reg, uint8_t data);
/**
* \par Function
* readData
* \par Description
* Write the data to i2c device.
* \param[in]
* start - the address which will write the data to.
* \param[in]
* pData - the head address of data array.
* \param[in]
* size - set the number of data will be written to the devide.
* \par Output
* None
* \return
* Return the error code.
* the definition of the value of variable return_value:
* 0:success
* 1:BUFFER_LENGTH is shorter than size
* 2:address send, nack received
* 3:data send, nack received
* 4:other twi error
* refer to the arduino official library twi.c
* \par Others
* Calling the official i2c library to read data.
*/
int8_t readData(uint8_t start, uint8_t *buffer, uint8_t size);
/**
* \par Function
* writeData
* \par Description
* Write the data to i2c device.
* \param[in]
* start - the address which will write the data to.
* \param[in]
* pData - the head address of data array.
* \param[in]
* size - set the number of data will be written to the devide.
* \par Output
* None
* \return
* Return the error code.
* the definition of the value of variable return_value:
* 0:success
* 1:BUFFER_LENGTH is shorter than size
* 2:address send, nack received
* 3:data send, nack received
* 4:other twi error
* refer to the arduino official library twi.c
* \par Others
* Calling the official i2c library to write data.
*/
int8_t writeData(uint8_t start, const uint8_t *pData, uint8_t size);
/**
* \par Function
* MAX
* \par Description
* find the max one from r g b.
* \param[in]
* r g b
* \par Output
* None
* \return
* Return the max one.
* \par Others
*/
uint8_t MAX(uint8_t r,uint8_t g,uint8_t b);
/**
* \par Function
* MIN
* \par Description
* find the min one from r g b.
* \param[in]
* r g b
* \par Output
* None
* \return
* Return the min one.
* \par Others
*/
uint8_t MIN(uint8_t r,uint8_t g,uint8_t b);
/***********************************/
uint8_t Device_Address;
private:
volatile uint8_t _S1;
volatile uint8_t _S2;
volatile uint8_t _AD0;
volatile uint8_t _INT;
uint16_t Redvalue;
uint16_t Greenvalue;
uint16_t Bluevalue;
uint16_t Colorvalue;
};
#endif