Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / LightRing / LightRing.cpp @ f0393a37

History | View | Annotate | Download (2.777 KB)

1
#include "ch.hpp"
2
#include "hal.h"
3

    
4
#include "LightRing.h"
5

    
6
#include <global.hpp>
7

    
8
using namespace chibios_rt;
9
using namespace amiro;
10
using namespace types;
11

    
12
extern volatile uint32_t shutdown_now;
13
extern Global global;
14

    
15

    
16
LightRing::LightRing(CANDriver *can, TLC5947 *tlc5947, fileSystemIo::FSIOLightRing *memory)
17
    : ControllerAreaNetworkTx(can, CAN::LIGHT_RING_ID),
18
      ControllerAreaNetworkRx(can, CAN::LIGHT_RING_ID),
19
      tlc5947(tlc5947),
20
      memory(memory) {
21
  chDbgCheck(tlc5947 != NULL, "LightRing");
22
}
23

    
24
void LightRing::setLightBrightness(int brightness) {
25
  this->tlc5947->setBrightness(brightness);
26
  this->tlc5947->update();
27
}
28

    
29
void LightRing::setLightColor(int index, Color color) {
30
  this->tlc5947->setColor(index, color);
31
  this->tlc5947->update();
32
}
33

    
34
msg_t LightRing::receiveMessage(CANRxFrame *frame) {
35
  int deviceId = this->decodeDeviceId(frame);
36

    
37
  switch (deviceId) {
38

    
39
    case CAN::SHELL_REPLY_ID(CAN::LIGHT_RING_ID):
40
      if (frame->DLC > 0) {
41
        sdWrite(&SD1, frame->data8, frame->DLC);
42
        return RDY_OK;
43
      }
44
      break;
45

    
46
    case CAN::SHELL_QUERY_ID(CAN::LIGHT_RING_ID):
47
      if (frame->DLC != 0) {
48
        global.sercanmux1.convCan2Serial(frame->data8, frame->DLC);
49
        return RDY_OK;
50
      } else {
51
        global.sercanmux1.rcvSwitchCmd(this->decodeBoardId(frame));
52
        return RDY_OK;
53
      }
54
      break;
55

    
56
    case CAN::COLOR_ID(0):
57
    case CAN::COLOR_ID(1):
58
    case CAN::COLOR_ID(2):
59
    case CAN::COLOR_ID(3):
60
    case CAN::COLOR_ID(4):
61
    case CAN::COLOR_ID(5):
62
    case CAN::COLOR_ID(6):
63
    case CAN::COLOR_ID(7):
64
      if (frame->DLC == 3) {
65
        int index = deviceId & 0x7;
66
        Color color(frame->data8[0], frame->data8[1], frame->data8[2]);
67
        this->setLightColor(index, color);
68
        return RDY_OK;
69
      }
70
      break;
71

    
72
    case CAN::BRIGHTNESS_ID:
73
      if (frame->DLC == 1) {
74
        int brightness = frame->data8[0];
75
        this->setLightBrightness(brightness);
76
        return RDY_OK;
77
      }
78
      break;
79

    
80
    case CAN::BROADCAST_SHUTDOWN:
81
      if (frame->DLC == 2 && frame->data16[0] == CAN::SHUTDOWN_MAGIC) {
82
        shutdown_now = 0x4;
83
        return RDY_OK;
84
      }
85
      break;
86

    
87
    case CAN::ROBOT_ID:
88
      if (frame->DLC == 1) {
89
        this->robotId = frame->data8[0];
90
        return RDY_OK;
91
      }
92
      break;
93

    
94
    default:
95
      break;
96
  }
97
  return -1;
98
}
99

    
100
ThreadReference LightRing::start(tprio_t PRIO) {
101
  this->ControllerAreaNetworkRx::start(PRIO + 1);
102
  this->ControllerAreaNetworkTx::start(PRIO);
103
  return NULL;
104
}
105

    
106
msg_t
107
LightRing::terminate(void) {
108
  msg_t ret = RDY_OK;
109

    
110
  this->ControllerAreaNetworkTx::requestTerminate();
111
  ret |= this->ControllerAreaNetworkTx::wait();
112
  this->ControllerAreaNetworkRx::requestTerminate();
113
  ret |= this->ControllerAreaNetworkRx::wait();
114

    
115
  return ret;
116
}
117

    
118
void LightRing::periodicBroadcast() {
119

    
120
}