Psi Swarm robot library version 0.9

Fork of PsiSwarmV9 by Psi Swarm Robot

Committer:
jah128
Date:
Mon May 14 15:35:38 2018 +0000
Revision:
20:1bc6c6dc477b
Parent:
16:50686c07ad07
Updated?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /* University of York Robotics Laboratory PsiSwarm Library: LED Functions Source File
jah128 9:dde9e21030eb 2 *
jah128 16:50686c07ad07 3 * Copyright 2017 University of York
jah128 6:b340a527add9 4 *
jah128 9:dde9e21030eb 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 6:b340a527add9 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 6:b340a527add9 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 9:dde9e21030eb 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 6:b340a527add9 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 6:b340a527add9 10 *
jah128 0:d6269d17c8cf 11 * File: led.cpp
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 14 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 15 *
jah128 16:50686c07ad07 16 * PsiSwarm Library Version: 0.9
jah128 0:d6269d17c8cf 17 *
jah128 16:50686c07ad07 18 * June 2017
jah128 0:d6269d17c8cf 19 *
jah128 0:d6269d17c8cf 20 *
jah128 0:d6269d17c8cf 21 */
jah128 0:d6269d17c8cf 22
jah128 0:d6269d17c8cf 23 #include "psiswarm.h"
jah128 0:d6269d17c8cf 24
jah128 0:d6269d17c8cf 25 char green_led_states;
jah128 0:d6269d17c8cf 26 char red_led_states;
jah128 0:d6269d17c8cf 27 char center_led_state;
jah128 0:d6269d17c8cf 28
jah128 0:d6269d17c8cf 29 char held_red_states;
jah128 0:d6269d17c8cf 30 char held_green_states;
jah128 0:d6269d17c8cf 31 Timeout blink_led_timeout;
jah128 0:d6269d17c8cf 32
jah128 9:dde9e21030eb 33 unsigned short Led::get_led_states()
jah128 9:dde9e21030eb 34 {
jah128 9:dde9e21030eb 35 return (green_led_states << 8) + red_led_states;
jah128 0:d6269d17c8cf 36 }
jah128 0:d6269d17c8cf 37
jah128 9:dde9e21030eb 38 void Led::set_leds(char green, char red)
jah128 9:dde9e21030eb 39 {
jah128 0:d6269d17c8cf 40 green_led_states = green;
jah128 0:d6269d17c8cf 41 red_led_states = red;
jah128 0:d6269d17c8cf 42 IF_update_leds();
jah128 0:d6269d17c8cf 43 }
jah128 0:d6269d17c8cf 44
jah128 9:dde9e21030eb 45 void Led::set_base_led(char state)
jah128 9:dde9e21030eb 46 {
jah128 11:312663037b8c 47 i2c_setup.IF_set_base_LED(state);
jah128 0:d6269d17c8cf 48 }
jah128 0:d6269d17c8cf 49
jah128 9:dde9e21030eb 50 void Led::set_green_leds(char green)
jah128 9:dde9e21030eb 51 {
jah128 0:d6269d17c8cf 52 green_led_states = green;
jah128 0:d6269d17c8cf 53 IF_update_leds();
jah128 0:d6269d17c8cf 54 }
jah128 0:d6269d17c8cf 55
jah128 9:dde9e21030eb 56 void Led::set_red_leds(char red)
jah128 9:dde9e21030eb 57 {
jah128 0:d6269d17c8cf 58 red_led_states = red;
jah128 0:d6269d17c8cf 59 IF_update_leds();
jah128 0:d6269d17c8cf 60 }
jah128 0:d6269d17c8cf 61
jah128 9:dde9e21030eb 62 void Led::set_led(char led, char state)
jah128 9:dde9e21030eb 63 {
jah128 0:d6269d17c8cf 64 if(state % 2 == 1) red_led_states |= 1 << led;
jah128 0:d6269d17c8cf 65 else red_led_states &= ~(1 << led);
jah128 0:d6269d17c8cf 66 if(state / 2) green_led_states |= 1 << led;
jah128 0:d6269d17c8cf 67 else green_led_states &= ~(1 << led);
jah128 9:dde9e21030eb 68 IF_update_leds();
jah128 0:d6269d17c8cf 69 }
jah128 0:d6269d17c8cf 70
jah128 9:dde9e21030eb 71 void Led::blink_leds(float timeout)
jah128 9:dde9e21030eb 72 {
jah128 0:d6269d17c8cf 73 save_led_states();
jah128 0:d6269d17c8cf 74 set_leds(0xFF,0xFF);
jah128 9:dde9e21030eb 75 blink_led_timeout.attach(this,&Led::restore_led_states, timeout);
jah128 0:d6269d17c8cf 76 }
jah128 0:d6269d17c8cf 77
jah128 9:dde9e21030eb 78 void Led::set_center_led(char state)
jah128 9:dde9e21030eb 79 {
jah128 0:d6269d17c8cf 80 set_center_led(state, center_led_brightness);
jah128 0:d6269d17c8cf 81 }
jah128 0:d6269d17c8cf 82
jah128 9:dde9e21030eb 83 void Led::set_center_led(char state, float brightness)
jah128 9:dde9e21030eb 84 {
jah128 0:d6269d17c8cf 85 center_led_brightness = brightness;
jah128 0:d6269d17c8cf 86 center_led_state = state;
jah128 9:dde9e21030eb 87 switch(center_led_state) {
jah128 9:dde9e21030eb 88 case 0:
jah128 9:dde9e21030eb 89 center_led_red.write(0);
jah128 9:dde9e21030eb 90 center_led_green.write(0);
jah128 9:dde9e21030eb 91 break;
jah128 9:dde9e21030eb 92 case 1:
jah128 9:dde9e21030eb 93 center_led_red.write(center_led_brightness / 4);
jah128 9:dde9e21030eb 94 center_led_green.write(0);
jah128 9:dde9e21030eb 95 break;
jah128 9:dde9e21030eb 96 case 2:
jah128 9:dde9e21030eb 97 center_led_red.write(0);
jah128 9:dde9e21030eb 98 center_led_green.write(center_led_brightness);
jah128 9:dde9e21030eb 99 break;
jah128 9:dde9e21030eb 100 case 3:
jah128 9:dde9e21030eb 101 center_led_red.write(center_led_brightness / 4);
jah128 9:dde9e21030eb 102 center_led_green.write(center_led_brightness);
jah128 9:dde9e21030eb 103 break;
jah128 0:d6269d17c8cf 104 }
jah128 0:d6269d17c8cf 105 }
jah128 0:d6269d17c8cf 106
jah128 9:dde9e21030eb 107 void Led::set_center_led_brightness(float brightness)
jah128 9:dde9e21030eb 108 {
jah128 0:d6269d17c8cf 109 set_center_led(center_led_state,brightness);
jah128 0:d6269d17c8cf 110 }
jah128 0:d6269d17c8cf 111
jah128 9:dde9e21030eb 112 void Led::save_led_states()
jah128 9:dde9e21030eb 113 {
jah128 0:d6269d17c8cf 114 held_red_states = red_led_states;
jah128 9:dde9e21030eb 115 held_green_states = green_led_states;
jah128 0:d6269d17c8cf 116 }
jah128 0:d6269d17c8cf 117
jah128 9:dde9e21030eb 118 void Led::restore_led_states()
jah128 9:dde9e21030eb 119 {
jah128 0:d6269d17c8cf 120 set_leds(held_green_states,held_red_states);
jah128 0:d6269d17c8cf 121 }
jah128 0:d6269d17c8cf 122
jah128 9:dde9e21030eb 123 void Led::IF_init_leds()
jah128 9:dde9e21030eb 124 {
jah128 0:d6269d17c8cf 125 green_led_states = 0;
jah128 0:d6269d17c8cf 126 red_led_states = 0;
jah128 0:d6269d17c8cf 127 center_led_red.period_us(100);
jah128 9:dde9e21030eb 128 center_led_green.period_us(100);
jah128 0:d6269d17c8cf 129 set_center_led(0,0.2);
jah128 0:d6269d17c8cf 130 }
jah128 0:d6269d17c8cf 131
jah128 9:dde9e21030eb 132 void Led::IF_update_leds()
jah128 9:dde9e21030eb 133 {
jah128 0:d6269d17c8cf 134 char led_byte_0 = (((green_led_states & (1 << 3)) == 0) << 7) +
jah128 0:d6269d17c8cf 135 (((green_led_states & (1 << 2)) == 0) << 5) +
jah128 0:d6269d17c8cf 136 (((green_led_states & (1 << 1)) == 0) << 3) +
jah128 0:d6269d17c8cf 137 (((green_led_states & (1)) == 0) << 1) +
jah128 0:d6269d17c8cf 138 (((red_led_states & (1 << 3)) == 0) << 6) +
jah128 0:d6269d17c8cf 139 (((red_led_states & (1 << 2)) == 0) << 4) +
jah128 0:d6269d17c8cf 140 (((red_led_states & (1 << 1)) == 0) << 2) +
jah128 0:d6269d17c8cf 141 (((red_led_states & (1)) == 0));
jah128 0:d6269d17c8cf 142 char led_byte_1 = (((green_led_states & (1 << 7)) == 0) << 7) +
jah128 0:d6269d17c8cf 143 (((green_led_states & (1 << 6)) == 0) << 5) +
jah128 0:d6269d17c8cf 144 (((green_led_states & (1 << 5)) == 0) << 3) +
jah128 0:d6269d17c8cf 145 (((green_led_states & (1 << 4)) == 0) << 1) +
jah128 0:d6269d17c8cf 146 (((red_led_states & (1 << 7)) == 0) << 6) +
jah128 0:d6269d17c8cf 147 (((red_led_states & (1 << 6)) == 0) << 4) +
jah128 0:d6269d17c8cf 148 (((red_led_states & (1 << 5)) == 0) << 2) +
jah128 9:dde9e21030eb 149 (((red_led_states & (1 << 4)) == 0));
jah128 11:312663037b8c 150 i2c_setup.IF_write_to_led_ic(led_byte_0,led_byte_1);
jah128 0:d6269d17c8cf 151 }