Why this linux module code not pressing keys? - linux-kernel

I'm trying to make module that simulate press keys.
Why this linux module code not pressing keys?
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/init.h>
#include<linux/device.h>
#include<linux/timer.h>
#include<linux/kdev_t.h>
#include<linux/jiffies.h>
#include<linux/input.h>
#include<asm/io.h>
static struct input_dev *key_dev;
static int hellopress_init(void)
{
key_dev = input_allocate_device();
set_bit(EV_KEY,key_dev->evbit);
set_bit(KEY_A,key_dev->keybit);
set_bit(KEY_B,key_dev->keybit);
set_bit(KEY_C,key_dev->keybit);
set_bit(KEY_D,key_dev->keybit);
set_bit(KEY_E,key_dev->keybit);
set_bit(KEY_F,key_dev->keybit);
set_bit(KEY_G,key_dev->keybit);
set_bit(KEY_H,key_dev->keybit);
set_bit(KEY_I,key_dev->keybit);
set_bit(KEY_J,key_dev->keybit);
set_bit(KEY_K,key_dev->keybit);
set_bit(KEY_L,key_dev->keybit);
set_bit(KEY_M,key_dev->keybit);
set_bit(KEY_N,key_dev->keybit);
set_bit(KEY_O,key_dev->keybit);
set_bit(KEY_P,key_dev->keybit);
set_bit(KEY_Q,key_dev->keybit);
set_bit(KEY_R,key_dev->keybit);
set_bit(KEY_S,key_dev->keybit);
set_bit(KEY_T,key_dev->keybit);
set_bit(KEY_U,key_dev->keybit);
set_bit(KEY_V,key_dev->keybit);
set_bit(KEY_W,key_dev->keybit);
set_bit(KEY_X,key_dev->keybit);
set_bit(KEY_Y,key_dev->keybit);
set_bit(KEY_Z,key_dev->keybit);
input_register_device(key_dev);
input_report_key(key_dev, KEY_A, 1);
input_report_key(key_dev, KEY_A, 0);
input_report_key(key_dev, KEY_B, 1);
input_report_key(key_dev, KEY_B, 0);
input_report_key(key_dev, KEY_C, 1);
input_report_key(key_dev, KEY_C, 0);
input_report_key(key_dev, KEY_D, 1);
input_report_key(key_dev, KEY_D, 0);
input_report_key(key_dev, KEY_E, 1);
input_report_key(key_dev, KEY_E, 0);
input_report_key(key_dev, KEY_F, 1);
input_report_key(key_dev, KEY_F, 0);
input_report_key(key_dev, KEY_G, 1);
input_report_key(key_dev, KEY_G, 0);
input_report_key(key_dev, KEY_H, 1);
input_report_key(key_dev, KEY_H, 0);
input_report_key(key_dev, KEY_I, 1);
input_report_key(key_dev, KEY_I, 0);
input_report_key(key_dev, KEY_J, 1);
input_report_key(key_dev, KEY_J, 0);
input_report_key(key_dev, KEY_K, 1);
input_report_key(key_dev, KEY_K, 0);
input_report_key(key_dev, KEY_L, 1);
input_report_key(key_dev, KEY_L, 0);
input_report_key(key_dev, KEY_M, 1);
input_report_key(key_dev, KEY_M, 0);
input_report_key(key_dev, KEY_N, 1);
input_report_key(key_dev, KEY_N, 0);
input_report_key(key_dev, KEY_O, 1);
input_report_key(key_dev, KEY_O, 0);
input_report_key(key_dev, KEY_P, 1);
input_report_key(key_dev, KEY_P, 0);
input_report_key(key_dev, KEY_Q, 1);
input_report_key(key_dev, KEY_Q, 0);
input_report_key(key_dev, KEY_R, 1);
input_report_key(key_dev, KEY_R, 0);
input_report_key(key_dev, KEY_S, 1);
input_report_key(key_dev, KEY_S, 0);
input_report_key(key_dev, KEY_T, 1);
input_report_key(key_dev, KEY_T, 0);
input_report_key(key_dev, KEY_U, 1);
input_report_key(key_dev, KEY_U, 0);
input_report_key(key_dev, KEY_V, 1);
input_report_key(key_dev, KEY_V, 0);
input_report_key(key_dev, KEY_W, 1);
input_report_key(key_dev, KEY_W, 0);
input_report_key(key_dev, KEY_X, 1);
input_report_key(key_dev, KEY_X, 0);
input_report_key(key_dev, KEY_Y, 1);
input_report_key(key_dev, KEY_Y, 0);
input_report_key(key_dev, KEY_Z, 1);
input_report_key(key_dev, KEY_Z, 0);
input_report_key(key_dev, 30, 1);
input_report_key(key_dev, 30, 0);
input_report_key(key_dev, 76, 1);
input_report_key(key_dev, 76, 0);
input_sync(key_dev);
printk(KERN_INFO "Device Driverz Insert...Done!!!\n");
return 0;
}
static void hellopress_exit(void)
{
input_unregister_device(key_dev);
printk(KERN_INFO "Device Driver unloaded...Done!!!\n");
}
module_init(hellopress_init);
module_exit(hellopress_exit);
MODULE_AUTHOR("Vic");
MODULE_VERSION("0.1.2");
MODULE_LICENSE("GPL");
It compiles and run without errors, but no key press. Only one suspicious message:
[ 1275.986167] input: Unspecified device as /devices/virtual/input/input19
[ 1275.986622] Device Driverz Insert...Done!!!
What I made wrong? And how to make keypress from kernel module?
I want to simulate key presses as it was done by the real keyboard.

