簡易 Java 碼錶(A Simple Java Stopwatch)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog. Thank you very much.

程式執行畫面如下(不好意思,美工沒什麼處理):
This application screenshot is the below picture:

GUI是用Netbeans建立出來的,程式碼在 https://github.com/pinglunliao/JavaStopWatch
This application is created by Netbeans. Here is the source code:https://github.com/pinglunliao/JavaStopWatch

mBlock 單元十一:模擬雲朵燈 ( mBlock Unit 11: Cloud Lighting Simulation )

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

延續mBlock 單元十:模擬閃電之LED燈,利用mBlock中的自訂積木功能來增加LED閃爍的積木,方便管理程式積木,此外加上多顆LED後,感覺就比較像雲朵燈的效果。
硬體電路:


硬體材料:
1. Arduino UNO R3 板子 x 1
2. 麵包板 x 1
3. LED燈,藍色、綠色、紅色、黃色各1顆,白色三顆
4. 杜邦線公對公 x 9
5. 220歐姆電阻 x 7
6. 透明塑膠袋 x 1

完成圖:


程式積木:


示範影片:

試著用不一樣顏色的塑膠袋看看或有什麼效果。

影音播放器 Video Player

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

在用過KMplayerPotPlayer後,覺得PotPlayer比較好用而且不是只有我這樣認為喔:

PotPlayer 1.6.58772 可攜式阿榮版 (正式版) (1.6.58970 Beta) - 取代KMPlayer的免費影片播放軟

[下載] PotPlayer 繁體中文版,更勝 KMPlayer 的影音播放器!

所以筆者現在變成KMPlayer的叛徒啦。

mBlock 單元十:模擬閃電之LED燈 ( mBlock Unit 10: Thunder Lighting Simulation )

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

主要概念是用亂數來控制LED的亮度與亮多久時間,此效果搭配寶特瓶與棉花就可以做雲朵燈了。

硬體電路:


硬體材料:
1. Arduino UNO R3 板子 x 1
2. 麵包板 x 1
3. LED燈,藍色、白色各1顆
4. 杜邦線公對公 x 3
5. 220歐姆電阻 x 2

完成圖:

程式積木:
 

示範影片:

mBlock與Arduino板子相容性問題

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

用了一段時間後,才發現我手上原廠的板子(下圖)在使用mBlock時,有個奇怪的現象:「在和Scratch互動時,會延遲5秒。」


然後換上USB to UART converter 的晶片則是使用 CH340的相容板,就正常了,而且尚未確定到底是USB to UART驅動程式的問題,還是硬體的問題,總之此問題要等mBlock官方來解決了。

有在使用mBlock的人請注意一下此問題,若是用mBlock的Arduino模式寫Arduino積木程式(不與Scratch互動時),此問題是不會發生的。(PS:至少我現在還沒有發生過)

mBlock 單元九:LED 燈條 ( mBlock Unit 9: Multiple LEDs with Different Colors )

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

此單元用五種顏色:綠、藍、紅、白、黃的LED燈,讓每顆LED燈依序快速閃爍。

硬體電路:


硬體材料:
1. Arduino UNO R3 板子 x 1
2. 麵包板 x 1
3. LED燈,綠色、藍色、紅色、白色、黃色各1顆
4. 杜邦線公對公 x 6
5. 220歐姆電阻 x 5

完成圖:


程式積木:


示範影片:

Linux:驅動程式除錯技術

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

Kernel Module不同於Application,需用其他方式的除錯方法,沒有printf可用,需用printk,甚至有可能讓Linux系統當機,於是比Application更難除錯,底下介紹幾種除錯技術。

Debugging Support in the Kernel

Kernel Configuration裡有個選項「Kernel hacking」供開發者勾選需要的除錯功能。

LDD3建議啟用下列選項(不同Kernel版本與不同CPU架構會有不同的選項)


以上都有附上說明的網址,若有需要請自行研究。


Debugging by Printing

應用程式所使用的printf是輸出到standard output(buffer I/O),但有時程式會在訊息輸出前就掛掉,而程式裡的buffer尚未印出來,因此要記得印到standard error。但Kernel Module不能使用printf輸出訊息到STDOUT,要改用printk

printk的輸出訊息是分等級的,如下表:
Loglevel
Description
KERN_EMERG
An emergency condition; the system is probably dead
KERN_ALERT
A problem that requires immediate attention
KERN_CRIT
A critical condition
KERN_ERR
An error
KERN_WARNING
A warning
KERN_NOTICE
A normal, but perhaps noteworthy, condition
KERN_INFO
An informational message
KERN_DEBUG
A debug messagetypically superfluous


Redirecting Console Messages

使用ioctl(TIOCLINUX)可將Kernel Message輸出到/dev/console裡,程式碼請見github

設定Console的位置
./setconsole 2

載入含有Kernel Messagemodule,例如hello
sudo insmod hello.ko
sudo rmmod hello

這樣printk的訊息也會輸出到tty2裡。
而檔案/proc/kmsg存有Kernel Message,於是可以用cat /proc/kmsg來觀察訊息。


Turning the Messages On and Off

可用macro的寫法,例如在scull.h裡:
#undef PDEBUG /* undef it, just in case */
#ifdef SCULL_DEBUG
# ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)
# else
/* This one for user space */
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
# endif
#else
# define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif
#undef PDEBUGG
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */

Makefile中:
# Comment/uncomment the following line to disable/enable debugging
DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
CFLAGS += $(DEBFLAGS)

Debugging by Querying

Debugging by Querying的方法如下:
  1. /proc檔案系統中建立檔案。
  2. ioctl API函式。
  3. 透過sysfs輸出特徵。
