2016年11月3日 星期四

如何一次設定一個 Click Event Handler 到同一 StackPanel 裡的所有Button

ref : http://stackoverflow.com/questions/37876936/event-handler-formatting-multiple-buttons-wpf?noredirect=1&lq=1

有這樣的做法, 在前面指定 Button.Click :

<StackPanel Button.Click="button_Click" Grid.RowSpan="20">
     <Button Grid.Column="0" Grid.Row="0" FontWeight="Bold" BorderBrush="Black" Style="{StaticResource greenButton}">LT 1</Button>
     <Button x:Name="btn100" Grid.Column="0" Grid.Row="2" Style="{StaticResource greenButton}">100</Button>
     <Button x:Name="btn101" Grid.Column="0" Grid.Row="3" Style="{StaticResource greenButton}">101</Button>
     <Button x:Name="btn102" Grid.Column="0" Grid.Row="4" Style="{StaticResource greenButton}">102</Button>
 </StackPanel>
 
但是這樣會有 null exception 跑出來, 當然可以用 (e.OriginalSource as Button) 來避這個問題, 
也可以在 Style 裡用 EventSetter :
 
<Style x:Key="greenButton" TargetType="Button">
    ...
    <EventSetter Event="Click" Handler="button_Click"/>
</Style> 

WPF Binding

將 TextBox 裡的值隨著 ListBox 的選項改變

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" Margin="401,312,0,0" VerticalAlignment="Top" Width="100" >
    <ListBoxItem>Line 1</ListBoxItem>
    <ListBoxItem>Line 2</ListBoxItem>
</ListBox>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="207,290,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
    <TextBox.Text>
        <Binding ElementName="listBox" Path="SelectedItem.Content"></Binding>
    </TextBox.Text>
</TextBox>

如下圖, TextBox 裡顯示 ListBox 選的項目:


2016年10月24日 星期一

MSP430 LaunchPad Timer_0 Interrupt

Using Timer_0 Interrupt to Toggle LED on MSP430 LaunchPad


#include <msp430.h>

#define OUTPUT_PIN  BIT0
/*
 * main.c
 *
 *  Testing timer_0 interrupt to toggle LED (P1.0)
 */

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer

    P1DIR |= OUTPUT_PIN; // Set P1.0 and P1.6 to output direction
    P1OUT |= OUTPUT_PIN;
    P1OUT ^= (OUTPUT_PIN);


    BCSCTL1 = CALBC1_8MHZ;
    DCOCTL = CALDCO_8MHZ;

    CCTL0 = CCIE;
    TACTL = TASSEL_2 + ID_3 + MC_1;     // Set the timer A to SMCLCK, UP to TACCR0, Input Divider: /8
    TACCR0 = 50000-1;       // 50ms @(8MHz/8)

    __enable_interrupt();

    __bis_SR_register(LPM0 + GIE); // LPM0 with interrupts enabled
    while(1);
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
    static unsigned char ucCount = 0;
    if(ucCount++ >= 20){
        P1OUT ^= OUTPUT_PIN;
        ucCount = 0;
    }

}

2016年8月25日 星期四

LME288 的處理方法

  1. Run command prompt as Administrator.
  2. Type (without quotes) "bcdedit /set IncreaseUserVa 3072"
  3. Reboot computer.

2016年7月19日 星期二

在 Batch File 中使用 findstr 比對 log 檔中的訊息

當 Batch File 需要判斷某個程式輸出的 log 檔內容時,  findstr 是一個可用的工具.

目前用過的參數:

參數說明:
findstr /?

比對檔案中的文字
findstr /c:string filename

可用 ERRORLEVEL 判斷是否有符合字串.

ERRORLEVEL 為 0 時, 有符合字串.
ERRORLEVEL 為 1 時, 沒有找到符合字串.

--
ERRORLEVEL 的用法:

IF ERRORRLEVEL N to check if the errorlevel is >= N. 
IF NOT ERRORLEVEL N to check if errorlevel is < N

例如:

findstr /C:FFFF info_1.hex
IF ERRORLEVEL 1 goto NOT_MATCH
echo MATCH
goto END

:NOT_MATCH

echo NOT_MATCH

:END

Batch file 指令太長如何換行

當 Batch File 的一行太長要換行時, 加入 "^" (不含 ") , 緊接著換行就可以了.
例如:

echo off
IF "%1"=="" goto end
echo %1

:end

寫成:


echo off
IF "%1"=="" ^
goto end

echo %1

:end

注意 "^" 一定要緊接著換行碼, 不能有空格!

2016年7月5日 星期二

python 中使用 enum


from enum import Enum
class Color(Enum):
    red = 1
    green = 2
    blue = 3