Skip to content

Commit

Permalink
Added GrovePi demo program which uses the light sensor, button, lcd a…
Browse files Browse the repository at this point in the history
…nd led addons.
  • Loading branch information
nieklinnenbank committed Nov 5, 2015
1 parent 639d8ce commit a2445b4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
64 changes: 64 additions & 0 deletions server/i2c/grovepi/Demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2015 Niek Linnenbank
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <FreeNOS/Config.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv)
{
int greenLed = open("/dev/groveDigi4", O_WRONLY);
int button = open("/dev/groveDigi3", O_RDONLY);
int light = open("/dev/groveAnalog0", O_RDONLY);
int lcd = open("/dev/groveLCD", O_WRONLY);
char buf[32], text[32];

if (greenLed < 0 || button < 0 || light < 0 || lcd < 0)
{
printf("%s: failed to open GrovePi devices: %s\n",
argv[0], strerror(errno));
return EXIT_FAILURE;
}

while (1)
{
// Reset fds
lseek(button, 0, SEEK_SET);
lseek(greenLed, 0, SEEK_SET);
lseek(light, 0, SEEK_SET);
lseek(lcd, 0, SEEK_SET);

// Copy button value to the green LED
read(button, buf, sizeof(buf));
write(greenLed, buf, 1);

// Display light status on the LCD
read(light, buf, sizeof(buf));
snprintf(text, sizeof(text), "light: %s\nFreeNOS %s",
buf, VERSION);
write(lcd, text, sizeof(text));

// Sleep a bit
sleep(1);
}

return EXIT_SUCCESS;
}
3 changes: 2 additions & 1 deletion server/i2c/grovepi/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ env = build_env.Clone()
if env['SYSTEM'] == 'raspberry':
env.UseServers(['log', 'filesystem', 'core'])
env.UseLibraries([ 'libposix', 'liballoc', 'libstd', 'libarch', 'libexec', 'libi2c' ])
env.TargetProgram('server', [ Glob('*.cpp') ])
env.TargetProgram('server', [ 'AnalogPort.cpp', 'DigitalPort.cpp', 'Main.cpp', 'LCDBar.cpp' ])
env.TargetProgram('demo', [ 'Demo.cpp' ])

0 comments on commit a2445b4

Please sign in to comment.