Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TI demo smart home backend #1

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
856 changes: 856 additions & 0 deletions demos/high_res/adc.c

Large diffs are not rendered by default.

Binary file added demos/high_res/assets/ti-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions demos/high_res/audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Simple sound playback using ALSA API and libasound.
*
* Compile:
* $ cc -o play sound_playback.c -lasound
*
* Usage:
* $ ./play <sample_rate> <channels> <seconds> < <file>
*
* Examples:
* $ ./play 44100 2 5 < /dev/urandom
* $ ./play 22050 1 8 < /path/to/file.wav
*
div2779 marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (C) 2009 Alessandro Ghedini <[email protected]>
* --------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Alessandro Ghedini wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can
* buy me a beer in return.
* --------------------------------------------------------------
*/

#include <alsa/asoundlib.h>
#include <stdio.h>
#include <pthread.h>

#define PCM_DEVICE "default"
extern pthread_mutex_t playing_now_lock;
extern int playing_now;

void *audio_play() {
unsigned int pcm, tmp, dir;
int rate, channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff;
int buff_size, loops;

rate = 44100;
channels = 2;
seconds = 30;

/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0){
printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
return;
}

/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(&params);

snd_pcm_hw_params_any(pcm_handle, params);

/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0){
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
return;
}

if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE) < 0){
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
return;
}

if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0){
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
return;
}

if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0){
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
return;
}

/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0){
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
return;
}

/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));

printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);

if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");

snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);

printf("seconds: %d\n", seconds);

/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);

buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);

snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

while(1){
int fd = open("/usr/share/ti-lvgl-demo/audio/mixkit-game-show-suspense-waiting-667.wav", O_RDONLY);
if (fd == -1) {
perror("Error opening audio file");
return;
}
for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) {
while(1){
pthread_mutex_lock(&playing_now_lock);
if(playing_now){
pcm = snd_pcm_pause(pcm_handle, 0);
pthread_mutex_unlock(&playing_now_lock);
break;
}else{
pcm = snd_pcm_pause(pcm_handle, 1);
}
pthread_mutex_unlock(&playing_now_lock);
}

if (pcm = read(fd, buff, buff_size) == 0) {
printf("Early end of file.\n");
return 0;
}

if (pcm = snd_pcm_writei(pcm_handle, buff, frames) == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
}
close(fd);
}
}
92 changes: 92 additions & 0 deletions demos/high_res/button.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>

#include <stdarg.h>
#include <stdint.h>
#include <string.h>

#include <linux/fb.h>
#include <sys/mman.h>
#include "lv_demo_high_res_private.h"

int button_configured = 0;

const char *ev_type[EV_CNT] = {
[EV_SYN] = "EV_SYN",
[EV_KEY] = "EV_KEY",
[EV_REL] = "EV_REL",
[EV_ABS] = "EV_ABS",
[EV_MSC] = "EV_MSC",
[EV_SW] = "EV_SW",
[EV_LED] = "EV_LED",
[EV_SND] = "EV_SND",
[EV_REP] = "EV_REP",
[EV_FF] = "EV_FF",
[EV_PWR] = "EV_PWR",
[EV_FF_STATUS] = "EV_FF_STATUS",
[EV_MAX] = "EV_MAX",
};

const char *ev_code_syn[SYN_CNT] = {
[SYN_REPORT] = "SYN_REPORT",
[SYN_CONFIG] = "SYN_CONFIG",
[SYN_MT_REPORT] = "SYN_MT_REPORT",
[SYN_DROPPED] = "SYN_DROPPED",
[SYN_MAX] = "SYN_MAX",
};

void *button_init(lv_demo_high_res_api_t * api)
{
int fd;
char dev[] = "/dev/input/eventX";
struct input_event ie;

char buffer[128];
FILE *fp = popen("cat /proc/bus/input/devices | grep Phys=gpio-keys/input", "r");

if (fp == NULL) {
perror("popen failed");
return 1;
}

if (fgets(buffer, sizeof(buffer), fp) != NULL) {
int len_dev = strlen(dev);
int len_buffer = strlen(buffer);
dev[len_dev - 1] = buffer[len_buffer - 2];
button_configured = 1;
}
else{
printf("Button not Configured\n");
return 0;
}

if ((fd = open(dev, O_RDONLY)) == -1) {
perror("opening device");
exit(EXIT_FAILURE);
}

int last_val=-1;
int curr_val=-1;
while (read(fd, &ie, sizeof(struct input_event))) {
if(ie.type != 1)
continue;
curr_val = ie.value;
if(curr_val==0 && last_val > 0){
fprintf(stderr, "Button Released!!\n");
lv_lock();
int lock_status = lv_subject_get_int(&api->subjects.locked);
lv_subject_set_int(&api->subjects.locked, !lock_status);
lv_unlock();
}
last_val = curr_val;
}

close(fd);

return EXIT_SUCCESS;
}
133 changes: 133 additions & 0 deletions demos/high_res/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include "lv_demo_high_res_private.h"