Related

SIGABRT: abort attempting encoding PCM to AAC

I am trying to encoding incoming raw PCM audio data into an AAC encoded audio file. The following crashes with SIGABRT when it hits the avcodec_encode_audio2 call:
aac_encoding.c
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
typedef struct AACEncoder {
AVFormatContext* pFormatCtx;
AVStream* audio_st;
AVCodecContext* pCodecCtx;
AVFrame* pFrame;
AVPacket* pkt;
uint8_t* frame_buf;
} AACEncoder;
AACEncoder *openEncoder(char* out_file) {
AACEncoder* encoder = (AACEncoder*)malloc(sizeof(AACEncoder*));
av_register_all();
AVFormatContext* pFormatCtx = avformat_alloc_context();
encoder->pFormatCtx = pFormatCtx;
AVOutputFormat* outFormat = av_guess_format(NULL, out_file, NULL);
pFormatCtx->oformat = outFormat;
if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
printf("Failed to open output file!\n");
return NULL;
}
AVStream* audio_st = avformat_new_stream(pFormatCtx, 0);
if (audio_st==NULL){
return NULL;
}
encoder->audio_st;
AVCodecContext* pCodecCtx = audio_st->codec;
encoder->pCodecCtx = pCodecCtx;
pCodecCtx->codec_id = outFormat->audio_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
pCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
pCodecCtx->sample_rate= 48000;
pCodecCtx->channel_layout = AV_CH_LAYOUT_MONO;
pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
pCodecCtx->bit_rate = 64000;
av_dump_format(pFormatCtx, 0, out_file, 1);
AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec){
printf("Can not find encoder!\n");
return NULL;
}
if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
printf("Failed to open encoder!\n");
return NULL;
}
AVFrame* pFrame = av_frame_alloc();
encoder->pFrame = pFrame;
pFrame->nb_samples= pCodecCtx->frame_size;
pFrame->format= pCodecCtx->sample_fmt;
int size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt, 1);
uint8_t* frame_buf = (uint8_t *)av_malloc(size);
encoder->frame_buf = frame_buf;
avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);
//Write Header
avformat_write_header(pFormatCtx,NULL);
AVPacket pkt;
encoder->pkt = &pkt;
av_new_packet(&pkt,size);
return encoder;
}
int writePCM(AACEncoder* encoder, int16_t* pcmData, size_t pcmSize) {
SwrContext* swr = swr_alloc();
av_opt_set_int(swr, "in_channel_layout", encoder->pCodecCtx->channel_layout, 0);
av_opt_set_int(swr, "out_channel_layout", encoder->pCodecCtx->channel_layout, 0);
av_opt_set_int(swr, "in_sample_rate", encoder->pCodecCtx->sample_rate, 0);
av_opt_set_int(swr, "out_sample_rate", encoder->pCodecCtx->sample_rate, 0);
av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
swr_init(swr);
printf("Initialized SwrContext\n");
fflush(stdout);
int ret = swr_convert(swr, encoder->pFrame->extended_data, pcmSize, &pcmData, pcmSize);
int got_frame=0;
if(ret < 0){
printf("Failed to resample!\n");
return -1;
}
//Encode
ret = avcodec_encode_audio2(encoder->pCodecCtx, encoder->pkt, encoder->pFrame, &got_frame);
printf("Encoded audio using AAC\n");
fflush(stdout);
swr_free(&swr);
if(ret < 0){
printf("Failed to encode!\n");
return -1;
}
if (got_frame==1){
printf("Succeed to encode 1 frame! \tsize:%5d\n", encoder->pkt->size);
encoder->pkt->stream_index = encoder->audio_st->index;
ret = av_write_frame(encoder->pFormatCtx, encoder->pkt);
av_free_packet(encoder->pkt);
}
}

