smalrubot 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ /*--------------------------------------------------------------------
2
+ This file is part of the Adafruit NeoPixel library.
3
+
4
+ NeoPixel is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as
6
+ published by the Free Software Foundation, either version 3 of
7
+ the License, or (at your option) any later version.
8
+
9
+ NeoPixel is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public
15
+ License along with NeoPixel. If not, see
16
+ <http://www.gnu.org/licenses/>.
17
+ --------------------------------------------------------------------*/
18
+
19
+ #ifndef ADAFRUIT_NEOPIXEL_H
20
+ #define ADAFRUIT_NEOPIXEL_H
21
+
22
+ #if (ARDUINO >= 100)
23
+ #include <Arduino.h>
24
+ #else
25
+ #include <WProgram.h>
26
+ #include <pins_arduino.h>
27
+ #endif
28
+
29
+ // 'type' flags for LED pixels (third parameter to constructor):
30
+ #define NEO_RGB 0x00 // Wired for RGB data order
31
+ #define NEO_GRB 0x01 // Wired for GRB data order
32
+ #define NEO_BRG 0x04
33
+
34
+ #define NEO_COLMASK 0x01
35
+ #define NEO_KHZ800 0x02 // 800 KHz datastream
36
+ #define NEO_SPDMASK 0x02
37
+ // Trinket flash space is tight, v1 NeoPixels aren't handled by default.
38
+ // Remove the ifndef/endif to add support -- but code will be bigger.
39
+ // Conversely, can comment out the #defines to save space on other MCUs.
40
+ #ifndef __AVR_ATtiny85__
41
+ #define NEO_KHZ400 0x00 // 400 KHz datastream
42
+ #endif
43
+
44
+ class Adafruit_NeoPixel {
45
+
46
+ public:
47
+
48
+ // Constructor: number of LEDs, pin number, LED type
49
+ Adafruit_NeoPixel(uint16_t n, uint8_t p=6, uint8_t t=NEO_GRB + NEO_KHZ800);
50
+ ~Adafruit_NeoPixel();
51
+
52
+ void
53
+ begin(void),
54
+ show(void),
55
+ setPin(uint8_t p),
56
+ setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b),
57
+ setPixelColor(uint16_t n, uint32_t c),
58
+ setBrightness(uint8_t),
59
+ clear();
60
+ uint8_t
61
+ *getPixels(void) const,
62
+ getBrightness(void) const;
63
+ uint16_t
64
+ numPixels(void) const;
65
+ static uint32_t
66
+ Color(uint8_t r, uint8_t g, uint8_t b);
67
+ uint32_t
68
+ getPixelColor(uint16_t n) const;
69
+ inline bool
70
+ canShow(void) { return (micros() - endTime) >= 50L; }
71
+
72
+ private:
73
+
74
+ const uint16_t
75
+ numLEDs, // Number of RGB LEDs in strip
76
+ numBytes; // Size of 'pixels' buffer below
77
+ uint8_t
78
+ pin, // Output pin number
79
+ brightness,
80
+ *pixels, // Holds LED color values (3 bytes each)
81
+ rOffset, // Index of red byte within each 3-byte pixel
82
+ gOffset, // Index of green byte
83
+ bOffset; // Index of blue byte
84
+ const uint8_t
85
+ type; // Pixel flags (400 vs 800 KHz, RGB vs GRB color)
86
+ uint32_t
87
+ endTime; // Latch timing reference
88
+ #ifdef __AVR__
89
+ const volatile uint8_t
90
+ *port; // Output PORT register
91
+ uint8_t
92
+ pinMask; // Output PORT bitmask
93
+ #endif
94
+
95
+ };
96
+
97
+ #endif // ADAFRUIT_NEOPIXEL_H
@@ -5,14 +5,41 @@
5
5
  #include "Arduino.h"
6
6
  #include "Smalrubot.h"
7
7
 
