-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedInterface.cpp
69 lines (60 loc) · 1.64 KB
/
LedInterface.cpp
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
/**
* @file LedInterface.cpp
*
* @brief LedInterface module
*
* This file implements the LedInterface module, which acts as a bridge between
* the Led class and the LedDriver of the other running process. This module
* updates the shared memory used by both processes with the desired LED states
*
* @author Bryan Cisneros
*/
#include "LedInterface.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/shm.h>
#define ON ((uint8_t)0xFF)
#define OFF ((uint8_t)0x00)
typedef struct
{
bool update;
char led_values[12];
} shared_leds_t;
void* shared_pointer = NULL;
shared_leds_t* shared_leds;
int shared_leds_id;
void LedInterface_init(void)
{
// get shared memory
shared_leds_id = shmget((key_t)1234, sizeof(shared_leds_t), 0);
if (shared_leds_id == -1)
{
printf("failed to get shared memory\n");
}
shared_pointer = shmat(shared_leds_id, (void*)0, 0);
if (shared_pointer == (void*)-1)
{
printf("shmat() failed!\n");
}
shared_leds = (shared_leds_t*)shared_pointer;
// Initialize all LEDs to off
for (int i = 0; i < 12; i++)
{
shared_leds->led_values[i] = OFF;
}
shared_leds->update = true;
}
void LedInterface_turnOnLed(int channel)
{
// Update value in shared memory and set update flag.
// The LED driver will then update the LED accordingly
shared_leds->led_values[channel] = ON;
shared_leds->update = true;
}
void LedInterface_turnOffLed(int channel)
{
// Update value in shared memory and set update flag.
// The LED driver will then update the LED accordingly
shared_leds->led_values[channel] = OFF;
shared_leds->update = true;
}