What is wrong while providing arguments for sws_scale?

In the following code, I can't figure out what's wrong:
uint8_t *dstData[4];
int dstLinesize[4];
AVPixelFormat convertToPixFmt = AV_PIX_FMT_RGBA;
int ret;
// ...
printf("tmp_frame format: %d (%s) %dx%d\n", tmp_frame->format, av_get_pix_fmt_name((AVPixelFormat)tmp_frame->format), tmp_frame->width, tmp_frame->height);
// The above line prints: tmp_frame format: 23 (nv12) 480x480
int size = av_image_get_buffer_size(convertToPixFmt, tmp_frame->width, tmp_frame->height, 1);
uint8_t *buffer = (uint8_t *) av_malloc(size);
ret = av_image_copy_to_buffer(buffer, size,
(const uint8_t * const *)&tmp_frame->data[i],
(const int *)&tmp_frame->linesize[i], (AVPixelFormat)tmp_frame->format,
tmp_frame->width, tmp_frame->height, 1);
ASSERT(ret >= 0);
ret = av_image_fill_arrays(dstData, dstLinesize, buffer, convertToPixFmt, dest_width, dest_height, 1);
ASSERT(ret >= 0);
ret = sws_scale(
convertContext,
dstData,
dstLinesize,
0,
dest_width,
convertedFrame->data,
convertedFrame->linesize);
printf("sws_scale returns %d\n", ret); // prints: sws_scale returns 0
ASSERT(ret == tmp_frame->height);
// ...
It's part of a code which uses dxva2 to obtain tmp_frame. I inspired the code from hw_decode.c and am sure that there's no mistake in the code. The tmp_frame is properly made in NV12 format. The error occurs just when I call sws_scale and it's:
bad src image pointers
So I don't know how to provide pointers not to get this error and sws_scale may work properly.
Any idea?
I update the question to include my whole code:
static AVBufferRef *hw_device_ctx = NULL;
static enum AVPixelFormat hw_pix_fmt;
static FILE *output_file = NULL;
int main(int argc, char *argv[])
{
AVFormatContext *input_ctx = NULL;
int video_stream, ret;
AVStream *video = NULL;
AVCodecContext *decoder_ctx = NULL;
AVCodec *decoder = NULL;
AVPacket packet;
enum AVHWDeviceType type;
int i;
if (argc < 2)
{
fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
return -1;
}
type = av_hwdevice_find_type_by_name("dxva2");
ASSERT(type != AV_HWDEVICE_TYPE_NONE);
ASSERT(avformat_open_input(&input_ctx, argv[1], NULL, NULL) == 0);
ASSERT(avformat_find_stream_info(input_ctx, NULL) >= 0);
video_stream = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
ASSERT(video_stream >= 0);
decoder_ctx = avcodec_alloc_context3(decoder);
ASSERT(decoder_ctx);
video = input_ctx->streams[video_stream];
ASSERT(avcodec_parameters_to_context(decoder_ctx, video->codecpar) >= 0);
ASSERT(av_hwdevice_ctx_create(&hw_device_ctx, type, NULL, NULL, 0) >= 0);
decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
ASSERT(avcodec_open2(decoder_ctx, decoder, NULL) >= 0);
printf("video info: %dx%d\n", decoder_ctx->width, decoder_ctx->height);
AVFrame *frame = av_frame_alloc();
ASSERT(frame);
AVFrame *sw_frame = av_frame_alloc();
ASSERT(sw_frame);
AVFrame* convertedFrame = av_frame_alloc();
ASSERT(convertedFrame);
AVPixelFormat convertToPixFmt = AV_PIX_FMT_RGBA;
//int dest_width = 320, dest_height = 200;
int dest_width = decoder_ctx->width, dest_height = decoder_ctx->height;
SwsContext* convertContext = sws_getContext(decoder_ctx->width, decoder_ctx->height, AV_PIX_FMT_YUV420P,
dest_width, dest_height, convertToPixFmt,
SWS_FAST_BILINEAR, NULL, NULL, NULL);
ASSERT(convertContext);
int convertedFrameAspectBufferSize = avpicture_get_size(convertToPixFmt, dest_width, dest_height);
void *convertedFrameBuffer = av_malloc(convertedFrameAspectBufferSize);
avpicture_fill((AVPicture*)convertedFrame, (uint8_t *)convertedFrameBuffer, convertToPixFmt, dest_width, dest_height);
output_file = fopen("1.out", "w+");
for (int i = 0; /*i < 20*/; i++)
{
ret = av_read_frame(input_ctx, &packet);
if (ret == AVERROR_EOF)
break;
ASSERT(ret >= 0);
if (video_stream != packet.stream_index)
continue;
int ret = avcodec_send_packet(decoder_ctx, &packet);
ASSERT(ret >= 0);
//printf("%p", decoder->hw_configs->hwaccel);
ret = avcodec_receive_frame(decoder_ctx, frame);
if (ret < 0)
printf("%d\t%d\n", i, ret);
AVFrame *tmp_frame;
if (frame->format > 0) // hw enabled
{
ASSERT(av_hwframe_transfer_data(sw_frame, frame, 0) >= 0);
tmp_frame = sw_frame;
}
else
{
tmp_frame = frame;
}
printf("frame format: %d (%s) %dx%d\n", frame->format, av_get_pix_fmt_name((AVPixelFormat)frame->format), frame->width, frame->height);
printf("sw_frame format: %d (%s) %dx%d\n", sw_frame->format, av_get_pix_fmt_name((AVPixelFormat)sw_frame->format), sw_frame->width, sw_frame->height);
printf("tmp_frame format: %d (%s) %dx%d\n", tmp_frame->format, av_get_pix_fmt_name((AVPixelFormat)tmp_frame->format), tmp_frame->width, tmp_frame->height);
/*
video info: 480x480
frame format: 53 (dxva2_vld) 480x480
sw_frame format: 23 (nv12) 480x480
[swscaler # 004cb2c0] bad src image pointers
*/
int size = av_image_get_buffer_size(convertToPixFmt, tmp_frame->width, tmp_frame->height, 1);
uint8_t *buffer = (uint8_t *) av_malloc(size);
ret = av_image_copy_to_buffer(buffer, size,
(const uint8_t * const *)&tmp_frame->data[i],
(const int *)&tmp_frame->linesize[i], (AVPixelFormat)tmp_frame->format,
tmp_frame->width, tmp_frame->height, 1);
ASSERT(ret > 0);
ret = av_image_fill_arrays(dstData, dstLinesize, buffer, convertToPixFmt, dest_width, dest_height, 1);
ASSERT(ret > 0);
ret = sws_scale(
convertContext,
tmp_frame->data,
tmp_frame->linesize,
0,
dest_width,
convertedFrame->data,
convertedFrame->linesize);
printf("sws_scale returns %d\n", ret);
ASSERT(ret == tmp_frame->height);
ret = fwrite(convertedFrame->data, tmp_frame->height * tmp_frame->width, 1, output_file);
ASSERT(ret == 1);
break;
}
av_frame_free(&frame);
av_packet_unref(&packet);
avcodec_free_context(&decoder_ctx);
avformat_close_input(&input_ctx);
av_buffer_unref(&hw_device_ctx);
return 0;
}