static void slice(const char* str, char* result, size_t start, size_t end) {
strncpy(result, str + start, end - start);
}

void *clock_init(lv_demo_high_res_api_t * api) {
int last_minute = -1;
while(1){
char buffer[128];
FILE *fp = popen("date", "r");

if (fp == NULL) {
perror("popen failed");
return 1;
}

if (fgets(buffer, sizeof(buffer), fp) == NULL) {
continue;
}

char day[10];
char sliced_day[5];
slice(buffer, sliced_day, 0, 0 + 3);
if(strcmp(sliced_day, "Mon")==0){
strcpy(day, "Monday");
}
else if(strcmp(sliced_day, "Tue")==0){
strcpy(day, "Tuesday");
}
else if(strcmp(sliced_day, "Wed")==0){
strcpy(day, "Wednesday");
}
else if(strcmp(sliced_day, "Thu")==0){
strcpy(day, "Thursday");
}
else if(strcmp(sliced_day, "Fri")==0){
strcpy(day, "Friday");
}
else if(strcmp(sliced_day, "Sat")==0){
strcpy(day, "Saturday");
}
else if(strcmp(sliced_day, "Sun")==0){
strcpy(day, "Sunday");
}

char month[10];
char sliced_month[5];
slice(buffer, sliced_month, 4, 4 + 3);
if(strcmp(sliced_month, "Jan")==0){
strcpy(month, "January");
}
else if(strcmp(sliced_month, "Feb")==0){
strcpy(month, "February");
}
else if(strcmp(sliced_month, "Mar")==0){
strcpy(month, "March");
}
else if(strcmp(sliced_month, "Apr")==0){
strcpy(month, "April");
}
else if(strcmp(sliced_month, "May")==0){
strcpy(month, "May");
}
else if(strcmp(sliced_month, "Jun")==0){
strcpy(month, "June");
}
else if(strcmp(sliced_month, "Jul")==0){
strcpy(month, "July");
}
else if(strcmp(sliced_month, "Aug")==0){
strcpy(month, "August");
}
else if(strcmp(sliced_month, "Sep")==0){
strcpy(month, "September");
}
else if(strcmp(sliced_month, "Oct")==0){
strcpy(month, "October");
}
else if(strcmp(sliced_month, "Nov")==0){
strcpy(month, "November");
}
else if(strcmp(sliced_month, "Dec")==0){
strcpy(month, "December");
}

char sliced_date[4];
slice(buffer, sliced_date, 8, 8 + 2);
int date = atoi(sliced_date);

char sliced_hour[4];
slice(buffer, sliced_hour, 11, 11 + 2);
int hour = atoi(sliced_hour);

char sliced_minute[4];
slice(buffer, sliced_minute, 14, 14 + 2);
int minute = atoi(sliced_minute);
if(last_minute==-1){
lv_lock();
lv_subject_set_pointer(&api->subjects.month_name, month);
lv_subject_set_int(&api->subjects.month_day, date);
lv_subject_set_pointer(&api->subjects.week_day_name, day);
lv_subject_set_int(&api->subjects.hour, hour);
lv_subject_set_int(&api->subjects.minute, minute);
lv_unlock();
last_minute = minute;
}

char sliced_year[4];
slice(buffer, sliced_year, 24, 24 + 4);
int year = atoi(sliced_year);

pclose(fp);

if(last_minute != minute){
lv_lock();
lv_subject_set_pointer(&api->subjects.month_name, month);
lv_subject_set_int(&api->subjects.month_day, date);
lv_subject_set_pointer(&api->subjects.week_day_name, day);
lv_subject_set_int(&api->subjects.hour, hour);
lv_subject_set_int(&api->subjects.minute, minute);
lv_unlock();
}
last_minute = minute;
usleep(1000000);
}
}
Loading