ioctl sysfs 不在此紀錄說明。

/proc檔案系統中建立檔案

此範例使用seq_file 介面,需寫四個function(start, next, stop, show)來使用seq_file,程式碼如下:

/* The scullmem proc implementation. */
int scull_read_procmem(struct seq_file *s, void *v)
{
int i, j;
int limit = s->size - 80; /* Don't print more characters than this. */

for (i = 0; i < scull_nr_devs && s->count <= limit; i++) {
struct scull_dev *d = &scull_devices[i];
struct scull_qset *qs = d->data;
if (mutex_lock_interruptible(&d->mutex))
return -ERESTARTSYS;
seq_printf(s, "\nDevice %i: qset %i, q %i, sz %li\n",
i, d->qset, d->quantum, d->size);
for (; qs && s->count <= limit; qs = qs->next) { /* Scan the list. */
seq_printf(s, " item at %p, qset at %p\n",
qs, qs->data);
if (qs->data && !qs->next) /* Dump only the last item. */
for (j = 0; j < d->qset; j++) {
if (qs->data[j])
seq_printf(s, " % 4i: %8p\n",
j, qs->data[j]);
}
}
mutex_unlock(&scull_devices[i].mutex);
}
return 0;
}

static int scullmem_proc_open(struct inode *inode, struct file *filp)
{
return single_open(filp, scull_read_procmem, NULL);
}

struct file_operations scullmem_proc_ops = {
.owner = THIS_MODULE,
.open = scullmem_proc_open,
.llseek = seq_lseek,
.read = seq_read,
.release = single_release,
};

/* The scullseq proc implementation. */

static void *scull_seq_start(struct seq_file *s, loff_t *pos)
{
if (*pos >= scull_nr_devs)
return NULL; /* No more to read. */
return scull_devices + *pos;
}

static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
(*pos)++;
if (*pos >= scull_nr_devs)
return NULL;
return scull_devices + *pos;
}

static void scull_seq_stop(struct seq_file *s, void *v)
{
/* There's nothing to do here! */
}

static int scull_seq_show(struct seq_file *s, void *v)
{
struct scull_dev *dev = (struct scull_dev *) v;
struct scull_qset *d;
int i;

if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
seq_printf(s, "\nDevice %i: qset %i, q %i, sz %li\n",
(int) (dev - scull_devices), dev->qset,
dev->quantum, dev->size);
for (d = dev->data; d; d = d->next) { /* Scan the list. */
seq_printf(s, " item at %p, qset at %p\n", d, d->data);
if (d->data && !d->next) /* Dump only the last item. */
for (i = 0; i < dev->qset; i++) {
if (d->data[i])
seq_printf(s, " % 4i: %8p\n",
i, d->data[i]);
}
}
mutex_unlock(&dev->mutex);
return 0;
}
/*
* Set up the sequence operator pointers.
*/
static struct seq_operations scull_seq_ops = {
.start = scull_seq_start,
.next = scull_seq_next,
.stop = scull_seq_stop,
.show = scull_seq_show
};

static int scullseq_proc_open(struct inode *inode, struct file *filp)
{
return seq_open(filp, &scull_seq_ops);
}

static struct file_operations scullseq_proc_ops = {
.owner = THIS_MODULE,
.open = scullseq_proc_open,
.llseek = seq_lseek,
.read = seq_read,
.release = seq_release,
};

/* Set up and remove the proc entries */

static void scull_create_proc(void)
{
proc_create_data("scullmem", 0 /* default mode */,
NULL /* parent dir */, &scullmem_proc_ops,
NULL /* client data */);
proc_create_data("scullseq", 0, NULL, &scullseq_proc_ops, NULL);
}

static void scull_remove_proc(void)
{
/* No problem if it was not registered. */
remove_proc_entry("scullmem", NULL /* parent dir */);
remove_proc_entry("scullseq", NULL);
}

若還不清楚seq_file介面怎麼用,請參考底下範例
/*
* seq_file interface sample.
*
*/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

static void *holan_seq_start(struct seq_file *s, loff_t *pos)
{
loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (! spos)
return NULL;
*spos = *pos;
return spos;
}

static void *holan_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
loff_t *spos = (loff_t *) v;
*pos = ++(*spos);
return spos;
}

static void holan_seq_stop(struct seq_file *s, void *v)
{
kfree (v);
}

static int holan_seq_show(struct seq_file *s, void *v)
{
loff_t *spos = (loff_t *) v;
seq_printf(s, "%Ld\n", *spos);
return 0;
}

static struct seq_operations holan_seq_ops = {
.start = holan_seq_start,
.next = holan_seq_next,
.stop = holan_seq_stop,
.show = holan_seq_show
};


static int holan_open(struct inode *inode, struct file *file)
{
return seq_open(file, &holan_seq_ops);
};

static struct file_operations holan_file_ops = {
.owner = THIS_MODULE,
.open = holan_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
static int holan_init(void)
{
proc_create("holan_seq", 0, NULL, &holan_file_ops);
return 0;
}

static void holan_exit(void)
{
remove_proc_entry("holan_seq", NULL);
}

MODULE_AUTHOR("Holan Liao");
MODULE_LICENSE("Free Sample");

module_init(holan_init);
module_exit(holan_exit);

Oops Messages

faulty.c程式可產生Oops Messages,此例子是因為null pointer的緣故,這種訊息通常很難除錯,下圖可看到錯誤訊息是從faulty_write所產生的。



System Hangs

此種狀況有個工具可以用—SysRq組合鍵,SysnRq在鍵盤上的位置如下圖,
SysRq需搭配Alt和第三個指令按鍵,例如SysRq + Alt + b會將系統重開機,其它的指令按鍵請參考這兒