How to set buffer size when encoding video?

I use the following code to encode an incoming h264 stream:
int ret;
ffmpeg::AVDictionary *opts2 = NULL;
av_dict_set(&opts2, "preset", "medium", 0);
av_dict_set(&opts2, "crf", "29", 0);
av_dict_set(&opts2, "profile", "baseline", 0);
av_dict_set(&opts2, "level", "30", 0);
av_dict_set(&opts2, "maxrate", "200000", 0);
av_dict_set(&opts2, "minrate", "0", 0);
av_dict_set(&opts2, "bufsize", "2000000", 0);
ffmpeg::AVOutputFormat* fmt = ffmpeg::av_guess_format("mpeg", NULL, NULL);
// Open the context
//---------------------------------------------------------------------
outFormatCtx = ffmpeg::avformat_alloc_context();
if (!outFormatCtx)
{
return false;
}
//Set the output format
//----------------------------------------------------------------------
outFormatCtx->oformat = fmt;
// Open the output file
//-------------------------------------
if (!(outFormatCtx->flags & AVFMT_NOFILE))
{
ret = ffmpeg::avio_open2(&outFormatCtx->pb, "Record.avi", AVIO_FLAG_WRITE, NULL, NULL);
if (ret < 0)
{
return false;
}
}
// Create the output stream
// -------------------------------------
ffmpeg::AVStream* out_stream = ffmpeg::avformat_new_stream(outFormatCtx, inputStream->codec->codec);
if (!out_stream)
{
return false;
}
//Set the stream parameters
//-------------------------------------
ret = ffmpeg::avcodec_copy_context(out_stream->codec, inputStream->codec);
if (ret < 0)
{
return false;
}
// Check then setup for global headers
//-------------------------------------
if (outFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
av_dump_format(outFormatCtx, 0, "Record.avi", 1);
//Write the header
//--------------------------------------
ret = ffmpeg::avformat_write_header(outFormatCtx, &opts2);
if (ret < 0)
{
return false;
}
However I always get the warning:
"VBV buffer size not set, using default size of 130KB\n"
"If you want the mpeg file to be compliant to some specification\n"
"Like DVD, VCD or others, make sure you set the correct buffer size\n"
I have tried very hard, but how could I set this VBV buffer size?

Arduino TimeAlarms are not working sometimes

i use an Arduino Mega compatible board (SainSmart Mega 2560 R3 ATmega2560-16AU) which has a rtc module attached (v1.1 ds1307rtc) and working on it with the attached code. I have used the TimeAlarms library (downloaded it from http://www.pjrc.com/teensy/td_libs_TimeAlarms.html) to have an alarm every hour. The alarms should occur at different minutes every hour, but for testing i have set them all to the 12th minute.
This code is waiting for a correct time, which i can set via usb, serial interface. The code is working fine most of the times. But sometimes the alarm is not activated and my leds are not flashing. I don't know why and when this happens, i didn't any changes between the working flashing and not working. Also i can't see any hours where it doesn't working correct, afaik it works correct, but fails sometimes. If it failes, it looks like all hours after the failing hour are also failing and no alarm is triggered.
I know about the restriction of 6 alarms in TimeAlarms.h and set the variable dtNBR_ALARMS in this file to 25.
As you can see i have implemented a printTime function which print out the rtc and the system time and always both are correct.
Has anyone an idea what i am doing wrong or why it fails sometimes?
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>
tmElements_t tm;
int pwm_2 = 2;
int pwm_3 = 3;
int pwm_4 = 4;
int pwm_5 = 5;
int pwm_6 = 6;
int pwm_7 = 7;
int pwm_8 = 8;
int pwm_9 = 9;
int pwm_10 = 10;
int pwm_11 = 11;
int pwm_12 = 12;
int pwm_13 = 13;
//delay in the for loops
int dly = 120;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
setSyncProvider(RTC.get); // the function to get the time from the RTC
RTC.read(tm);
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tm.Year);
printTime();
//wait for time input via serial, time must be set at every system boot
while(tmYearToCalendar(tm.Year) < 2014) {
Serial.println("wait in time loop, you have to set a time, current time is not correct:");
printTime();
if (Serial.available()) {
time_t t = processSyncMessage();
Serial.println("wait in time loop");
if (t != 0) {
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
Serial.println("time is succesfully set to");
printTime();
break;
}
}
delay(1000);
}
Serial.println("Time is already set to:");
printTime();
//set alarms for all hours in UTC, not in CET
Alarm.alarmRepeat(0, 12, 0, shotAlarm);
Alarm.alarmRepeat(1, 12, 0, shotAlarm);
Alarm.alarmRepeat(2, 12, 0, shotAlarm);
Alarm.alarmRepeat(3, 12, 0, shotAlarm);
Alarm.alarmRepeat(4, 12, 0, shotAlarm);
Alarm.alarmRepeat(5, 12, 0, shotAlarm);
Alarm.alarmRepeat(6, 12, 0, shotAlarm);
Alarm.alarmRepeat(7, 12, 0, shotAlarm);
Alarm.alarmRepeat(8, 12, 0, shotAlarm);
Alarm.alarmRepeat(9, 12, 0, shotAlarm);
Alarm.alarmRepeat(10, 12, 0, shotAlarm);
Alarm.alarmRepeat(11, 12, 0, shotAlarm);
Alarm.alarmRepeat(12, 12, 0, shotAlarm);
Alarm.alarmRepeat(13, 12, 0, shotAlarm);
Alarm.alarmRepeat(14, 12, 0, shotAlarm);
Alarm.alarmRepeat(15, 12, 0, shotAlarm);
Alarm.alarmRepeat(16, 12, 0, shotAlarm);
Alarm.alarmRepeat(17, 12, 0, shotAlarm);
Alarm.alarmRepeat(18, 12, 0, shotAlarm);
Alarm.alarmRepeat(19, 12, 0, shotAlarm);
Alarm.alarmRepeat(20, 12, 0, shotAlarm);
Alarm.alarmRepeat(21, 12, 0, shotAlarm);
Alarm.alarmRepeat(22, 12, 0, shotAlarm);
Alarm.alarmRepeat(23, 12, 0, shotAlarm);
// declare pin 2-13 to be an output:
pinMode(pwm_2, OUTPUT); //red
pinMode(pwm_3, OUTPUT); //blue
pinMode(pwm_4, OUTPUT); //green
pinMode(pwm_5, OUTPUT); //red
pinMode(pwm_6, OUTPUT); //blue
pinMode(pwm_7, OUTPUT); //green
pinMode(pwm_8, OUTPUT); //red
pinMode(pwm_9, OUTPUT); //blue
pinMode(pwm_10, OUTPUT); //green
pinMode(pwm_11, OUTPUT); //red
pinMode(pwm_12, OUTPUT); //blue
pinMode(pwm_13, OUTPUT); //green
}
void shotAlarm() {
Serial.println("SHOTALARM");
analogWrite(pwm_2, 255);
analogWrite(pwm_5, 255);
analogWrite(pwm_8, 255);
analogWrite(pwm_11, 255);
analogWrite(pwm_3, 255);
analogWrite(pwm_4, 255);
analogWrite(pwm_6, 255);
analogWrite(pwm_7, 255);
analogWrite(pwm_9, 255);
analogWrite(pwm_10, 255);
analogWrite(pwm_12, 255);
analogWrite(pwm_13, 255);
for(int a = 0; a < 60; a = a+1) {
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
delay (5);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
delay (5);
}
}
}
void loop() {
Alarm.delay(0);
Serial.println("new loop");
printTime();
analogWrite(pwm_2, 0);
analogWrite(pwm_5, 0);
analogWrite(pwm_8, 0);
analogWrite(pwm_11, 0);
analogWrite(pwm_3, 255);
analogWrite(pwm_4, 255);
analogWrite(pwm_6, 255);
analogWrite(pwm_7, 255);
analogWrite(pwm_9, 255);
analogWrite(pwm_10, 255);
analogWrite(pwm_12, 255);
analogWrite(pwm_13, 255);
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_4, i); //green
analogWrite(pwm_7, i); //green
analogWrite(pwm_10, i); //green
analogWrite(pwm_13, i); //green
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
Alarm.delay (dly);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_3, i); //blue
analogWrite(pwm_6, i); //blue
analogWrite(pwm_9, i); //blue
analogWrite(pwm_12, i); //blue
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_4, i); //green
analogWrite(pwm_7, i); //green
analogWrite(pwm_10, i); //green
analogWrite(pwm_13, i); //green
Alarm.delay (dly);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_3, i); //blue
analogWrite(pwm_6, i); //blue
analogWrite(pwm_9, i); //blue
analogWrite(pwm_12, i); //blue
Alarm.delay (dly);
}
Alarm.delay(0);
}
void printTime() {
if (RTC.read(tm)) {
Serial.print("Ok, RTC Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
}
else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
}
else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
}
Serial.print("Ok, System Time = ");
print2digits(hour());
Serial.write(':');
print2digits(minute());
Serial.write(':');
print2digits(second());
Serial.print(", Date (D/M/Y) = ");
Serial.print(day());
Serial.write('/');
Serial.print(month());
Serial.write('/');
Serial.print(year());
Serial.println();
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
For all who read this thread later:
I found the mistake in my code. The following line was wrong:
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tm.Year);
The correct version is:
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tmYearToCalendar(tm.Year));
Unfortunately i forgot to convert the year value to the expected format. The code from above is working very fine with this correction. I checked the alarms for more then a week and it is working very stable.
Thanks all for their help.
One thing to mention is that in the read me of the TimerAlarm library, it says you can only define up to 6 alarms only, but that can be changed inside the library by dtNBR_ALARMS field. The number ofcourse is restricted by ram but you can easily add that since the Arduino mega has a significantly large SRAM. So this might be worth looking at if you experience further problems with the alarms.