8
- Smalrubot::Smalrubot(){
8
+ Smalrubot::Smalrubot(int neo_pixel_num, int neo_pixel_pin) :
9
+ pixels(neo_pixel_num, neo_pixel_pin, NEO_RGB + NEO_KHZ400)
10
+ {
9
11
  reset();
10
12
  }
11
13
 
12
14
  void Smalrubot::parse(char c) {
13
- if (c == '!') index = 0; // Reset request
14
- else if (c == '.') process(); // End request and process
15
- else request[index++] = c; // Append to request
15
+ if (c == '!') {
16
+ index = 0;
17
+ receivingRequest = true;
18
+ }
19
+ else if (receivingRequest) {
20
+ if (c == '.') {
21
+ process();
22
+ receivingRequest = false;
23
+ }
24
+ else if (index < MAX_REQUEST_LENGTH) {
25
+ request[index++] = c;
26
+ }
27
+ else {
28
+ receivingRequest = false;
29
+ }
30
+ }
31
+ }
32
+
33
+
34
+ int Smalrubot::parseRequestValue(int n) {
35
+ if (index < 4 + (n + 1) * 3) {
36
+ return 0;
37
+ }
38
+
39
+ strncpy(valStr, request + 4 + n * 3, 3);
40
+ valStr[3] = '\0';
41
+
42
+ return atoi(valStr);
16
43
  }
17
44
 
18
45
  void Smalrubot::process() {
@@ -21,10 +48,9 @@ void Smalrubot::process() {
21
48
  // Parse the request.
22
49
  strncpy(cmdStr, request, 2); cmdStr[2] = '\0';
23
50
  strncpy(pinStr, request + 2, 2); pinStr[2] = '\0';
24
- strncpy(valStr, request + 4, 3); valStr[3] = '\0';
25
51
  cmd = atoi(cmdStr);
26
52
  pin = atoi(pinStr);
27
- val = atoi(valStr);
53
+ val = parseRequestValue(0);
28
54
 
29
55
  #ifdef debug
30
56
  Serial.print("Received request - "); Serial.println(request);
@@ -48,21 +74,50 @@ void Smalrubot::process() {
48
74
  void Smalrubot::processCommand() {
49
75
  // Call the command.
50
76
  switch(cmd) {
51
- case 0: setMode (); break;
52
- case 1: dWrite (); break;
53
- case 2: dRead (); break;
54
- case 3: aWrite (); break;
55
- case 4: aRead (); break;
56
- case 8: servoToggle (); break;
57
- case 9: servoWrite (); break;
58
- case 90: reset (); break;
59
- default: break;
77
+ case 0:
78
+ setMode();
79
+ break;
80
+ case 1:
81
+ dWrite();
82
+ break;
83
+ case 2:
84
+ dRead();
85
+ break;
86
+ case 3:
87
+ aWrite();
88
+ break;
89
+ case 4:
90
+ aRead();
91
+ break;
92
+ case 8:
93
+ servoToggle();
94
+ break;
95
+ case 9:
96
+ servoWrite();
97
+ break;
98
+ case 10:
99
+ setNeoPixelPin();
100
+ break;
101
+ case 11:
102
+ setNeoPixelNumPixels();
103
+ break;
104
+ case 12:
105
+ setNeoPixelColor();
106
+ break;
107
+ case 13:
108
+ showNeoPixel();
109
+ break;
110
+ case 90:
111
+ reset();
112
+ break;
113
+ default:
114
+ break;
60
115
  }
61
116
  }
62
117
 
63
-
64
118
  // WRITE CALLBACK
65
119
  void Smalrubot::setupWrite(void (*writeCallback)(char *str)) {
120
+ pixels.begin();
66
121
  _writeCallback = writeCallback;
67
122
  }
68
123
  void Smalrubot::writeResponse() {
@@ -153,10 +208,47 @@ void Smalrubot::servoWrite() {
153
208
  servos[pin - SERVO_OFFSET].write(val);
154
209
  }
155
210
 
211
+ // CMD = 10
212
+ void Smalrubot::setNeoPixelPin() {
213
+ #ifdef debug
214
+ Serial.print("set NeoPixel pin "); Serial.println(pin);
215
+ #endif
216
+ pixels.setPin(pin);
217
+ }
218
+
219
+ // CMD = 11
220
+ void Smalrubot::setNeoPixelNumPixels() {
221
+ #ifdef debug
222
+ Serial.print("set NeoPixel num pixels "); Serial.println(val);
223
+ #endif
224
+ // not support yet
225
+ // pixels.setNumPixels(val);
226
+ }
227
+
228
+ // CMD = 12
229
+ void Smalrubot::setNeoPixelColor() {
230
+ #ifdef debug
231
+ Serial.print("set NeoPixel color "); Serial.println(val);
232
+ #endif
233
+ uint16_t n = pin;
234
+ uint8_t r = val, g = parseRequestValue(1), b = parseRequestValue(2);
235
+
236
+ pixels.setPixelColor(n, pixels.Color(r, g, b));
237
+ }
238
+
239
+ // CMD = 13
240
+ void Smalrubot::showNeoPixel() {
241
+ #ifdef debug
242
+ Serial.println("show NeoPixel");
243
+ #endif
244
+ pixels.show();
245
+ }
246
+
156
247
  // CMD = 90
157
248
  void Smalrubot::reset() {
158
249
  #ifdef debug
159
250
  Serial.println("Reset the board to defaults.");
160
251
  #endif
161
252
  sprintf(response, "ACK:%02d", A0);
253
+ receivingRequest = false;
162
254
  }
@@ -7,6 +7,8 @@
7
7
 
8
8
  #include "Arduino.h"
9
9
  #include <Servo.h>
10
+ #include "Adafruit_NeoPixel.h"
11
+ #include <avr/power.h>
10
12
 
11
13
  // Allocate listener storage based on what board we're running.
12
14
  #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
@@ -20,17 +22,20 @@
20
22
  // Uncomment this line to enable debugging mode.
21
23
  // #define debug true
22
24
 
25
+ #define MAX_REQUEST_LENGTH 20
26
+
23
27
  class Smalrubot {
24
28
  public:
25
- Smalrubot();
29
+ Smalrubot(int neo_pixel_num, int neo_pixel_pin);
26
30
  void setupWrite(void (*writeCallback)(char *str));
27
31
  void parse(char c);
28
32
  void process();
29
33
 
30
34
  protected:
31
35
  // Request storage.
32
- char request[8];
36
+ char request[MAX_REQUEST_LENGTH];
33
37
  int index;
38
+ bool receivingRequest;
34
39
  char cmdStr[3];
35
40
  byte cmd;
36
41
  char pinStr[3];
@@ -46,17 +51,24 @@ class Smalrubot {
46
51
 
47
52
  Servo servos[12];
48
53
 
54
+ Adafruit_NeoPixel pixels;
55
+
49
56
  virtual void processCommand();
57
+ int parseRequestValue(int n);
50
58
 
51
59
  // API-accessible functions.
52
- void setMode ();
53
- void dWrite ();
54
- void dRead ();
55
- void aWrite ();
56
- void aRead ();
57
- void servoToggle ();
58
- void servoWrite ();
59
- void reset ();
60
+ void setMode();
61
+ void dWrite();
62
+ void dRead();
63
+ void aWrite();
64
+ void aRead();
65
+ void servoToggle();
66
+ void servoWrite();
67
+ void setNeoPixelPin();
68
+ void setNeoPixelNumPixels();
69
+ void setNeoPixelColor();
70
+ void showNeoPixel();
71
+ void reset();
60
72
  };
61
73
 
62
74
  #endif
@@ -2,7 +2,9 @@
2
2
 
3
3
  typedef Smalrubot base;
4
4
 
5
- SrStuduino::SrStuduino() {
5
+ SrStuduino::SrStuduino(int neo_pixel_num, int neo_pixel_pin) :
6
+ Smalrubot(neo_pixel_num, neo_pixel_pin)
7
+ {
6
8
  }
7
9
 
8
10
  /*
@@ -10,7 +10,7 @@
10
10
 
11
11
  class SrStuduino : public Smalrubot {
12
12
  public:
13
- SrStuduino();
13
+ SrStuduino(int neo_pixel_num, int neo_pixel_pin);
14
14
 
15
15
  protected:
16
16
  Studuino studuino;
@@ -1,6 +1,12 @@
1
1
  #include "Smalrubot.h"
2
2
  #include <Servo.h>
3
- Smalrubot smalrubot;
3
+ #include "Adafruit_NeoPixel.h"
4
+ #include <avr/power.h>
5
+
6
+ #define DEFAULT_NEO_PIXEL_NUM 16
7
+ #define DEFAULT_NEO_PIXEL_PIN 5
8
+
9
+ Smalrubot smalrubot(DEFAULT_NEO_PIXEL_NUM, DEFAULT_NEO_PIXEL_PIN);
4
10
 
5
11
  // Smalrubot.h doesn't handle TXRX. Setup a function to tell it to write to Serial.
6
12
  void writeResponse(char *response) { Serial.println(response); }
@@ -1,10 +1,15 @@
1
1
  #include "SrStuduino.h"
2
2
  #include <Servo.h>
3
+ #include "Adafruit_NeoPixel.h"
4
+ #include <avr/power.h>
3
5
  #include <Wire.h>
4
6
  #include <MMA8653.h>
5
7
  #include "Studuino.h"
6
8
 
7
- SrStuduino smalrubot;
9
+ #define DEFAULT_NEO_PIXEL_NUM 16
10
+ #define DEFAULT_NEO_PIXEL_PIN 5
11
+
12
+ SrStuduino smalrubot(DEFAULT_NEO_PIXEL_NUM, DEFAULT_NEO_PIXEL_PIN);
8
13
 
9
14
  // Smalrubot.h doesn't handle TXRX. Setup a function to tell it to write to Serial.
10
15
  void writeResponse(char *response) { Serial.println(response); }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalrubot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-01 00:00:00.000000000 Z
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyserial
@@ -95,6 +95,8 @@ files:
95
95
  - lib/smalrubot/tx_rx/base.rb
96
96
  - lib/smalrubot/tx_rx/serial.rb
97
97
  - lib/smalrubot/version.rb
98
+ - sketch/lib/Adafruit_NeoPixel.cpp
99
+ - sketch/lib/Adafruit_NeoPixel.h
98
100
  - sketch/lib/Smalrubot.cpp
99
101
  - sketch/lib/Smalrubot.h
100
102
  - sketch/lib/SrStuduino.cpp