Mercurial > nkubik
changeset 5:cd880d8c2da3
Move serial to separate file
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Tue, 21 Oct 2014 00:32:19 +0200 |
| parents | cb01d3b5b6a7 |
| children | 76e6c48a6ec6 |
| files | nkubik/beep.ino nkubik/nkubik.ino nkubik/sensor.ino nkubik/serial.ino |
| diffstat | 4 files changed, 81 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nkubik/beep.ino Tue Oct 21 00:32:19 2014 +0200 @@ -0,0 +1,12 @@ +void beep_setup() { + pinMode(A1, OUTPUT); + tone(A1, 440, 100); + delay(100); + tone(A1, 880, 200); + delay(200); + tone(A1, 440, 50); +} + +void beep(unsigned int frequency, unsigned int duration) { + tone(A1, frequency, duration); +}
--- a/nkubik/nkubik.ino Tue Oct 21 00:23:16 2014 +0200 +++ b/nkubik/nkubik.ino Tue Oct 21 00:32:19 2014 +0200 @@ -2,7 +2,7 @@ void setup() { - Serial.begin(115200); + serial_setup(); servo_setup(); sensor_setup(); beep_setup(); @@ -12,57 +12,3 @@ void loop() { do_serial_command(); } - -int16_t _serial_read_word() { - /* Read two bytes from the serial port and make an int from them. */ - int16_t high = Serial.read(); - int16_t low = Serial.read(); - int16_t value = high << 8 | low; - return value; -} - -void _serial_write_word(int16_t value) { - /* Take an int and send it through the serial port as two bytes. */ - int16_t high = (value & 0xff00) >> 8; - int16_t low = value & 0xff; - Serial.write(high); - Serial.write(low); -} - -void do_serial_command() { - static unsigned char serial_command = 0; - /* Read and execute a command from the serial port's buffer. */ - switch (serial_command) { - case 0x00: - /* No command, read one. */ - if (Serial.available() < 1) { return; } - serial_command = Serial.read(); - return; - case 0xff: - /* Move all servos by 1/1000 of radians. */ - if (Serial.available() < SERVOS * 2) { return; } - for (unsigned char servo = 0; servo < SERVOS; ++servo) { - double pos = _serial_read_word(); - servo_move(servo, pos / 1000.0); - } - break; - case 0xfe: - if (Serial.available() < 2) { - return; - } else { - unsigned int frequency = Serial.read() << 3; - unsigned int duration = Serial.read() << 3; - beep(frequency, duration); - } - break; - case 0xfd: - if (Serial.available() < 1) { return; } - blink(Serial.read()); - break; - case 0xf0: - Serial.write(0xff); - _serial_write_word(sensor_read()); - break; - } - serial_command = 0; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nkubik/sensor.ino Tue Oct 21 00:32:19 2014 +0200 @@ -0,0 +1,8 @@ +void sensor_setup() { + pinMode(A0, INPUT); +} + +unsigned int sensor_read() { + unsigned int value = analogRead(A0); + return value; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nkubik/serial.ino Tue Oct 21 00:32:19 2014 +0200 @@ -0,0 +1,60 @@ +void serial_setup() { + Serial.begin(115200); +} + + +int16_t _serial_read_word() { + /* Read two bytes from the serial port and make an int from them. */ + int16_t high = Serial.read(); + int16_t low = Serial.read(); + int16_t value = high << 8 | low; + return value; +} + + +void _serial_write_word(int16_t value) { + /* Take an int and send it through the serial port as two bytes. */ + int16_t high = (value & 0xff00) >> 8; + int16_t low = value & 0xff; + Serial.write(high); + Serial.write(low); +} + + +void do_serial_command() { + static unsigned char serial_command = 0; + /* Read and execute a command from the serial port's buffer. */ + switch (serial_command) { + case 0x00: + /* No command, read one. */ + if (Serial.available() < 1) { return; } + serial_command = Serial.read(); + return; + case 0xff: + /* Move all servos by 1/1000 of radians. */ + if (Serial.available() < SERVOS * 2) { return; } + for (unsigned char servo = 0; servo < SERVOS; ++servo) { + double pos = _serial_read_word(); + servo_move(servo, pos / 1000.0); + } + break; + case 0xfe: + if (Serial.available() < 2) { + return; + } else { + unsigned int frequency = Serial.read() << 3; + unsigned int duration = Serial.read() << 3; + beep(frequency, duration); + } + break; + case 0xfd: + if (Serial.available() < 1) { return; } + blink(Serial.read()); + break; + case 0xf0: + Serial.write(0xff); + _serial_write_word(sensor_read()); + break; + } + serial_command = 0; +}