create toolbar with my bitmap images

this is an example code of msdn to create toolbar, but this example use the standard images of the system.
What do I need to change in this code to use my images from resource file, for example: IDB_COPY BITMAP "copy.bmp", and IDB_CUT BITMAP "cut.bmp", and IDB_PASTE BITMAP "paste.bmp".
HIMAGELIST g_hImageList = NULL;
HWND CreateSimpleToolbar(HWND hWndParent)
{
// Declare and initialize local constants.
const int ImageListID = 0;
const int numButtons = 3;
const int bitmapSize = 16;
const DWORD buttonStyles = BTNS_AUTOSIZE;
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
hWndParent, NULL, g_hInst, NULL);
if (hWndToolbar == NULL)
return NULL;
// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
numButtons, 0);
// Set the image list.
SendMessage(hWndToolbar, TB_SETIMAGELIST,
(WPARAM)ImageListID,
(LPARAM)g_hImageList);
// Load the button images.
SendMessage(hWndToolbar, TB_LOADIMAGES,
(WPARAM)IDB_STD_SMALL_COLOR,
(LPARAM)HINST_COMMCTRL);
// Initialize button info.
// IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.
TBBUTTON tbButtons[numButtons] =
{
{ MAKELONG(STD_FILENEW, ImageListID), IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
{ MAKELONG(STD_FILEOPEN, ImageListID), IDM_OPEN, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
{ MAKELONG(STD_FILESAVE, ImageListID), IDM_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)L"Save"}
};
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
// Resize the toolbar, and then show it.
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, TRUE);
return hWndToolbar;
}
I found this solution:
const int ID_TB_STANDARD = 0;
const int ID_IL_STANDARD = 0;
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_TOOLTIPS, 0, 0, 0, 0, hWnd, (HMENU)ID_TB_STANDARD, hInstance, NULL);
HIMAGELIST hImageList = ImageList_LoadBitmap(hInstance, MAKEINTRESOURCEW(IDB_CUT), 16, 0, RGB(255, 0, 255));
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_COPY)), NULL);
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PASTE)), NULL);
SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ID_IL_STANDARD, (LPARAM)hImageList);
SendMessage(hWndToolbar, (UINT) TB_SETHOTIMAGELIST, 0, (LPARAM)hHotImageList);
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[3] =
{
{0,ID_CUT,TBSTATE_ENABLED,TBSTYLE_BUTTON},
{1,ID_COPY,TBSTATE_ENABLED,TBSTYLE_BUTTON},
{2,ID_PASTE,TBSTATE_ENABLED,TBSTYLE_BUTTON},
};
SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&tbb);
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar , SW_SHOW);

Resources